Lesson 2: Stateful Agents with LangGraph
Model agent behavior as a state graph: define nodes, edges, and conditional routing. Build a ReAct agent that loops until done — with full control over state at each step.
By Bishwambhar Sen • Last updated • 85 min
LangGraph solves the problem that LangChain chains cannot: agents that loop, branch, and maintain state across multiple model calls. It models your agent as a directed graph where nodes are functions and edges control the flow.
The ReAct Loop as a Graph
The ReAct pattern (Reason + Act) is the most common agent architecture:
Building the Graph: Step by Step
State is a TypedDict that flows through the graph. Every node reads from and writes to state. Define all fields upfront.
from typing import TypedDict, Annotated
from langgraph.graph.message import add_messages
class AgentState(TypedDict):
# Messages accumulate — add_messages merges rather than replaces
messages: Annotated[list, add_messages]
# Custom state fields
research_results: list[str]
final_answer: str
iterations: intLangGraph vs. AgentExecutor
| Feature | AgentExecutor (legacy) | LangGraph |
|---|---|---|
| Control flow | Black box loop | Explicit graph — full control |
| Custom routing | Limited | Full conditional branching |
| Multi-agent | Not supported | First-class: sub-graphs, handoffs |
| Persistence | No | Checkpointers (memory, Redis, Postgres) |
| Human-in-the-loop | Hacky | Built-in interrupt/resume |
In the next lesson, we explore the different memory strategies available to LangGraph agents: short-term (within-thread), long-term (cross-thread), and episodic memory.