LangGraph Explained: Building Stateful AI Agents in Python

LangGraph builds stateful AI agents in Python as graphs: nodes do the work, edges route, and a checkpointer saves every step. How the pieces fit together.

By Galactic News Editorial · Published 2026-11-19T10:37:36+00:00 · Updated 2026-11-19T10:37:36+00:00

LangGraph explained: building stateful AI agents in Python

LangGraph Explained: Building Stateful AI Agents in Python

LangGraph is a Python library for building AI agents as stateful graphs. It comes from the LangChain team. It adds cycles, persistent state, and conditional routing to agent workflows. If a plain chain cannot loop, remember, or pause for a human, LangGraph is the tool that fixes that. It reached version 1.0 on October 22, 2025.

The core idea is a state machine. Your agent is a graph. Nodes do work. Edges decide what runs next. A shared state object flows through every node. A checkpointer saves the state after each step, so the agent can pause, resume, and survive a crash.

LangGraph Explained: Why Chains Are Not Enough

LangChain's expression language, LCEL, builds linear pipelines. Input flows through a sequence of steps and produces output. That works for summarisation and single-turn questions. It breaks for agents.

Agents need loops. A reasoning agent calls a tool, reads the result, and decides what to do next. It may call the tool again. It may ask the model to retry. A linear chain cannot loop back. LangGraph models the work as a directed graph instead. Graphs can have cycles. Chains cannot.

Agents also need memory. A chain forgets everything when it finishes. LangGraph keeps a shared state object alive across the whole run. Every node reads from it and writes updates back to it. With a checkpointer, that state persists across runs too. The agent can remember earlier turns and pick up where it left off.

The Building Blocks: State, Nodes, Edges

LangGraph has five concepts that cover most real projects. State is the shared object every node can see. It is usually a TypedDict or a Pydantic model. Nodes are Python functions that take the state and return updates. Edges are transitions between nodes. Conditional edges pick the next node based on logic, often the model's own decision. The checkpointer saves state after each step.

A minimal agent state looks like this:

from typing import Annotated, TypedDict
from langgraph.graph.message import add_messages
from langchain_core.messages import AnyMessage

class AgentState(TypedDict):
    messages: Annotated[list[AnyMessage], add_messages]

The add_messages reducer is the key detail. It appends new messages onto the existing list instead of replacing it. That is how the conversation history survives across nodes. LangGraph also ships a built-in MessagesState that does the same thing with less code.

Nodes are plain functions. A node that calls a model reads the messages from state, calls the model, and returns the new message. A tool node runs the tools the model requested. The graph wires them together with edges, and a conditional edge decides whether to call tools again or finish.

Building a ReAct Agent in Five Steps

  1. Install the packages. Run pip install langgraph langchain-openai. LangGraph needs Python 3.10 or newer. Add langchain-anthropic or another provider package if you use a different model.
  2. Define the state. Use the AgentState TypedDict above, or the built-in MessagesState. The state holds the message history and any fields your agent needs, such as a task list or a result.
  3. Write the nodes. Create an agent node that calls the model with its tools bound, and a tool node that executes tool calls. LangGraph ships a prebuilt ToolNode that runs tools from the last AI message.
  4. Wire the graph. Create a StateGraph, add the nodes, and add a conditional edge from the agent node. The routing function checks whether the model requested a tool call. If yes, go to the tool node. If no, go to END.
  5. Compile with a checkpointer. Compile the graph with a checkpointer such as InMemorySaver for development or SqliteSaver for persistence on disk. Pass a thread_id in the config. Invoke the graph with a human message and it runs the loop.

The compiled graph runs in super-steps. Each super-step runs the nodes at the current level, merges their updates into state, and follows the edges. The loop continues until it reaches END. The checkpointer saves a snapshot after each super-step.

Checkpoints: Memory, Resuming, and Time Travel

Checkpointing is the feature most teams adopt LangGraph for. A checkpoint is a state snapshot saved after each super-step. It is keyed by a thread_id from the config. Invoke again with the same thread_id and the earlier messages are already in the state. The agent remembers the conversation.

Checkpoints also give fault tolerance. A crashed run resumes from the last completed step instead of starting over. For production, use a database-backed checkpointer. SQLite works for a single process. Postgres works for a service. The in-memory saver is for development only, since it loses state on restart.

LangGraph also supports time travel. Because every step is saved, you can rewind a thread to an earlier checkpoint and replay from there. That is useful for debugging a bad agent run and for testing a fix against the same starting state.

Human-in-the-Loop: Pausing for Approval

Some agent actions need a person's approval: sending an email, issuing a refund, deleting data. LangGraph handles this with interrupts. A node calls interrupt() with the details of the proposed action. The graph pauses and saves its state in the checkpointer. Control returns to your application. Later, your code resumes the thread with Command(resume=...) and the value you pass becomes the return value of interrupt().

Three things are required for interrupts to work. A checkpointer must be set, because the paused state has to be saved. A thread_id must be passed, so the resume call finds the right thread. The value passed to interrupt() must be JSON-serialisable.

One gotcha matters. When the graph resumes, the node restarts from the beginning. All code before interrupt() runs again. Keep the interrupting node free of side effects. Put the side effect in its own node after the approval. Use an idempotency key so a retried action does not execute twice.

There is also a simpler pattern. Compile with interrupt_before=["node_name"] to pause before a named node. Inspect the state, get approval, then invoke again with no input to continue. Both patterns work. The interrupt() call gives finer control over what the human sees.

LangGraph vs Plain LangChain: When to Use Which

LangChain LCEL chainLangGraph
StructureLinear onlyGraphs with cycles and branching
StateNone, statelessShared typed state across all nodes
MemoryManual, per callCheckpoints keyed by thread_id
Human approvalNot built ininterrupt() and interrupt_before
Best forSimple pipelines, single-pass tasksAgents, multi-step workflows, multi-agent systems

Use LCEL chains for document summarisation, single-turn question answering, and prompt formatting. Use LangGraph for anything with loops, branching, multi-turn memory, human approval gates, or several agents working together. LangGraph depends on langchain-core for message types, but you do not need LCEL chains inside your nodes. A node can call any Python code, any API, and any model client directly.

LangGraph also ships prebuilt helpers. The create_agent function builds a standard tool-calling agent in a few lines. Start there for a first agent. Drop down to a hand-built StateGraph when you need custom routing, parallel nodes, or subgraphs.

Common Mistakes to Avoid

State updates must follow the reducer rules. If two nodes update the same key in one step without a reducer, LangGraph raises an error. Use add_messages for message lists and define reducers for any field several nodes write to.

Do not put side effects before an interrupt. The node re-runs on resume, so a notification or a payment sent before the interrupt goes out twice. Keep the interrupting node pure. Do the work after approval.

Do not use the in-memory checkpointer in production. It loses all state when the process restarts. Use SQLite for a single process or Postgres for a service. Match the checkpointer to the deployment.

Do not skip the thread ID. Without a stable thread_id in the config, every invoke starts a fresh conversation. Memory only works when the same thread is reused.

Bottom Line

LangGraph turns an agent from a script into a state machine. State flows through nodes, edges decide the next step, and the checkpointer makes every step durable. Start with pip install langgraph, build a small ReAct agent with StateGraph and ToolNode, and add a checkpointer before you add features. Add interrupts before your agent takes any action that costs money, sends a message, or deletes data. If your workflow is a straight line, a chain is enough. If it loops, remembers, or needs approval, use LangGraph.