1:1 mentoring with Big Tech AI engineers
LLM & Agentic

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

Core9 min readFirst readThe Agent Loop

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
34

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.

Key idea

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.

A graph run is a series of super-steps: read state, return an update, merge, pick the next node
ONE SUPER-STEP, FROM STATE TO STATE state messages: 1 item status: "new" node: chat reads state, calls the model return {"messages": [ai]} reducer add_messages appends the reply state messages: 2 items status: "new" 1 2 3 checkpoint 4 edges pick what runs next next node or END WHAT THE NODE RETURNED {"messages": [AIMessage("Clip papers, …")]} only the keys it changed, never the whole state 1 the node reads the state · 2 it returns an update · 3 the reducer merges it · 4 an edge picks the next node The gap between two super-steps is where the runtime can save, pause, inspect or stream the run.

Related

More in LLM & Agentic

Get full access to all 74+ sections with code examples, diagrams, and interactive animations.

Unlock Premium