Introduction
Single agents hit a wall: one LLM trying to do research, write code, and review it produces mediocre results on all three. Multi-agent systems split the work — a researcher gathers facts, a coder writes code, a reviewer checks it. Each agent is good at one thing.
LangGraph is the framework for this: stateful graphs where nodes are agents and edges are handoffs. This post builds a 3-agent system (researcher → coder → reviewer) with real code.
Why LangGraph?
- Stateful — agents share a typed state object across the graph.
- Cyclic — agents can loop (reviewer sends work back to coder).
- Checkpointing — pause, resume, and replay runs (great for debugging).
- Streaming — stream intermediate steps to a UI.
Setup
pip install langgraph langchain-openai langchain-community
Set OPENAI_API_KEY in your env (or swap to Ollama/Mistral).
Step 1: Define the shared state
from typing import TypedDict, Annotated
from langgraph.graph.message import add_messages
class State(TypedDict):
task: str
research: str
code: str
review: str
messages: Annotated[list, add_messages]
add_messages is a reducer — it appends new messages instead of overwriting.
Step 2: Create the agents (nodes)
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
def researcher(state: State):
prompt = f"Research the following task and summarize key facts:\n{state['task']}"
result = llm.invoke(prompt)
return {"research": result.content, "messages": [result]}
def coder(state: State):
prompt = (f"Task: {state['task']}\n\nResearch:\n{state['research']}\n\n"
"Write Python code that solves the task. Return only code.")
result = llm.invoke(prompt)
return {"code": result.content, "messages": [result]}
def reviewer(state: State):
prompt = (f"Review this code for bugs and improvements:\n{state['code']}\n\n"
"Reply 'APPROVED' if good, otherwise list issues.")
result = llm.invoke(prompt)
return {"review": result.content, "messages": [result]}
Each node is just a function that takes state and returns partial updates.
Step 3: Build the graph
from langgraph.graph import StateGraph, END
graph = StateGraph(State)
graph.add_node("researcher", researcher)
graph.add_node("coder", coder)
graph.add_node("reviewer", reviewer)
graph.set_entry_point("researcher")
graph.add_edge("researcher", "coder")
graph.add_edge("coder", "reviewer")
# Conditional: reviewer can send back to coder
def should_revise(state: State):
return "coder" if "APPROVED" not in state["review"] else END
graph.add_conditional_edges("reviewer", should_revise)
app = graph.compile()
Step 4: Run it
result = app.invoke({"task": "Write a function that checks if a number is prime.",
"research": "", "code": "", "review": "", "messages": []})
print("=== CODE ===")
print(result["code"])
print("=== REVIEW ===")
print(result["review"])
The graph runs researcher → coder → reviewer, and loops coder ↔ reviewer until approved (or max iterations).
Step 5: Add tools to an agent
Give the researcher a search tool:
from langchain_community.tools import DuckDuckGoSearchRun
search = DuckDuckGoSearchRun()
def researcher(state: State):
# Agent decides whether to search
...
For tool-calling agents, use create_react_agent:
from langgraph.prebuilt import create_react_agent
researcher_agent = create_react_agent(llm, tools=[search], state_modifier="You are a researcher.")
Step 6: Stream intermediate steps
for step in app.stream({"task": "...", "research": "", "code": "", "review": "", "messages": []}):
for node, output in step.items():
print(f"\n--- {node} ---")
print(output.get("code") or output.get("review") or output.get("research", "")[:200])
Stream to a Streamlit UI for live “agent is thinking” feedback — pairs with st.status steps.
The supervisor pattern
Instead of fixed edges, a supervisor LLM decides which agent runs next:
from langgraph.prebuilt import create_supervisor
supervisor = create_supervisor(
[researcher_agent, coder_agent, reviewer_agent],
model=llm,
prompt="Route tasks: research → coder → reviewer. Approve when done.",
)
app = supervisor.compile()
The supervisor is an LLM that picks the next agent based on state. More flexible, but costs an extra LLM call per step.
Common mistakes
- No max iterations — cyclic graphs can loop forever. Set
recursion_limitinapp.invoke(..., config={"recursion_limit": 10}). - Shared state mutations — always return partial dicts; never mutate state in place.
- Vague agent prompts — each agent needs a clear role and output format.
- Skipping checkpointing — enable
SqliteSaverfor replay/debugging. - Too many agents — 3-4 is the sweet spot; more adds coordination overhead.
Key Concepts
- State = shared TypedDict across all agents
- Node = an agent (function taking state, returning partial updates)
- Edge = handoff (fixed or conditional)
- Supervisor = LLM that routes between agents
- Checkpointing = save/resume/replay runs
What to Try Next
- Add a human-in-the-loop node that pauses for approval before the coder runs.
- Persist state with
SqliteSaverfor resumable long-running tasks. - Stream the graph to a Streamlit UI with
st.statussteps. - Combine with MCP tools so agents can use external capabilities.
- Build a voice agent that delegates to a multi-agent backend.
FAQ
LangGraph vs CrewAI vs AutoGen?
CrewAI is higher-level (role-based agents, less control). AutoGen is conversation-based (agents chat). LangGraph is graph-based (explicit state, cycles, checkpointing). Pick LangGraph when you need control and reproducibility; CrewAI for quick prototypes.
Do I need LangChain to use LangGraph?
LangGraph is part of the LangChain ecosystem but can be used standalone with any LLM (OpenAI, Anthropic, Ollama). You don’t need LangChain chains.
How do I prevent infinite loops?
Set recursion_limit in the invoke config, and make conditional edges terminate (always have a path to END). Add a “max revisions” counter in state if needed.