Lesson 05 LangGraph Intro
Theme:
Lesson 05 · Slide 1

Lesson 05 — LangGraph Intro

When your agent has a shape, draw the shape. Graphs, nodes, edges, checkpoints.

Agents Module 3 LangGraph StateGraph

Lesson 05 · Slide 2

Recap of Module 2

Lesson 05 · Slide 3

What LangGraph is

One-line definition

LangGraph is a Python library for building agent control flow as an explicit state graph.

You declare:

The runtime walks the graph, persists state at every step, and can resume from any checkpoint.

Lesson 05 · Slide 4

What LangGraph isn't

Lesson 05 · Slide 5

Three primitives you'll meet today

StateGraph(State)

A typed graph builder. State is usually a TypedDict (or Pydantic model) describing what flows between nodes.

Nodes

Plain Python functions (state) -> dict-of-state-updates. The runtime merges your return dict into the global state.

Edges

add_edge(a, b) for “always go a → b” and add_conditional_edges(a, decide, mapping) for “after a, call decide(state) and route to one of mapping's targets.”

Lesson 05 · Slide 6

Hello-world graph in 20 lines

from typing import TypedDict
from langgraph.graph import StateGraph, START, END

class S(TypedDict):
    n: int
    log: list[str]

def add_one(s: S) -> dict:
    return {"n": s["n"] + 1, "log": s["log"] + ["add_one"]}

def double(s: S) -> dict:
    return {"n": s["n"] * 2, "log": s["log"] + ["double"]}

g = StateGraph(S)
g.add_node("add_one", add_one)
g.add_node("double", double)
g.add_edge(START, "add_one")
g.add_edge("add_one", "double")
g.add_edge("double", END)
app = g.compile()
print(app.invoke({"n": 3, "log": []}))
# {'n': 8, 'log': ['add_one', 'double']}

Three nodes (START, add_one, double, END), two edges, one compile. No LLM in the hello world on purpose — get the shape first.

Lesson 05 · Slide 7

Why a graph, not a chain?

Chain (linear)Graph (today)
Always A → B → CBranch on state after B
One path throughLoops, retries, conditional skips
No checkpoint surfacePersist state at every node
Hard to drawTrivially renderable as a DAG
Lesson 05 · Slide 8

Conditional edges in 8 lines

def route(s: S) -> str:
    return "double" if s["n"] > 10 else "add_one"

g.add_conditional_edges(
    "add_one",
    route,
    {"add_one": "add_one", "double": "double"},
)
g.add_edge("double", END)

After add_one, call route(state); the string it returns is keyed into the mapping to pick the next node. This is how branching, looping, and early-exit are all expressed.

Lesson 05 · Slide 9

Checkpoints in one line

from langgraph.checkpoint.sqlite import SqliteSaver

memory = SqliteSaver.from_conn_string(":memory:")
app = g.compile(checkpointer=memory)
app.invoke({"n": 3, "log": []}, config={"configurable": {"thread_id": "run-1"}})

Pass a checkpointer to .compile() and a thread_id per run, and every node transition writes the merged state to sqlite.

Crash, restart, resume — state is on disk.

Lesson 05 · Slide 10

What you can do with checkpoints

Lesson 05 · Slide 11

When to pick LangGraph

Pick it when…

  • Your control flow has a shape you can draw (pipeline with branches).
  • You need durable state / checkpoint resume.
  • You need human-in-the-loop pauses.

Sweet spot.

Pick something else when…

  • The LLM decides everything — Claude Agent SDK (L03/L04).
  • You need multi-provider routing — OpenAI Agents SDK (Module 4).
  • You want a multi-agent role-play shape — CrewAI (Module 5).
Lesson 05 · Slide 12

Cost of switching

Lesson 05 · Slide 13

Recap & what's next

Coming up: Lesson 06 — build a graph-shaped pipeline agent (planner → fetcher → critique branch → writer) with checkpoint resume.
1 / ?