Overview
StateGraph is a powerful graph-based workflow engine that enables you to build complex, stateful AI applications with explicit control flow. Instead of writing monolithic functions or brittle chains, you define workflows as graphs of nodes that can branch, loop, persist state, and recover from failures. Without StateGraph, building reliable AI workflows means dealing with:- Tangled control flow - Complex if/else chains that become unmaintainable
- Lost state - No way to pause, resume, or recover from failures
- No visibility - Black box execution with no insight into what’s happening
- Brittle retry logic - Manual error handling scattered throughout your code
✅ Persistent State - Automatic checkpointing and state management across executions
✅ Time Travel - Access any historical state and fork execution timelines
✅ Human-in-the-Loop - Pause execution for human review and approval
✅ Built-in Reliability - Automatic retries, caching, and durability modes
✅ Dynamic Parallelization - Send API for orchestrator-worker patterns
✅ Recovery - Resume from failures without starting over
When to Use StateGraph
StateGraph is perfect for:- 🤖 AI Agents - Multi-step reasoning with tool calls and decision points
- 📋 Approval Workflows - Content review, moderation, human-in-the-loop processes
- 🔄 Stateful Applications - Chat systems, multi-turn conversations, games
- 🌊 Data Pipelines - Complex ETL with branching logic and error recovery
- 🧪 Research & Experimentation - Iterative processes with state inspection
- 📊 Business Processes - Customer support routing, order processing, onboarding
Core Concepts
States
States are typed dictionaries that flow through your graph. Use TypedDict to define your state schema with full type safety:Nodes
Nodes are functions that process state and return updates. Each node is a unit of work in your workflow:Edges
Edges connect nodes and define the flow of execution. You can have:- Simple edges - Direct connections from one node to another
- Conditional edges - Branch based on state
- Dynamic edges - Send API for parallel worker patterns
The Graph
Combine everything into a compiled graph that orchestrates execution:Key Features
Persistence & Time Travel
Every step is automatically checkpointed, enabling you to:- Resume execution after failures
- Inspect state at any point in history
- Fork execution to explore alternative paths
Human-in-the-Loop
Useinterrupt() to pause execution and wait for human input:
Reliability Features
- Retry policies - Automatic retry with exponential backoff
- Cache policies - Avoid re-executing expensive operations
- Durability modes - Control when state is persisted (sync/async/exit)
Dynamic Parallelization
Use the Send API for orchestrator-worker patterns:Architecture
StateGraph follows a simple but powerful architecture:- Receives the current state
- Performs its work (with optional retry/cache)
- Returns state updates
- Updates are merged using reducers
- State is checkpointed (based on durability mode)
- Next node(s) are determined and executed

