Module 7 · Lesson 3 · 16 min read
LangGraph: the agent as a state graph
LangGraph: Agents as State Graphs
Introduction
LangGraph treats the agent as a graph: nodes that read and update a shared state, and edges that define the path. In this lesson we build a small graph that runs without an API key, and understand why an explicit graph makes testing, resumption and human approval easier.
Learning objectives
- Explain the concepts of the state graph, nodes and edges in LangGraph
- Build a simple graph using StateGraph, MessagesState, START and END
- Understand how a node returns an update to the state, not a full state
- Compare the state graph with the open loop with handoff
LangGraph: low-level orchestration
The official documentation describes LangGraph as a low-level orchestration framework for building long-running, stateful agents, and it focuses on four capabilities: durable execution, which allows a run to resume after an interruption, streaming, human-in-the-loop, and persistence and memory. Importantly, it does not require using LangChain, although it integrates with it. It is installed with the command pip install -U langgraph, and the latest version on PyPI is 1.2.22 (according to the official documentation, reviewed 18 November 2026).
The basic idea: instead of the model alone deciding what happens after each step, as in the open loop, you draw a state graph: nodes are functions that read the state and return an update to it, and edges define the transition from one node to another.
The five components you need to start
StateGraph: the graph builder, which takes the state type.MessagesState: a ready-made state containing amessageskey, where new messages are appended to the list instead of replacing it.add_node(name, fn): adds a node. The function receives the state and returns a dictionary with the update.add_edge(a, b): adds an edge. The constantsSTARTandENDrepresent the beginning and end of the graph.compile()theninvoke(...): turning the builder into a runnable graph, then running it on an initial state.
A practical example that runs without an API key
The scenario: a college support desk receives students' messages. We want a node that classifies the message, then a node that writes the reply. To illustrate the structure without any language model, the nodes here are plain Python functions that rely on simple rules:
# langgraph 1.2.22 — reviewed 18 November 2026
# Requires: pip install -U langgraph (this example needs no API key because the nodes do not call a model)
from langgraph.graph import StateGraph, MessagesState, START, END
def classify(state: MessagesState):
text = state["messages"][-1].content
label = "financial" if "fees" in text or "invoice" in text else "academic"
return {"messages": [{"role": "assistant", "content": "Classification: " + label}]}
def respond(state: MessagesState):
label = state["messages"][-1].content
reply = "We will transfer you to the finance office" if "financial" in label else "We will transfer you to the academic advisor"
return {"messages": [{"role": "assistant", "content": reply}]}
builder = StateGraph(MessagesState)
builder.add_node("classify", classify)
builder.add_node("respond", respond)
builder.add_edge(START, "classify")
builder.add_edge("classify", "respond")
builder.add_edge("respond", END)
graph = builder.compile()
result = graph.invoke({"messages": [{"role": "user", "content": "I have a question about the semester fees"}]})
for m in result["messages"]:
print(type(m).__name__, ":", m.content)When it runs, three messages appear: the user's message, then the classification, then the reply. Notice that each node returned an update containing only a new message, and that MessagesState appended it to the list. And notice that the dictionaries we wrote were turned into message objects (such as HumanMessage and AIMessage), so we read the text through .content.
Why is an explicit graph useful?
- Predictability: the path is written in code, so the model cannot skip the verification or approval step.
- Testability: each node is an independent function that can be tested on its own.
- Resumption: because the state is central and explicit, the framework can save it after each step (persistence) and resume the run from the last point, which is the basis of durable execution and the human approval points we study in the autonomous agents module.
- Intentional cycles: you can draw an edge that returns to an earlier node to build a "think, then execute, then review" loop, with a clear exit condition.
A quick comparison with the loop and handoff
| Question | Loop with handoff (Agents SDK) | State graph (LangGraph) |
|---|---|---|
| Who determines the next step? | Usually the model | The edges you draw, with the model's decisions inside the nodes |
| Shape of the state | The conversation history | A defined state shared by the nodes |
| Best use | Conversations with several specialists | Long, controlled and resumable workflows |
Summary
LangGraph is a low-level orchestration framework for long-running agents that focuses on durable execution, streaming, human-in-the-loop and persistence, and it does not require LangChain. You build the graph with StateGraph, add nodes and edges between START and END, then call compile and invoke. Nodes return updates to the state, and MessagesState appends new messages to the list. An explicit graph makes the path predictable, testable and resumable.
Terms
| Term | English | Definition |
|---|---|---|
| Node | Node | A function that reads the state and returns an update to it |
| Edge | Edge | A defined transition from one node to another |
| Messages state | MessagesState | A ready-made state with a messages key to which new messages are appended |
| Durable execution | Durable Execution | The ability to resume a run after an interruption from the last saved state |
| Compile | compile | Turning the builder into a runnable graph |
Review questions
- What is the role of START and END in a LangGraph graph?
- Why does a node return an update dictionary instead of the full state?
- How does an explicit graph help put in place a human approval point that cannot be skipped?
- What is the danger in drawing a cycle without an exit condition?
References
- LangGraph overview docs.langchain.com Reviewed on 2026-11-18
- langgraph on PyPI pypi.org Reviewed on 2026-11-18
