Build stateful, multi-actor AI applications with LangGraph. Create workflows with durable execution, human-in-the-loop patterns, and comprehensive memory management.
Build stateful, multi-actor applications with LLMs using LangGraph, a low-level orchestration framework for creating long-running, stateful agents with durable execution and human-in-the-loop capabilities.
This skill helps you create LangGraph-based agent workflows with state management, node-based execution graphs, and persistent memory. LangGraph is trusted by companies like Klarna, Replit, and Elastic for building production-grade agent systems.
First, install the LangGraph package:
```bash
pip install -U langgraph
```
For integration with LangChain components:
```bash
pip install -U langchain langgraph
```
Create a typed state dictionary that will be passed between nodes:
```python
from typing_extensions import TypedDict
class State(TypedDict):
text: str
counter: int
messages: list
```
**Key principle:** State is the shared memory that flows through your graph. Each node reads from and writes to this state.
Define functions that transform state. Each node receives state and returns a dictionary of updates:
```python
def node_a(state: State) -> dict:
return {"text": state["text"] + "a", "counter": state.get("counter", 0) + 1}
def node_b(state: State) -> dict:
return {"text": state["text"] + "b"}
def conditional_node(state: State) -> dict:
# Nodes can contain any logic
if state["counter"] > 3:
return {"text": state["text"] + " (limit reached)"}
return {"text": state["text"]}
```
Use `StateGraph` to define your workflow:
```python
from langgraph.graph import START, END, StateGraph
graph = StateGraph(State)
graph.add_node("node_a", node_a)
graph.add_node("node_b", node_b)
graph.add_node("conditional", conditional_node)
graph.add_edge(START, "node_a")
graph.add_edge("node_a", "node_b")
graph.add_edge("node_b", "conditional")
graph.add_edge("conditional", END)
```
Implement branching logic based on state:
```python
def route_logic(state: State) -> str:
if state["counter"] > 5:
return "end_path"
return "continue_path"
graph.add_conditional_edges(
"node_a",
route_logic,
{
"continue_path": "node_b",
"end_path": END
}
)
```
Compile the graph and invoke it with initial state:
```python
app = graph.compile()
result = app.invoke({"text": "", "counter": 0})
print(result)
```
Enable state persistence for long-running agents:
```python
from langgraph.checkpoint.sqlite import SqliteSaver
memory = SqliteSaver.from_conn_string(":memory:")
app = graph.compile(checkpointer=memory)
config = {"configurable": {"thread_id": "user-123"}}
result = app.invoke({"text": "", "counter": 0}, config=config)
```
Add interruption points for human review:
```python
from langgraph.graph import interrupt
def review_node(state: State) -> dict:
# Interrupt execution for human review
interrupt("Please review the current state")
return state
graph.add_node("review", review_node)
```
Stream state updates in real-time:
```python
for chunk in app.stream({"text": "", "counter": 0}):
print(chunk)
```
Nest graphs within nodes for modular design:
```python
subgraph = StateGraph(State)
subgraph.add_node("sub_a", node_a)
graph.add_node("subgraph_node", subgraph.compile())
```
Execute multiple nodes simultaneously:
```python
from langgraph.graph import parallel
graph.add_edge("start", parallel(["node_a", "node_b", "node_c"]))
```
Add fallback nodes for error recovery:
```python
def error_handler(state: State) -> dict:
return {"text": "Error occurred", "counter": 0}
graph.add_node("error_handler", error_handler)
graph.add_edge("risky_node", "error_handler", on_error=True)
```
Use LangChain components within LangGraph nodes:
```python
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
def llm_node(state: State) -> dict:
llm = OpenAI()
prompt = PromptTemplate.from_template("Process: {text}")
result = llm(prompt.format(text=state["text"]))
return {"text": result}
```
1. **Keep state minimal**: Only include data that needs to be shared between nodes
2. **Use TypedDict**: Enforce state schema with type hints
3. **Idempotent nodes**: Design nodes to produce consistent results for the same input
4. **Checkpoint strategically**: Add checkpoints before expensive operations or external API calls
5. **Test incrementally**: Build and test nodes individually before composing complex graphs
6. **Use conditional routing**: Avoid deeply nested if/else logic; use graph edges instead
7. **Monitor with LangSmith**: Integrate LangSmith for observability in production
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/langgraph-stateful-agent-builder/raw