GenAI Systems Lab Open interactive version →
Agents & Tool Use 10 min read

LangGraph Reducers and HITL: State Machines for Agentic Workflows

LangGraph models agents as explicit state machines with typed state schemas, reducer functions, and conditional edges — not as implicit loops. Reducer functions control how concurrent node outputs merge into shared state. HITL checkpoint patterns pause execution at defined interrupt points, persist state, and resume only after a human decision. When graph-based orchestration beats a custom loop, and when it doesn't.

Most agent loops are implicit: call the LLM, parse the tool call, execute it, call the LLM again. This works for simple chains. It breaks when you need parallel execution, human approval gates, durable state across restarts, or complex conditional routing. LangGraph makes the agent's control flow explicit — as a typed state machine with nodes, edges, and reducers.

Why implicit loops fail at scale

An implicit loop loses state on failure, has no natural pause point for human review, can't run nodes in parallel without custom threading code, and gives you no visibility into what the agent decided between steps. These are not implementation details — they are architectural constraints. LangGraph's state machine model directly addresses each one.

Common interview trap: candidates describe LangGraph as 'LangChain with a graph.' It is a different programming model. The core abstraction is a typed StateGraph where every node receives the full state and returns a partial state update — not a chain of sequential transformations.

The state schema and reducer functions

Every LangGraph agent begins with a typed state schema — a TypedDict (Python) or equivalent that defines every field the graph can read or write. When a node returns a partial update, LangGraph must decide how to merge that update into the existing state. That merge logic is the reducer.

The HITL interrupt pattern

Human-in-the-loop in LangGraph is not a polling mechanism — it is a first-class interrupt. You declare interrupt_before=["node_name"] or interrupt_after=["node_name"] when compiling the graph. When the graph reaches that node, execution halts, state is persisted via the checkpointer, and the process returns. A human reviews the state. When approved, graph.invoke(Command(resume=...)) continues from the exact interruption point.

The most common HITL bug: the graph uses interrupt_before but no checkpointer is configured. The graph compiles without error and runs correctly until the first interrupt — then state is lost and the resume call raises a KeyError on the thread_id. Checkpointer configuration is not optional when HITL is used.

Conditional edges and routing

After a node runs, LangGraph decides which node to execute next. Static edges always route to the same target. Conditional edges call a routing function that reads the current state and returns the next node name. This is where agent decision-making lives — not in the LLM prompt, but in a deterministic Python function that reads state fields like route_decision, requires_human_review, or error_count.

PatternWhen to useRisk if misused
Static edgeLinear steps with no branchingInflexible — can't handle errors or escalation
Conditional edge on LLM output fieldRoute based on agent's classification decisionLLM non-determinism bleeds into control flow
Conditional edge on hard state fieldRoute based on error_count, status, validated_by_humanSafe — deterministic, testable routing
interrupt_before nodeHuman approval before irreversible actionState lost if checkpointer not configured

When graph-based orchestration beats a custom loop

When a custom loop is simpler

LangGraph adds real overhead: state schema declaration, checkpointer configuration, graph compilation, and a different mental model. For agents with linear execution (call LLM → execute tool → return), a custom loop is faster to write, easier to read, and has fewer moving parts to debug. The graph model pays off when the control flow itself is the complexity — not the individual nodes.

Try it interactively

GenAI Systems Lab is a free platform for AI engineers — configure real failure modes, break things, and build the judgment that gets you hired.

Open GenAI Systems Lab →