LangGraph — Build Stateful AI Agent Workflows
Framework for building stateful, multi-step AI agent workflows as graphs. LangGraph enables cycles, branching, human-in-the-loop, and persistent state for complex agent systems.
先审查再安装
这个资产需要先审查。复制的指令会要求 Agent dry-run、列出写入项,确认后再继续。
npx -y tokrepo@latest install b985ba42-4ba9-452d-93f1-9efa434df107 --target codex先 dry-run,确认写入项后再运行此命令。
What it is
LangGraph is a framework for building stateful AI agent workflows as directed graphs. Each node in the graph represents a step (LLM call, tool invocation, decision point), and edges define the flow between steps. Unlike linear chain abstractions, LangGraph supports cycles, conditional branching, and parallel execution.
LangGraph targets developers building production AI agents that need reliability features like state persistence, human approval gates, and automatic retries. It is part of the LangChain ecosystem.
How it saves time or tokens
Building a multi-step agent from scratch requires implementing state management, retry logic, and conversation persistence manually. LangGraph provides these as built-in primitives. State is automatically checkpointed between nodes, so agents can resume after failures without restarting.
The estimated token cost for this workflow is approximately 3,900 tokens. LangGraph's graph structure makes it easy to add or remove steps without rewriting the entire agent.
How to use
- Install LangGraph:
pip install langgraph
- Define a state schema and build a graph:
from langgraph.graph import StateGraph, START, END
from typing import TypedDict
class State(TypedDict):
messages: list
next_step: str
def research(state: State):
# Call LLM or tool
return {'messages': state['messages'] + ['research done']}
def draft(state: State):
return {'messages': state['messages'] + ['draft done']}
graph = StateGraph(State)
graph.add_node('research', research)
graph.add_node('draft', draft)
graph.add_edge(START, 'research')
graph.add_edge('research', 'draft')
graph.add_edge('draft', END)
app = graph.compile()
- Run the graph:
result = app.invoke({'messages': [], 'next_step': ''})
Example
# Conditional branching example
from langgraph.graph import StateGraph, START, END
def router(state):
if len(state['messages']) > 5:
return 'summarize'
return 'continue'
graph = StateGraph(State)
graph.add_node('process', process_fn)
graph.add_node('summarize', summarize_fn)
graph.add_conditional_edges('process', router, {
'continue': 'process',
'summarize': 'summarize'
})
graph.add_edge('summarize', END)
graph.add_edge(START, 'process')
app = graph.compile()
Related on TokRepo
- Multi-Agent Frameworks -- Deep-dive into LangGraph agent patterns
- AI Tools for Agents -- Compare agent frameworks and orchestration tools
Common pitfalls
- LangGraph graphs must have at least one path from START to END. Infinite loops without exit conditions will run until timeout or token limits.
- State must be serializable for persistence to work. Avoid putting non-serializable objects (database connections, file handles) in the state dict.
- Conditional edges require returning string keys that exactly match the routing map. Typos in routing keys cause silent failures.
常见问题
TokRepo hosts multiple community-contributed workflows for the same tool. This entry focuses on multi-step workflow patterns, while other entries may cover different aspects like basic agent setup or specific integration patterns.
LangGraph depends on langchain-core, which is lightweight. You do not need the full LangChain library. However, most users combine LangGraph with LangChain's LLM wrappers and tool abstractions.
LangGraph supports PostgreSQL, SQLite, and Redis for state checkpointing. In-memory persistence is available for development. Production deployments typically use PostgreSQL.
Yes. LangGraph supports parallel node execution via fan-out patterns. Multiple nodes can execute simultaneously, and a merge node collects their results before continuing.
Use interrupt_before or interrupt_after on any node to pause execution. The agent waits for external input (approval, edits, rejection) before continuing. This is how you build approval gates in production agents.
引用来源 (3)
- LangGraph GitHub Repository— LangGraph builds stateful agents as directed graphs
- LangGraph Persistence Docs— Supports PostgreSQL, SQLite, and Redis persistence
- LangChain Documentation— Part of the LangChain ecosystem for AI agent development
来源与感谢
langchain-ai/langgraph — 8k+ stars, MIT
讨论
相关资产
LangGraph — Build Stateful AI Agents as Graphs
LangChain framework for building resilient, stateful AI agents as graphs. Supports cycles, branching, persistence, human-in-the-loop, and streaming. 28K+ stars.
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.
Julep — Stateful AI Agent Platform with Workflows
Build persistent AI agents with multi-step workflows, long-term memory, and tool integrations. Julep handles state management so agents remember across sessions.
DeepAgents — Multi-Step Agent Framework by LangChain
Agent harness built on LangGraph by the LangChain team. Features planning tools, filesystem backend, and sub-agent spawning for complex multi-step tasks like codebase refactoring. 16,500+ stars.