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

Core4 min readFirst readThe Agent Loop

After this section you can

  • Decide between a while-loop, LangGraph and the Claude Agent SDK for a given run
  • Build and run a StateGraph from scratch
  • Choose a reducer so parallel writes merge instead of clobbering
01

Why a Graph? State, Nodes & Edges

A LangGraph app is three things: a state shape, functions that return updates to it, and edges that decide what runs next. Persistence, approval gates and streaming are then one flag each.

THE CENTRAL IDEA

A node never mutates state — it returns an update, and a reducer decides how that update merges. Because every merge happens at a known point between two nodes, the runtime has somewhere to write a checkpoint. That one rule is what buys you resume-after-crash, human approval and time travel later.

The graph is not a nicer while-loop — it is a loop the runtime can stop inside
WHILE-LOOP · STATE IN A LOCAL VARIABLE messages = [user_turn] while True: reply = model(messages) messages.append(reply) PROCESS DIES ON STEP 9 nothing was written down — restart from step 1 GRAPH · STATE CHECKPOINTED AFTER EVERY NODE agent tools agent ckpt 1 ckpt 2 ckpt 3 PROCESS DIES ON STEP 9 resume the same thread — it restarts at ckpt 8 A node returns an update; the runtime merges it, checkpoints, then picks the next edge. That gap between two nodes is the only place a run can be paused, inspected or resumed — which is why durability and approval gates cost one flag each.

Related

More in LLM & Agentic

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

Unlock Premium