LangGraph — Stateful AI Agent Graphs by LangChain
Framework for building stateful, multi-actor AI agent applications as directed graphs. Supports cycles, branching, persistence, and human-in-the-loop patterns. By LangChain. 8,000+ stars.
What it is
LangGraph is a framework by LangChain for building stateful, multi-actor AI agent applications as directed graphs. Instead of linear chains, LangGraph lets agents loop, branch, wait for human input, and checkpoint state automatically.
It targets AI engineers and teams building production agent systems that require retry logic, parallel tool calls, approval gates, or long-running workflows.
How it saves time or tokens
LangGraph eliminates the boilerplate of building state machines for agent orchestration. Its built-in persistence means you do not need to implement checkpointing or crash recovery. The human-in-the-loop primitives (interrupt_before, interrupt_after) replace custom webhook and polling logic. By modeling agents as graphs, you can visualize and debug complex multi-step flows instead of tracing through nested function calls.
How to use
- Install LangGraph:
pip install langgraph
- Define state and nodes:
from langgraph.graph import StateGraph, END
from typing import TypedDict
class State(TypedDict):
messages: list
next_step: str
def researcher(state: State):
return {'messages': state['messages'] + ['research done']}
def writer(state: State):
return {'messages': state['messages'] + ['draft complete']}
- Build and compile the graph:
graph = StateGraph(State)
graph.add_node('research', researcher)
graph.add_node('write', writer)
graph.add_edge('research', 'write')
graph.add_edge('write', END)
graph.set_entry_point('research')
app = graph.compile()
result = app.invoke({'messages': [], 'next_step': ''})
Example
A ReAct agent with validation loop:
def should_continue(state: State):
if 'valid' in state['messages'][-1]:
return 'end'
return 'retry'
graph = StateGraph(State)
graph.add_node('act', researcher)
graph.add_node('validate', writer)
graph.add_conditional_edges('validate', should_continue, {
'retry': 'act',
'end': END
})
graph.add_edge('act', 'validate')
graph.set_entry_point('act')
app = graph.compile()
Related on TokRepo
- Multi-agent frameworks — compare LangGraph with CrewAI, AutoGen, and others
- LangGraph deep-dive — dedicated LangGraph resources on TokRepo
Common pitfalls
- Creating infinite loops without a max-iteration guard causes runaway token spend. Always add a counter or timeout to conditional edges.
- Storing large objects in graph state slows checkpoint serialization. Keep state lean and reference external storage for big payloads.
- Mixing synchronous blocking calls inside async graph nodes degrades throughput. Use async-native tool implementations.
Frequently Asked Questions
LangChain provides building blocks like LLM wrappers, tool schemas, and prompt templates. LangGraph is the orchestration layer that connects those blocks into stateful agent graphs with loops, branching, and persistence. LangChain recommends LangGraph for non-trivial agent applications.
Yes. State is automatically checkpointed between nodes. LangGraph supports PostgreSQL, SQLite, and Redis as checkpoint backends. If a node fails, the agent resumes from the last checkpoint.
Use interrupt_before or interrupt_after on any node. Execution pauses and returns control to your application. When you provide input (approval, edit, rejection), the graph resumes from that point.
Technically yes. LangGraph depends only on langchain-core, which is lightweight. You can build graph-based workflows without using LangChain's full LLM abstractions, though most users pair them.
When a LANGCHAIN_API_KEY is set, every node execution is traced in LangSmith with token usage, latency, and tool call details. LangSmith offers a free tier for small projects.
Citations (3)
- LangGraph GitHub— LangGraph supports cycles, branching, persistence, and human-in-the-loop
- LangGraph Persistence Docs— Persistence backends include PostgreSQL, SQLite, and Redis
- LangChain Agents Tutorial— LangChain recommends LangGraph for agent applications
Related on TokRepo
Source & Thanks
Discussion
Related Assets
NAPI-RS — Build Node.js Native Addons in Rust
Write high-performance Node.js native modules in Rust with automatic TypeScript type generation and cross-platform prebuilt binaries.
Mamba — Fast Cross-Platform Package Manager
A drop-in conda replacement written in C++ that resolves environments in seconds instead of minutes.
Plasmo — The Browser Extension Framework
Build, test, and publish browser extensions for Chrome, Firefox, and Edge using React or Vue with hot-reload and automatic manifest generation.