Skip to main content
Concepts Agent Build complex, stateful AI workflows with graph-based orchestration. The StateGraph class 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.

Overview

StateGraph can be created with minimal configuration or with extensive customization to suit your specific needs. The graph provides a robust foundation for AI-powered applications with built-in support for various advanced features.

Key Features

  • Explicit Control Flow - Define workflows as visual graphs with nodes and edges
  • 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
  • Multi-Model Support - Works with OpenAI, Anthropic, Google, and others!

Example

Basic Example

When creating a graph without specifying a checkpointer, state is not persisted between executions. Use MemorySaver or SqliteCheckpointer for persistence.

Advanced Example with Tools