Skip to main content

States

States are the data that flows through your graph. They’re defined as TypedDict for type safety.

Basic State Definition

State Reducers

Reducers control how state updates are merged. By default, new values replace old ones:
Common Reducer Patterns:

Example: Message History

Nodes

Nodes are functions that process state. They receive the current state and return updates.

Basic Node

Node Signatures

Nodes can have different signatures:

Node Return Types

Nodes can return:
  1. Dictionary - State updates (merged with reducers)
  2. Command - State updates + routing
  3. Send - Dynamic parallel invocation
  4. List[Send] - Multiple parallel invocations

Edges

Edges define the flow of execution between nodes.

Simple Edges

Direct connections from one node to another:

Conditional Edges

Branch based on state:
All nodes mentioned in targets must be connected to other nodes or END. The graph validator will catch missing connections.

Conditional Routing

Method 1: Conditional Edges

Use add_conditional_edges with a routing function:

Method 2: Command Objects

Nodes return Command to control routing:
Command vs Conditional Edges:
  • Use Command when the node itself decides routing (more explicit)
  • Use conditional_edges when routing logic should be separate from node logic

Loops and Cycles

Create loops by routing back to earlier nodes:
Recursion Limits: Always set a recursion_limit when using loops to prevent infinite execution. Default is 100 steps.

Best Practices

1. Keep Nodes Focused

Each node should have a single responsibility:

2. Use Type Hints

Always type your state and return values:

3. Design State Carefully

Think about what needs to be in state vs what can be computed:

Next Steps