Skip to content
VDAI with VD

Phase 14 · Agent Engineering

Stateful Graph Orchestration — Durable Execution and Checkpoints

Agent is a state machine; nodes are functions; edges are transitions; state is checkpointed after each node. Resume from any failure at the last successful checkpoint. LangGraph is the 2026 reference for this model of low-level stateful orchestration.

  • Learn + Build
  • Python (stdlib)
  • 75 minutes
  • Before this: Phase 14 · 01 (Agent Loop), Phase 14 · 12 (Workflow Patterns)

Learning Objectives

  • Describe LangGraph's core model: state machine with typed state, function nodes, conditional edges, and post-node checkpoints.
  • Name the four capabilities the docs highlight: durable execution, streaming, human-in-the-loop, comprehensive memory.
  • Explain the three orchestration topologies LangGraph supports: supervisor, peer-to-peer (swarm), hierarchical (nested subgraphs).
  • Implement a stdlib state graph with typed state, conditional edges, and a checkpoint/resume cycle.

The Problem

Agents and workflows share a problem: when a 40-step run fails at step 38, you want to resume from step 38, not start over. Second-class state models leave operators hacking retries around a library that assumes fresh runs.

LangGraph's design answer: state is a first-class typed object, mutations are explicit, and checkpoints persist after every node. Resume is a load_state(session_id) call.

The Concept

The graph

A graph is defined by:

  • State type. A typed dict (or Pydantic model) that every node reads and mutates.
  • Nodes. Pure functions (state) -> state_update. Updates are merged into state after return.
  • Edges. Conditional or direct transitions between nodes.
  • Entry and exit. START and END sentinel nodes mark the boundary.

Example: an agent with classify, refund, bug, sales, done nodes — a routing workflow as a graph.

Durable execution

After each node returns, the runtime serializes the state and writes it to a checkpointer (SQLite, Postgres, Redis, custom). On failure at step N, the runtime can resume(session_id) and pick up from step N+1 with exact state.

The LangGraph docs explicitly highlight production users where this matters: Klarna, Uber, J.P. Morgan. The claim isn't the graph shape; it's that the graph shape plus checkpointing makes recovery cheap.

Streaming

Every node can yield partial output. The graph streams per-node-delta events to the caller so UIs update as the graph runs.

Human-in-the-loop

Inspect and modify state between nodes. Implementations: pause before a critical node, surface state to a human, accept modifications, resume. The checkpointer makes this easy because state is already serialized.

Memory

Short-term (within a run — conversation history in state) and long-term (across runs — persistent via the checkpointer plus a separate long-term store). LangGraph integrates with external memory systems (Mem0, custom) via tools.

Three topologies

  1. Supervisor. Central router LLM dispatches to specialist subagents. create_supervisor() in langgraph-supervisor (though the LangChain team in 2026 recommends doing this through tool calls directly for more context control).
  2. Swarm / peer-to-peer. Agents hand off directly via a shared tool surface. No central router.
  3. Hierarchical. Supervisors managing sub-supervisors, implemented as nested subgraphs.

Where this pattern goes wrong

  • Checkpoints too small. Only checkpointing conversation turns leaves tool state and memory writes unrecoverable. Full state must serialize.
  • Non-deterministic nodes. Resume assumes node inputs produce the same state update. Random seeds, wall-clock, external APIs must be captured.
  • Over-use of conditional edges. A graph with every edge conditional is a state machine that cannot be reasoned about. Prefer linear chains with occasional branches.

Interactive figure coming later.

Build It

code/main.py implements a stdlib stateful graph:

  • State — a typed dict with messages, step, route, output, human_approval.
  • Node — callable taking state and returning an update dict.
  • StateGraph — nodes + edges + conditional edges + run + resume.
  • SQLiteCheckpointer (in-memory fake) — serializes state after every node; load(session_id) restores.
  • A demo graph: classify -> branch(refund / bug / sales) -> human gate -> send.

Run it:

python3 code/main.py

The trace shows the first run failing at the human gate, persistence, then resume producing the final output.

Use It

  • LangGraph — the reference, production-ready. Use create_react_agent, create_supervisor, or build your own graph.
  • AutoGen v0.4 (Lesson 14) — actor model alternative for high-concurrency scenarios.
  • Claude Agent SDK (Lesson 17) — managed harness with built-in session store.
  • Custom — when you need exact control over state shape or checkpointer backend.

Ship It

outputs/skill-state-graph.md generates a LangGraph-shaped state graph in any target runtime with checkpointing and resume wired in.

Exercises

  1. Add a conditional edge from classify to end when classification confidence is below a threshold. Resume the run after a human sets route manually.
  2. Swap the SQLite-like fake for a real SQLite checkpointer. Measure per-step serialization overhead.
  3. Implement parallel edges: two nodes run concurrently, merge by a custom reducer. What does immutable state buy here?
  4. Read langgraph-supervisor reference. Port the toy to create_supervisor. Compare the trace shapes.
  5. Add streaming: each node yields partial state while it runs. Print the deltas as they arrive.

Key Terms

TermWhat people sayWhat it actually means
State graph"Agent as state machine"Typed state + nodes + edges + reducers
Checkpointer"Persistence backend"Serializes state after every node; enables resume
Reducer"State merger"Function that combines current state with a node's update
Conditional edge"Branch"Edge chosen by a function of state
Subgraph"Nested graph"A graph used as a node inside another graph
Durable execution"Resume from failure"Restart at the last successful node with exact state
Supervisor"Router LLM"Central dispatcher for specialist subagents
Swarm"P2P agents"Agents hand off via shared tools; no central router

Further Reading

Code

  • main.py

    python phases/14-agent-engineering/13-langgraph-stateful-graphs/code/main.pyOpen on GitHub ↗
  • main.ts

    npx tsx phases/14-agent-engineering/13-langgraph-stateful-graphs/code/main.tsOpen on GitHub ↗

Take-away files

skill-state-graph.md

---
name: state-graph
description: Build a LangGraph-shaped state machine with typed state, conditional edges, per-node checkpointing, and durable resume.
version: 1.0.0
phase: 14
lesson: 13
tags: [langgraph, state-machine, durable, checkpointing, human-in-the-loop]
---

Given a target runtime, a state shape, a set of node functions, and a checkpointer backend, produce a stateful agent graph.

Produce:

1. A typed `State` (dict or Pydantic). Document every field. Nodes read state; they return updates.
2. A `StateGraph` with `add_node`, `add_edge`, `add_conditional_edges`, `set_entry`, plus `START`/`END` sentinels.
3. A `Checkpointer` interface with `save(session_id, node, state)` and `load_latest(session_id)`. Default to SQLite; allow Postgres/Redis/custom.
4. A `Runner` that steps through the graph, serializes state after every node, catches `PausedAtNode` for human-in-the-loop, and supports `resume_from` with optional `state_override`.
5. Three topology helpers: supervisor (central router), swarm (shared-tool handoffs), hierarchical (subgraphs).

Hard rejects:

- Non-deterministic nodes without explicit random-seed or wall-clock capture. Resume assumes node output is reproducible given input state.
- A checkpointer that only saves "summary" state. Serialize the full state or resume breaks.
- Graphs where every edge is conditional. Prefer linear chains with occasional branches.

Refusal rules:

- If the user asks for a state graph without persistence, refuse. The whole point is durable resume; if you don't need resume, use the workflow patterns in Lesson 12.
- If the user asks to "checkpoint only on success," refuse. Failures need state too — that's where debugging starts.
- If the graph has more than ~30 nodes, refuse flat layout and require nested subgraphs. Flat 30-node graphs are unreviewable.

Output: `state.py`, `graph.py`, `checkpointer.py`, `runner.py`, `README.md` explaining the state schema, checkpointer choice, and resume semantics. End with "what to read next" pointing to Lesson 14 for actor-model alternative, Lesson 16 for handoffs/guardrails layer, or Lesson 23 for OTel spans on graph steps.