LangGraph: Build Stateful AI Agent Workflows
LangGraph is a framework by LangChain for building stateful, multi-step AI agent workflows. It extends LangChain with graph-based orchestration for complex agentic systems.
What is LangGraph?
LangGraph is a library built on top of LangChain that enables developers to create stateful, multi-step AI agent workflows using a graph-based approach. Unlike simple chains, LangGraph allows cycles, conditional branching, and persistent state — making it ideal for complex agentic systems that need to plan, execute, and adapt.
Key features: •Graph-based orchestration — define agent workflows as directed graphs with nodes and edges •Stateful execution — maintain conversation history, tool results, and intermediate state across steps •Human-in-the-loop — pause execution for human review or input •Persistence — save and resume workflows across sessions •Streaming — stream intermediate results as agents work
LangGraph vs LangChain
LangChain provides the building blocks (LLM calls, tools, memory). LangGraph provides the orchestration layer that connects these blocks into complex, stateful workflows.
•LangChain = individual components (LLM, retriever, tools) •LangGraph = how components connect and flow (graphs, state, branching)
Think of LangChain as the bricks and LangGraph as the architecture. For simple chatbots, LangChain alone suffices. For multi-agent systems, autonomous research agents, or complex workflows with branching logic — you need LangGraph.
Core Concepts
State: A TypedDict or Pydantic model that holds all data flowing through your graph. Every node reads from and writes to this state.
Nodes: Functions that process state. Each node takes the current state, performs an action (LLM call, tool execution, data processing), and returns updated state.
Edges: Define the flow between nodes. Can be unconditional (always go to next node) or conditional (choose next node based on state).
Graph: The complete workflow defined by nodes and edges. Compiled into a runnable that executes the workflow.
Building a LangGraph Agent
A typical LangGraph agent follows this pattern:
1. Define state — what data flows through the graph 2. Create nodes — functions for LLM reasoning, tool execution, human review 3. Add edges — connect nodes with conditional logic 4. Compile — turn the graph into a runnable 5. Execute — run with input and stream results
The most common pattern is the ReAct loop: the agent reasons about what to do, calls a tool, observes the result, and decides whether to continue or finish.
Production Best Practices
•Use checkpointing — persist state to recover from failures •Add timeouts — prevent infinite loops in agent reasoning •Implement guardrails — validate tool calls before execution •Monitor costs — track token usage per node •Test with deterministic inputs — use fixed seeds for reproducible testing •Handle errors gracefully — add error nodes that recover or escalate
Code Example: Simple ReAct Agent
Here's a minimal LangGraph agent that uses tools:
pythonfrom langgraph.graph import StateGraph, END
from langchain_anthropic import ChatAnthropic
from langchain_core.messages import HumanMessage
from typing import TypedDict, Annotated# 1. Define state class AgentState(TypedDict): messages: Annotated[list, operator.add]
# 2. Create model with tools model = ChatAnthropic(model="claude-sonnet-4-20250514")
def call_model(state: AgentState): response = model.invoke(state["messages"]) return {"messages": [response]}
def should_continue(state: AgentState): last = state["messages"][-1] if hasattr(last, "tool_calls") and last.tool_calls: return "tools" return END
# 3. Build graph graph = StateGraph(AgentState) graph.add_node("agent", call_model) graph.add_node("tools", tool_node) # your tool executor graph.set_entry_point("agent") graph.add_conditional_edges("agent", should_continue) graph.add_edge("tools", "agent")
# 4. Compile and run app = graph.compile() result = app.invoke({"messages": [HumanMessage("What's the weather?")]}) ~~~
This creates the classic ReAct loop: the agent reasons, optionally calls a tool, observes the result, and decides whether to continue or stop.
Code Example: Multi-Agent Supervisor
A supervisor agent that delegates to specialist agents:
pythondef supervisor(state): # Decide which specialist to call response = model.invoke( f"Given this task: {state['task']}, " f"which specialist should handle it? " f"Options: researcher, coder, writer" ) return {"next_agent": response.content.strip().lower()}
def researcher(state): result = model.invoke(f"Research: {state['task']}") return {"results": [result.content]}
def coder(state): result = model.invoke(f"Write code for: {state['task']}") return {"results": [result.content]}
def writer(state): result = model.invoke(f"Write about: {state['task']}") return {"results": [result.content]}
# Build the graph graph = StateGraph(AgentState) graph.add_node("supervisor", supervisor) graph.add_node("researcher", researcher) graph.add_node("coder", coder) graph.add_node("writer", writer) graph.set_entry_point("supervisor")
# Route based on supervisor decision graph.add_conditional_edges("supervisor", lambda s: s["next_agent"], {"researcher": "researcher", "coder": "coder", "writer": "writer"} ) # All specialists return to supervisor for next decision for agent in ["researcher", "coder", "writer"]: graph.add_edge(agent, "supervisor") ~~~
This pattern scales to any number of specialist agents. The supervisor acts as an orchestrator, routing tasks based on their nature.
Learn LangGraph hands-on in our agentic AI training
Live, instructor-led sessions with hands-on coding. Taught by a senior AI engineer (Ex-Atlassian, Ex-PhonePe).
Learn MoreNot ready to commit?
Get the full session outline + a reminder before the session.
No spam. Just the session details + one reminder email.
Frequently Asked Questions
Is LangGraph free?↓
Yes, LangGraph is open-source and free to use. LangGraph Cloud (managed hosting) is a paid service by LangChain.
Do I need to know LangChain to use LangGraph?↓
Basic LangChain knowledge helps, but LangGraph can be used with any LLM provider. Understanding of graph concepts and state management is more important.
LangGraph vs CrewAI vs AutoGen?↓
LangGraph offers the most control with graph-based orchestration. CrewAI is simpler for role-based multi-agent teams. AutoGen (Microsoft) focuses on conversational multi-agent patterns. LangGraph is best for complex, production-grade systems.