LangGraph - State, Nodes, Edges, Checkpoints, Interrupts
Problem with the while-loop agent
The Problem In a bare while-loop agent, state lives in local variables and control flow lives in if / while / break.
- Data sits in RAM. Machine restarts -> whole agent history lost.
- Variables get overwritten again and again. No transparency between steps.
- Cannot interrupt between steps, e.g. cannot decide whether a tool call should run.
- No human in the loop, no going back in time.
The Solution Keep one clearly defined state that stores every turn, message and tool call. -> LangGraph. It is a framework (opinionated about what it adds or drops) around the same agent loop.
Graph view of the agent: nodes and edges
- Node -> a piece of work (LLM call, tool execution, get weather, any function).
- Edge -> a route connecting nodes.
- Fixed edge - if A is done, move to B.
- Conditional edge - a condition decides the next node. This brings back the if/else.
- Example: if the model made a tool call, go to the tool node. If not, running a tool the model never asked for makes no sense.
The model never runs a tool. It only says which tool and which arguments (e.g. get_score with team names and date). The tool node executes it and feeds the result back.
- START -> entry point of the graph. END -> where the final answer returns to the user. Every graph starts at START and ends at END.
- Fan out - one node can branch to several nodes that run in parallel.
- You still need a workflow idea first: the nodes and edges are defined by you, though outputs are not known in advance.
State: the whiteboard
Three primitives: State, Nodes, Edges. State is a whiteboard in a room. Every node writes what it did. Anyone can read it. No node erases what another wrote.
- State - shared structured snapshot. In code, a dictionary.
- Node - function that does work and returns a state update, not the full state.
- Edge - decides which node runs after which.
- The graph merges the partial updates.
Reducers When a node returns something, there are two choices: overwrite the old value or append to it.
- Default behaviour -> overwrite.
- Append reducer for lists, so new items are added. Can also append by ID.
- MessagesState -> prebuilt state with one messages key. Common starting point.
Building a graph in code
from langgraph.graph import StateGraph, MessagesState, START, END
def echo(state):
return {"messages": [("ai", "hello world")]} # update only
g = StateGraph(MessagesState)
g.add_node("echo", echo)
g.add_edge(START, "echo")
g.add_edge("echo", END)
app = g.compile()
app.invoke({"messages": [("user", "hi")]})Compile - final step after all nodes and edges are decided. It checks the structure, attaches the thread ID.">checkpointer, and gives a runnable graph. The graph can also be visualised.
Tool-calling agent
- Define a tool with a decorator on a function (e.g. get_weather(city)).
- bind_tools -> attaches the available tools to the LLM so it knows them.
- Agent node: returns llm_with_tools.invoke(state messages).
- Router: look at the last message. Tool calls present -> go to tools. Otherwise -> END.
- Tools node: prebuilt ToolNode, executes the requested tools.
- Edges: START -> agent, agent -> (conditional) tools or END, tools -> agent.
def route(state):
last = state["messages"][-1]
return "tools" if last.tool_calls else ENDExample: 'weather in Paris and Tokyo?' -> two get_weather calls -> results (18 C clear, 24 C raining) -> final answer. The whole run is visible in the state.
Checkpointing and persistence
Checkpointer -> after every step, take a photo of the whiteboard. A thread ID labels which run the photos belong to, so their order is known.
- Pass the checkpointer in compile (e.g. in-memory saver); then invoke with a config holding the thread ID.
- Storage options: in memory, SQLite (local), Postgres.
- Snapshots can be taken at every step or only around chosen nodes. Useful for debugging: state before vs after a node.
| Unlocked | How |
|---|---|
| Memory | Read saved state of the thread |
| Human in the loop | Pause at a checkpoint, wait for a reply |
| Time travel | List checkpoints and go back to one |
| Fault tolerance | Agent stuck or crashed -> resume from last checkpoint |
Checkpointer vs Store
- Checkpointer -> state within one thread.
- Store -> memory shared across threads.
Interrupt and human in the loop
Interrupt -> a function that pauses the graph and asks a human (yes / no, approve / reject). It needs a checkpointer, because state must be saved while waiting. Time does not matter: the snapshot waits.
What happens on resume LangGraph does not continue from the interrupt line. It stores the human's answer and re-runs the node from the start. This time interrupt returns the stored answer and execution goes on.
- Reason: a Python function cannot be resumed mid-way. It can only be re-run.
- Side effect: everything before the interrupt in that node runs again.
- Example: an email sent before the interrupt is sent twice.
Rules: no important side effects before interrupt (or make them idempotent), keep interrupt calls in the same order each run, keep values JSON-serialisable. A flag stored in state can skip already-done work.
- Command -> LangGraph object that carries the human's reply (e.g. yes) and can update state or route.
- Static interrupts: interrupt before and after a node to compare state, for debugging.
- Dynamic interrupt: the real human-in-the-loop call inside a node.
Time travel
- get_state_history(thread config) -> lists all checkpoints of a thread.
- Replay - go back to a checkpoint and run again from there.
- Fork - branch from a checkpoint and take a different path.
- Think of checkpoints as git commits.
Subgraphs and multi-agent
A node is just a function, so it can be a full agent with its own graph. A supervisor routes to worker agents (researcher, engineer, weather, math) and ends when the work is done.
When to use LangGraph
- Free with state: pausing, resuming, inspecting, forking (git-like).
- Linear flow or one-shot prompting -> use a simpler tool or write it yourself.
- Conditional / non-linear control flow -> graph. The graph is the program, control flow is visible.
- The library is free for production. Paid: hosted agent server (handles checkpointing and storage), Studio UI to visualise nodes.
- Two APIs: Graph API and Functional API.
- Practice: study Open SWE (open-source, LangGraph based) and build your own variant; then explore Deep Agents.
Glossary
- Node
- A unit of work in a LangGraph graph, such as an LLM call or a tool execution.
- Edge
- A route connecting nodes; fixed or conditional.
- State
- Shared dictionary (whiteboard) that all nodes read and update.
- Reducer
- Rule for merging a node's update into state: overwrite or append.
- Compile
- Final step that validates the graph and makes it runnable.
- Checkpointer
- Saves a snapshot of state after each step, keyed by thread ID.
- Thread ID
- Label identifying the run whose snapshots are stored.
- Interrupt
- Pauses the graph to wait for a human response.
- Command
- Object carrying the human's reply to update state or route.
- Fork
- Branching a new run from an earlier checkpoint.