If you’ve been exploring LangGraph articles series lately, you’ve probably noticed that it’s more than just a fancy way to wire up agents and tools. It’s a framework built for control, helping you manage how data, messages, and states flow across your AI system.
But as your graphs grow, so does the complexity. You might find yourself wondering:
“How do I combine multiple outputs or keep track of what’s happening across different nodes?”
That’s where Reducers come in.
Before we dive in, here’s something you’ll love:
Learn LangChain the clear, concise, and practical way.
Whether you’re just starting out or already building, Langcasts gives you guides, tips, hands-on walkthroughs, and in-depth classes to help you master every piece of the AI puzzle. No fluff, just actionable learning to get you building smarter and faster. Start your AI journey today at Langcasts.com.
Reducers are the quiet organizers in your LangGraph workflows. They help you take multiple pieces of information, from agents, nodes, or even message streams, and neatly combine them into a single, meaningful result.
In this beginner’s guide, we’ll break down what Reducers are, why they matter, and how to start using them in your LangGraph projects. No complex math, no jargon, just clear explanations and simple examples you can try out right away.
Understanding Reducers: The Core Idea
Before we jump into code, let’s first get the idea right.
A Reducer in LangGraph is a function that takes in multiple inputs, like messages, states, or results from different parts of your graph, and reduces them into a single, unified output.
If that sounds abstract, think of it like this:
“You have three agents each giving different answers to the same question. Instead of picking one randomly, you can use a Reducer to combine their responses into a single, well-rounded answer.”
In programming terms, Reducers are similar to the reduce() function you may have seen in JavaScript or Python. They take a list of things and boil them down into one. In LangGraph, this means they can:
- Merge messages from multiple nodes.
- Summarize outputs from different agents.
- Keep your graph’s state clean and consistent.
Why does this matter? Because as your workflow grows, you’ll often have multiple branches running at once — and eventually, you’ll need a way to bring everything back together. Reducers make that process simple and predictable.
So, in short:
A Reducer helps your LangGraph workflow make sense of many outputs by turning them into one clear, useful result.
Up next, let’s see where Reducers fit inside a LangGraph workflow.
Where Reducers Fit in LangGraph
So, where exactly do Reducers come into play?
Think of your LangGraph workflow as a map, data flows from one node to another, sometimes branching into different paths. But at some point, those paths need to come back together. That’s where a Reducer steps in.
Reducers usually sit after multiple nodes to collect and merge their outputs into one result. You might use one:
- After several agents respond to the same query.
- To combine outputs from different tools or branches.
- To keep track of the evolving state in looping workflows.
Visually, it’s like this:
Inputs → Node A / Node B / Node C → Reducer → Final Output
In short, Reducers are the “glue” that helps your graph stay organized when things start branching out.
Writing Your First Reducer
Let’s make this real with a quick example.
Let's say you have a LangGraph where two nodes (or agents) each return a short response. You want to combine both into one final message. That’s the perfect job for a Reducer.
Here’s what that might look like:
from langgraph.graph import Graph, Reducer
# Step 1: Define your reducer function
def combine_responses(responses):
# 'responses' is a list of outputs from previous nodes
return " | ".join(responses)
# Step 2: Add it to your graph
graph = Graph()
graph.add_node("agent1", lambda: "Hello from agent 1")
graph.add_node("agent2", lambda: "Hi from agent 2")
# Step 3: Use the reducer to merge their outputs
graph.add_reducer(Reducer(["agent1", "agent2"], combine_responses))
# Step 4: Run the graph
result = graph.run()
print(result)
What’s happening here:
- Each agent (or node) produces a response.
- The Reducer collects those responses as a list (
["Hello from agent 1", "Hi from agent 2"]). - Then it merges them using our
combine_responsesfunction.
Output:
Hello from agent 1 | Hi from agent 2
Simple, right? You can replace that logic with anything, summarizing text, averaging numbers, or combining structured data. The key is that your Reducer takes many inputs and produces one clean output.
Tips & Best Practices
Here are a few quick pointers to keep your Reducers clean and effective:
Keep them simple.
A Reducer should do one thing — combine inputs clearly. Don’t overload it with logic.Use clear names.
Functions likemerge_answers()orsummarize_history()make your graphs easier to read and maintain.Test Reducers independently.
Run them outside the graph first to be sure they handle lists, empty inputs, or unexpected data gracefully.Avoid hidden state.
Let your Reducer depend only on what it receives. This keeps your workflow predictable and easier to debug.Think of the “why.”
Only add a Reducer if it genuinely helps simplify or unify your outputs not just because you can.
Reducers might seem like a small piece of the LangGraph puzzle, but they play a big role in keeping your workflows clean, organized, and efficient. They help your graphs “think clearly”, by merging scattered outputs into one cohesive result.
Once you get the hang of Reducers, you’ll find it easier to manage complexity, especially as your projects grow beyond simple, single-path flows.
So go ahead, try building one, experiment with how it combines data, and see how it transforms your LangGraph workflows.
Small concept. Big impact.
Top comments (0)