Suryanand/ Bootcamp
suryanand.com ↗Let's talk
← FrameworksAI guideIntermediate#frameworks#langgraph#agents#durability
Frame · ai-engineering / frameworks / 03-langgraph-durable-agents.mdGuide 039 / 103

LangGraph, state graphs and durable agents

LangGraph is the low level runtime underneath LangChain agents, and it is what you reach for when the prebuilt agent loop is not enough. You describe your agent as a graph: nodes are functions, edges decide what runs next, and a shared state object flows through it. The part that makes it worth learning is persistence. Every step can be checkpointed, so a run can pause for a human, survive a crash and pick up where it left off. As of October 2026 it is on the 1.2 line in Python, with a JavaScript version on 1.5.

Why it matters

Real agents are rarely a single loop. A support agent might classify the ticket, look things up, draft a reply, wait for a human to approve a refund, then send. A coding agent might plan, then fan out into parallel subtasks, then merge. You can write that as one big prompt and hope, or you can make the control flow explicit.

LangGraph gives you three things that are painful to build yourself:

  • Explicit control flow. Branches, loops and parallel fan out are edges in a graph, so you can read and test them.
  • Checkpoints. After each step the full state is saved to a checkpointer (in memory, SQLite, Postgres and others). A thread_id identifies a run, so you can resume it later or inspect its history.
  • Interrupts. A node can call interrupt() to pause the graph and hand a value to your application. When a human answers, you resume with Command(resume=...) and the value comes back as the return value of interrupt().

Together these are what people mean by a "durable agent": one whose progress is stored outside the process, so it can wait hours for an approval or restart after a deploy.

How it works

You define a state schema, add nodes and edges to a StateGraph, then compile() it. The compiled graph runs in steps. In each step the active nodes read the state and return partial updates, which are merged using reducers (for example operator.add to append messages).

Rendering diagram…

Every arrow in that diagram is a potential checkpoint. If the process dies while waiting at Approval, nothing is lost: the state is in the checkpointer, keyed by thread.

A few rules from the interrupts docs that trip people up:

  • You need a checkpointer to use interrupts. Use a durable one (such as Postgres) in production.
  • Resume with the same thread_id that was used when the interrupt happened.
  • When resumed, the interrupted node restarts from the beginning, so code before interrupt() runs again. Make it idempotent.

Reference architecture

In production the graph usually runs behind an API, with the checkpointer in a real database and a separate path for humans to answer interrupts.

Rendering diagram…

You can host this yourself with any web framework, or use LangChain's managed deployment platform, which runs graphs as an Agent Server with task queues and persistence built in.

Code

A small refund workflow with a human approval step. This uses the Graph API names from the current docs: StateGraph, START, END, interrupt, Command and a checkpointer.

from typing import TypedDict

from langgraph.checkpoint.memory import InMemorySaver
from langgraph.graph import END, START, StateGraph
from langgraph.types import Command, interrupt


class RefundState(TypedDict):
    order_id: str
    amount: float
    approved: bool
    result: str


def check_policy(state: RefundState) -> dict:
    # Pure logic, safe to rerun
    return {"approved": state["amount"] < 50}


def human_review(state: RefundState) -> dict:
    if state["approved"]:
        return {}
    decision = interrupt({"question": "Approve refund?", "order": state["order_id"]})
    return {"approved": bool(decision)}


def route(state: RefundState) -> str:
    return "issue_refund" if state["approved"] else "deny"


def issue_refund(state: RefundState) -> dict:
    return {"result": f"Refunded {state['amount']} for {state['order_id']}"}


def deny(state: RefundState) -> dict:
    return {"result": "Refund denied"}


builder = StateGraph(RefundState)
builder.add_node("check_policy", check_policy)
builder.add_node("human_review", human_review)
builder.add_node("issue_refund", issue_refund)
builder.add_node("deny", deny)
builder.add_edge(START, "check_policy")
builder.add_edge("check_policy", "human_review")
builder.add_conditional_edges("human_review", route, ["issue_refund", "deny"])
builder.add_edge("issue_refund", END)
builder.add_edge("deny", END)

graph = builder.compile(checkpointer=InMemorySaver())

config = {"configurable": {"thread_id": "refund-981"}}
first = graph.invoke({"order_id": "981", "amount": 120.0}, config)
print(first.get("__interrupt__"))   # paused, waiting for a human

final = graph.invoke(Command(resume=True), config)
print(final["result"])

For production, swap InMemorySaver for the Postgres checkpointer from the langgraph-checkpoint-postgres package so the paused thread survives restarts.

LangGraph also has a Functional API (@entrypoint and @task decorators) if you prefer writing ordinary Python control flow and still want checkpoints. The Graph API is better when you want the flow to be visible and testable as a structure.

Trade-offs and pitfalls

  • More code than a prebuilt agent. If create_agent with middleware covers your case, use it. It is a LangGraph graph already.
  • State design is the real work. Keep state small and serializable. Do not put clients, file handles or huge blobs in it; store references.
  • Side effects and replays. Because nodes can rerun on resume, put irreversible actions (charging a card, sending an email) in their own node after approval, and make them idempotent with an operation key.
  • Checkpoint growth. Long threads produce many checkpoints. Plan retention, and summarize message history.
  • Debugging is different. You are debugging a state machine. Use the checkpoint history (get_state_history) and tracing to see which step produced which state.

When to pick it: long running or multi step agents, human approval, branching or parallel work, anything that must survive restarts, or multi agent setups where you want explicit handoffs.

When not to: a single chat loop with a few tools, short lived request and response flows, or teams that already run a workflow engine such as Temporal and would rather put the LLM calls inside it.

Checklist

  • Draw the graph on paper before writing nodes.
  • Keep state as a small TypedDict or Pydantic model with clear reducers.
  • Use a durable checkpointer (Postgres) outside local development.
  • Use one thread_id per conversation or job, and store it.
  • Make every node safe to rerun, especially those that call interrupt().
  • Isolate irreversible actions in their own nodes after approval.
  • Trace runs and inspect checkpoint history when debugging.

Further reading

Why this exists README

I learn best by explaining things, so I record the explanations. Everything here is free, with no sign-up. If a lesson helps you ship something, I'd love to hear about it.