█
LastWrite
  • > Curriculum
  • > Pricing
  • > For Educators
  • > About
  • > Contact
Log InGet Started

Questions, concerns, bug reports, or suggestions? We read every message, write to us at [email protected].

More ways to reach us →
LastWrite

Structured computer science lessons for aspiring developers and security professionals.

[email protected]

(201) 785-7951

Mon–Fri, 9 AM–5 PM EST

Learn

  • Curriculum
  • Pricing

Company

  • About
  • For Educators & Schools
  • Contact Us

Legal

  • Terms of Service
  • Privacy Policy
© 2026 LastWrite. All rights reserved.
Curriculum/AI Agents/Code-First Agents/LangGraph: Stateful Multi-Step Agent Graphs
75 minAdvanced

LangGraph: Stateful Multi-Step Agent Graphs

After this lesson, you will be able to: Use LangGraph to build graph-based agentic workflows with conditional branching, cyclic loops, and persistent checkpointing.

AgentExecutor is fine for simple loops. Real workflows have conditionals, branches, parallel steps, and human-in-the-loop checkpoints. LangGraph models agents as state graphs, nodes are functions, edges are control flow, and state flows between them. This lesson builds a multi-step research agent with branching.

Prerequisites:LangChain: The Agent Framework

Why graph?

AgentExecutor = loop. LangGraph = graph. Same model deciding what to do, but you control where it can go next, what state persists, when to stop. Production agent code lives in LangGraph today.

START
→
plan
→
research
→
reflect
→
write
→
END
A LangGraph state machine. research and reflect loop until the plan is satisfied (research can even call itself), then a conditional edge sends control to write.

Install + concepts

  1. 1

    1. `pip install langgraph langchain-anthropic`

  2. 2

    2. Mental model: a graph has State (the dict that flows through), Nodes (functions that read+update state), and Edges (where to go next, conditionally).

  3. 3

    3. You compile the graph; calling it runs from START until END.

Plan-research-write graph

Three nodes, conditional edges, persistent state.

python
from typing import TypedDict, Annotated
from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages
from langchain_anthropic import ChatAnthropic
model = ChatAnthropic(model='claude-sonnet-4-6')
class State(TypedDict):
topic: str
plan: str
research: list[str]
draft: str
def plan(state: State):
p = model.invoke(f'Make a 3-bullet research plan for: {state["topic"]}').content
return {'plan': p}
def research(state: State):
r = model.invoke(f'Plan: {state["plan"]}\nResearch this and return facts.').content
return {'research': state.get('research', []) + [r]}
def write(state: State):
d = model.invoke(f'Plan: {state["plan"]}\nFacts: {state["research"]}\nWrite a 300-word piece.').content
return {'draft': d}
g = StateGraph(State)
g.add_node('plan', plan)
g.add_node('research', research)
g.add_node('write', write)
g.add_edge(START, 'plan')
g.add_edge('plan', 'research')
g.add_edge('research', 'write')
g.add_edge('write', END)
app = g.compile()
result = app.invoke({'topic': 'why ravens are smart'})
print(result['draft'])

💡 Conditional edges = branching

Use `add_conditional_edges('node_name', router_fn, {...})` to route to different nodes based on a function's return value. This is how you build retry loops, error branches, human-in-the-loop pauses.

Checkpointing for resumability

Pass `checkpointer=MemorySaver()` (or PostgresSaver for prod) to `g.compile()`. Each step's state is saved. You can resume mid-flow, replay, or fork, invaluable for long-running agents.

Sign in and purchase access to unlock this lesson.

Sign in to purchase
←LangChain: The Agent Framework
Back to Code-First Agents
LlamaIndex: RAG Pipelines and Data Agents→