Why a Graph? State, Nodes & Edges
Build a LangGraph agent from scratch: StateGraph, TypedDict state, the add_messages reducer, and the decision table for when a graph beats a plain while-loop.
Last updated
After this section you can
- Decide between a plain loop, create_agent, a custom StateGraph and the Claude Agent SDK for a given agent
- Build and run a StateGraph with state, nodes, normal and conditional edges
- Choose a reducer per field so parallel and repeated writes merge the way you intend
Why a Graph? State, Nodes & Edges
LangGraph turns the agent loop into a graph you declare: a state schema, nodes that return updates to it, and edges that decide what runs next. This section builds the smallest one and explains the one rule everything later depends on.
A node never mutates state. It returns the keys it changed, a reducer merges them, and an edge picks the next node. Because every merge happens at a known point between two steps, the runtime can save, pause or stream the run there.