
How reducers introduce discipline
A reducer is a simple function that receives the current state and a node’s output, then returns the updated state. This is the place to enforce rules like:- Only add a field if it doesn’t already exist.
- Append results to a list instead of replacing the list.
- Prune temporary or sensitive values before they persist.


Field-level reducers (custom merging)
By default, LangGraph performs a shallow merge when a node returns a field. For many workflows—chat history, tool logs, or aggregated analytics—you want accumulation instead of replacement. Field-level reducers let you define exactly how a single key is merged. Example: annotate amessages field with a reducer that appends new messages rather than replacing the list.

Example: custom merge for tool results
Below is a concrete reducer example that accumulates tool results instead of overwriting them, plus a sample node that returns thetool_results field.
Benefits and common reducer patterns
Reducers are plain functions—easy to write, test, and reuse. They help with:- Accumulation (lists of messages, logs, search results)
- Idempotence (avoid duplicate entries)
- Cleanup (remove or redact temporary or sensitive fields)
- Centralized business logic (same reducer reused across graphs)
Reuse and modularity
Because reducers are normal functions, you can import them across graphs to keep merging logic centralized. This enforces consistent state behavior across different flows and makes testing straightforward.
Use field-level reducers to centralize accumulation and cleanup logic (e.g., chat history, tool logs, diagnostic traces). This avoids ad-hoc state manipulation inside nodes and prevents accidental overwrites.
Best practices
- Define reducers for any field that represents accumulated history or logs.
- Keep reducers small and focused so they are easy to unit test.
- Use reducers to strip or redact sensitive data before it becomes persistent.
- Reuse reducers across graphs to maintain consistent state behavior.
Summary
State reducers are foundational for predictable state evolution in LangGraph. They let you implement intelligent merging, prevent accidental overwrites, and keep your nodes simple. Use field-level reducers to centralize accumulation, cleanup, and deduplication logic so your graph’s shared state remains reliable and auditable.Links and references
- LangGraph documentation
- Python:
typing_extensionsdocumentation - Pattern references: accumulation, idempotence, and cleanup techniques in stateful systems