Advanced10 min
State Design Patterns
Advanced state management: reducers, private vs public state, state channels, and handling state in long-running workflows.
Quick Reference
- →Reducers (Annotated[list, add_messages]) control how concurrent updates merge — essential for parallel node execution
- →Use private state fields (prefixed with _) for internal bookkeeping that the LLM should not see
- →State channels let nodes communicate typed data without polluting the main state schema
- →For long-running workflows, checkpoint state to the persistence layer after every node to enable resume-on-failure
- →Use state schema versioning when you need to update state shape without breaking in-flight workflows
Reducers — Merging Concurrent Updates
When multiple nodes update the same state field, you need a reducer to define how updates are merged. Without a reducer, the last write wins — which causes data loss in parallel execution:
Reducers prevent data loss in concurrent updates
| Reducer | Behavior | Use for |
|---|---|---|
| add_messages | Appends new messages, updates existing by ID | Conversation history (messages list) |
| add (operator.add) | Concatenates lists | Accumulating results from parallel nodes |
| None (default) | Last write wins | Single-writer fields (intent, status) |
| Custom function | Any merge logic you define | Counters, aggregations, conflict resolution |