Skip to content

Lesson 215·M21 AI Agents

LangGraph

Open

Learning Objectives

By the end of this lesson you should be able to:

  • Explain agents as graphs: nodes, edges, shared state (L215)
  • Map the L200 loop onto a graph: perceive, decide, act, observe (L200)
  • Explain the checkpointing: state as the graph's data (L207)
  • Explain the human gates: approval as a node (L208)
  • Explain when LangGraph fits vs the bare loop (L216)

One-Line Definition

LangGraph is agents as graphs — the loop modeled explicitly as nodes (the work), edges (the transitions), and shared state (L207), with structural checkpoints (L207), human gates as nodes (L208), and time-travel debugging — the framework that implements the L200 state-machine view (L215) and makes agents controllable, testable (L341), and resumable (L207).

The one-sentence interview answer: "LangGraph models the agent as a graph (L215). Nodes are the work — perceive, decide, act (L200); edges are the transitions — the model's decisions route the flow (L202); the shared state carries everything — the context, the scratchpad, the step count (L207). The graph is the L200 loop made explicit: the cycle is a cycle of nodes (L215). Three structural wins. Checkpoints — the state is the graph's data, so persistence (L207) is structural, not bolted on (L255). Human gates — an approval is a node: the graph pauses there, the human decides, the edge resumes (L208). Time-travel — you can replay the graph from any checkpoint (L207). When it fits: agents with branching flows and gates (L216). When it doesn't: a straight-line loop — the bare loop is simpler (L199)."

Mental Model

Think of LangGraph as a subway map, and the agent as the train. The map (the graph, L215) shows every station (node — the work) and every line (edge — the transitions). The train (the agent) travels the map: it leaves perceive station, the controller (the model, L202) decides the next line, the train goes to act station. The map makes the trip explicit — you can see where the train is (the state, L207), re-run a segment (checkpoints, L207), and install a gate at a station (the human approval node, L208). A while-loop is a train with no map — you know it's running, not where it is (L200).

   the subway map (the graph, L215)
   ┌──────────────────────────────────────────────┐
   │  perceive → decide → act → observe           │
   │      ▲                           │           │
   │      └─────────── cycle ─────────┘           │
   │  nodes = the work (L200)                     │
   │  edges = the transitions (L202)              │
   │  [HUMAN GATE] — a station with a guard (L208)│
   │  state = where the train is (L207)           │
   └──────────────────────────────────────────────┘

The mental model is the map and the train: the graph is the explicit map of the loop — checkpoints, gates, and state visible as structure (L215).

Visual Flow — The Loop as a Graph

   ┌──────────────────────────────────────────────────────────┐
   │  START ──► [PERCEIVE] (L200)                             │
   │               │  reads the shared state (L206)           │
   │               ▼                                          │
   │            [DECIDE] (L202)                               │
   │               │  the model routes: tool or answer        │
   │               ├──► [HUMAN GATE]? (L208)                  │
   │               │       approval is a NODE — pause,        │
   │               │       decide, resume (L208)              │
   │               ▼                                          │
   │            [ACT] (L201) → [OBSERVE] (L164)               │
   │               │  the result joins the state (L207)       │
   │               ▼                                          │
   │            [CHECKPOINT] — the state, written (L255)      │
   │               │                                          │
   │               └─────► back to [PERCEIVE] (the cycle)     │
   │   or ──► [ANSWER] (L205) — the terminal node             │
   └──────────────────────────────────────────────────────────┘

The flow is the graph: nodes for the work, edges for the routing, a node for the human gate, and the state checkpointed at the cycle's end — the L200 loop, made structural (L215).

How It Works — The Nodes, the State, the Wins

  • The graph (L215). Nodes are the work — perceive, decide, act, observe (L200). Edges are the transitions — the model's decisions route the flow (L202), and the graph's shape is the loop's shape (L215).
  • The shared state (L207). Everything the run holds — the context (L206), the scratchpad (L202), the step count (L205) — is the graph's data, passed between nodes (L207). The state is explicit, which is what makes it checkpointable (L255) and resumable (L207).
  • The checkpoints (L207). The state is the graph's data — checkpointing is structural: each node boundary can save (L255). Persistence isn't bolted on; it's the graph's nature (L215).
  • The human gates (L208). An approval is a node: the graph pauses there, the human decides, the edge resumes (L208). The gate is part of the map, not an interruption (L215).
  • Time-travel (L207). Because the state is checkpointed (L255), the graph can replay from any point — debugging by re-running (L213).

Note

The graph is the loop made explicit — and explicit is controllable (L215). A while-loop's shape is implicit (L200): where does it pause for a human (L208)? Where does the state get saved (L207)? What are the allowed transitions (L202)? The graph answers all three structurally: the nodes are the work, the edges are the transitions, the state is the data, and the gates are nodes (L215). That's why the state-machine view (L207) and the graph framework (L215) are the same idea — the senior design models the loop's shape before its code (L216).

Real Project Usage

  • Branching agents (L215). Research agents with multiple tool paths — the graph's edges route by the findings (L202).
  • Approval workflows (L208). Finance agents — the transfer node is gated by a human node (L208); the graph pauses and resumes (L215).
  • Long-running automation (L207). The state is checkpointed per node (L255) — a crash resumes mid-graph (L207).
  • Multi-agent systems (L210). A coordinator graph with specialist sub-graphs (L210) — the firm as a map (L215).
  • Anything branching (L216). Where the flow forks and gates — the graph is the right model (L199); where it's a straight line, the bare loop is simpler (L215).

The through-line: the graph is the controllable loop — nodes, edges, state, and gates explicit, so the agent is testable (L341) and resumable (L207).

Interview Explanation

Say it in four moves:

  1. The model. "Agents as graphs: nodes are the work, edges are the transitions, the shared state carries the run (L215)."
  2. The mapping. "The L200 loop is a cycle of nodes — perceive, decide, act, observe (L200); the model's decisions route the edges (L202)."
  3. The wins. "Checkpoints are structural (L207), human gates are nodes (L208), and time-travel re-runs from any point (L213)."
  4. The fit. "Branching flows and gates → the graph (L215); a straight-line loop → the bare loop is simpler (L199)."

Senior-Level Insights

  • The graph is the loop's shape made explicit (L215). The senior answer draws the map before the code (L216) — nodes, edges, state — because the shape is the architecture (L200).
  • The state is the graph's data (L207). Context (L206), scratchpad (L202), step count (L205) — explicit state is what makes checkpoints (L255), resume (L207), and testing (L341) structural (L215).
  • The human gate as a node is the design win (L208). The approval is on the map (L208) — visible, testable, and resumable (L215), not an interruption bolted onto a loop (L200).
  • Time-travel is the debugging superpower (L213). Checkpointed state (L255) means replaying a run from any node (L207) — the failure diagnosis (L211) becomes re-runnable (L213).
  • The graph composes with the frameworks' story (L216). LangGraph is the L214 family's stateful layer (L214) — the L155 interface for the glue, the graph for the control (L216).

Common Mistakes

  • A graph for a straight line (L199). The linear loop modeled as a graph — the framework's structure for a loop's simplicity (L215).
  • The loop as a while-loop (L200). The implicit shape — gates (L208) and checkpoints (L207) bolted on instead of structural (L215).
  • The state implicit (L207). The context and step count in scattered variables — checkpointing (L255) and resume (L207) become rewrites (L215).
  • Gates as interruptions (L208). The human approval interrupting the loop (L200) instead of a node on the map (L215).
  • No checkpoints (L207). The graph without the state saving — the crash still restarts (L211).
  • The framework by fashion (L214). The graph used because it's popular (L362), not because the flow branches (L199).

Best Practices

  • Draw the graph first (L216) — nodes, edges, state, gates (L215).
  • Map the L200 loop onto it (L200) — perceive, decide, act, observe as nodes (L215).
  • Make the state explicit (L207) — the graph's data is the run (L206).
  • Checkpoint at node boundaries (L255) — persistence is structural (L207).
  • Model the gates as nodes (L208) — approvals on the map (L215).
  • Choose by shape (L199) — branching → graph; straight line → bare loop (L216).

Interview Questions

Q: What is LangGraph?

A: Agents as graphs (L215). Nodes are the work — perceive, decide, act (L200). Edges are the transitions — the model's decisions route the flow (L202). The shared state carries the run — context, scratchpad, step count (L207). The L200 loop is a cycle of nodes, made explicit (L215). Three structural wins: checkpoints (L207), human gates as nodes (L208), and time-travel debugging (L213).

Q: Why model an agent as a graph?

A: Because explicit is controllable (L215). A while-loop's shape is implicit (L200) — where does it pause for a human (L208)? Where is the state saved (L207)? What are the allowed transitions (L202)? The graph answers structurally: nodes are the work, edges are the transitions, the state is the data, gates are nodes (L215). That's the state-machine view (L207) implemented (L216).

Q: How do human gates work in a graph?

A: As a node (L208). The flow reaches the approval node, the graph pauses, the human decides, and the edge resumes — approve, deny, or edit (L208). The gate is on the map (L215): visible, testable (L341), and resumable from the checkpoint (L207). It's not an interruption bolted onto a loop — it's part of the graph's shape (L215).

Q: When is LangGraph the wrong fit?

A: When the flow is a straight line (L199). A linear loop — perceive, act, answer, no branches — modeled as a graph is the framework's structure for a loop's simplicity (L215). The graph earns its shape when the flow branches and gates: research paths (L202), approval workflows (L208), multi-agent coordination (L210). Shape decides (L199); the bare loop is the simpler default (L216).

Follow-Up Questions

  • How do you map the L200 loop onto a graph (L215)?
  • What lives in the shared state (L207)?
  • How do checkpoints work structurally (L255)?
  • How do multi-agent systems compose as graphs (L210)?
  • When is the bare loop simpler (L199)?

Comparison Table — While-Loop vs Graph

While-loop (L200)Graph (this lesson)
Shape (L215)implicitexplicit — nodes + edges
State (L207)scattered variablesthe graph's data
Checkpoints (L207)bolted onstructural (L255)
Gates (L208)interruptionsnodes on the map
Debugging (L213)log readingtime-travel (L207)
Fit (L199)straight linesbranching flows

The senior read: the right column is the loop made explicit — the graph is the L200 shape, drawn and implemented (L215).

Code Example — The Loop as a Graph

// LangGraph: agents as graphs — nodes, edges, shared state (L215).
import { StateGraph } from '@langchain/langgraph';

// THE SHARED STATE (L207) — the run's data, explicit (L206).
const AgentState = { context: [], scratchpad: [], step: 0 };

// NODES — the L200 work (L200, L215).
async function perceive(state) { … }                 // reads the context (L206)
async function decide(state) { … }                   // the model routes (L202)
async function act(state) { … }                      // the tool, vetted (L201, L316)
async function observe(state) { … }                  // the result joins (L164)

// HUMAN GATE — approval as a NODE (L208, L215).
async function approvalGate(state) {
  const d = await humanApprove(state.proposal, state.reasoning);   // L208
  return { gate: d };                                // the edge resumes on the decision
}

// THE GRAPH — the loop's shape, explicit (L215).
const graph = new StateGraph(AgentState)
  .addNode('perceive', perceive)                     // L200
  .addNode('decide', decide)                         // L202
  .addNode('act', act)                               // L201
  .addNode('gate', approvalGate)                     // L208
  .addEdge('perceive', 'decide')
  .addConditionalEdges('decide', routeByModel)       // tool → gate, answer → END (L202)
  .addEdge('gate', 'act')                            // approved → act (L208)
  .addEdge('act', 'perceive');                       // the cycle (L200)

// CHECKPOINTS — structural: the state saves at node boundaries (L207, L255).
const app = graph.compile({ checkpointer: memoryCheckpointer });   // L207
const run = await app.invoke({ context: [], step: 0 });            // L215
What the reader must SEE — the loop, made structural:

  StateGraph(AgentState)      → the shared state is the data (L207)
  addNode('perceive'…)        → the L200 work as nodes (L200)
  addConditionalEdges(route)  → the model routes the edges (L202)
  addNode('gate', …)          → the human gate is a node (L208)
  compile({ checkpointer })   → checkpoints are structural (L255)

  The map and the train — the loop's shape, explicit.

Tip

The line that shows the whole lesson: addConditionalEdges('decide', routeByModel) — the loop's routing as explicit edges. The while-loop hides the shape; the graph draws it — and what's drawn is controllable (L215).

Performance Notes

  • The graph's overhead is the state (L150). The shared state (L207) is passed per node — the serialization (L255) is the cost; the checkpointer (L207) adds a write per boundary (L150).
  • The checkpoints are the resume's enabler (L255). Structural saves (L207) cost a write (L151) and buy the crash-free resume (L207) — the same trade as L207 (L215).
  • The graph is testable per node (L341). Each node is a unit (L200) — the graph's explicit shape is the test plan (L341); the golden set (L343) runs the full traversal (L215).
  • The framework adds indirection (L151). The graph runtime (L214) is an overhead — priced against the branching control it buys (L216).

Debugging Scenarios

SymptomLikely causeFirst thing to check
The loop's shape is a tangleThe graph never drawn (L216)Model the nodes + edges (L215)
Resume failsThe state not explicit (L207)Move the run into the graph's state (L255)
Gates bypassedApproval not a node (L208)Put the gate on the map (L215)
Crash restartsNo checkpointer (L207)Compile with one (L255)
Over-engineeringA graph for a straight line (L199)Use the bare loop (L216)

Quick Revision Notes

  • LangGraph = agents as graphs (L215): nodes, edges, shared state (L207).
  • The L200 loop is a cycle of nodes (L200) — the model routes the edges (L202).
  • The state is the graph's data (L207) — explicit, checkpointable (L255).
  • Human gates are nodes (L208) — on the map, resumable (L215).
  • Time-travel: replay from any checkpoint (L207, L213).
  • Fit: branching → graph; straight line → bare loop (L199).

Cheat Sheet

LANGGRAPH = agents as graphs — the loop, made explicit

THE MODEL (L215)
  nodes    the work — perceive, decide, act, observe (L200)
  edges    the transitions — the model routes (L202)
  state    the run's data — context (L206), scratchpad (L202),
           step (L205) — explicit (L207)

THE STRUCTURAL WINS (L215)
  checkpoints  the state is the graph's data — saving is
               structural (L207, L255), not bolted on
  gates        approval is a NODE — pause, decide, resume (L208)
  time-travel  replay from any checkpoint (L207, L213)

THE FIT (L199)
  branching flows + gates → the graph (L215)
  a straight-line loop    → the bare loop is simpler (L216)

THE RULE
  draw the graph before the code (L216)
  the shape is the architecture (L200)

INTERVIEW, 4 MOVES
  1 model    "nodes, edges, shared state (L215)"
  2 mapping  "the L200 loop as a cycle of nodes (L200)"
  3 wins     "checkpoints, gates as nodes, time-travel"
  4 fit      "branching → graph; straight → bare loop (L199)"

Key Takeaways

Recap

  • LangGraph models agents as graphs (L215): nodes are the work, edges are the transitions, and the shared state carries the run (L207)
  • The L200 loop maps onto the graph (L200): perceive, decide, act, observe as nodes, with the model routing the edges (L202) — the loop's shape made explicit (L215)
  • The state is the graph's data (L207) — explicit context (L206), scratchpad (L202), and step count (L205) make checkpoints (L255) and resume (L207) structural
  • Human gates are nodes (L208) — the approval is on the map: visible, testable (L341), and resumable (L215)
  • Time-travel debugging (L207, L213) — checkpointed state (L255) means replaying a run from any node
  • The fit is by shape (L199): branching flows and gates earn the graph (L215); a straight-line loop is simpler as a bare loop (L216)

Check your understanding

Answer each one out loud. Then rate yourself honestly.

0/8
1

What are the graph's three parts (L215)?

2

How does the L200 loop map onto a graph (L200)?

3

Why is the state the graph's data (L207)?

4

How do gates work as nodes (L208)?

5

What is time-travel (L213)?

6

Why are checkpoints structural (L255)?

7

When is the graph the right fit (L199)?

8

Why draw the graph before the code (L216)?

A Closing Note — The Map That Makes the Train Controllable

You now hold the graph view: nodes for the work, edges for the routing, the shared state as the data, and the gates on the map — the L200 loop, drawn and implemented. The agent's shape is no longer implicit — it's a map you can checkpoint, gate, and replay (L215).

Next: the module's capstone — MCP & production agent architecture (L216), the loop plus the protocol.