Module 11 · Notebook 01
GenAI 2026 · LangGraph

One arrow changes
everything

CHAIN ask search miss "I don't know." ✗ stuck GRAPH the arrow a chain cannot have ask search rewrite real answer ✓ solved
3
words carry the whole framework
10
steps, one new idea each
4
pieces inside every agent
0
magic objects anywhere
The problem

Same search. Different wording. Dead end.

Pick a customer question and watch where it lands.

question retrievekeyword match answer "I don't know." hit miss no way back
Click a question above.
A person would think "money back means refund — search again." A chain has no arrow for that thought.
The vocabulary

Three words. That's the framework.

STATE · the shared whiteboard query : "refund policy" context : "Refunds take 5–7 days" attempts: 2 NODEone step of work NODEone step of work writes a small update reads the whole board EDGE · the arrow
State
A dict everyone can read. Nodes write only what they changed.
Node
A plain Python function. Board in, small update out.
Edge
Always go there — or look at the board and decide. Including backwards.
Step 2 · no model yet

Watch the board get merged

add_world never wrote "hello" — yet it can read it.

board: {text: ""}
START say_hellowrites "hello" add_worldreads, appends END
whiteboard · after each nodestep 0
nothing written yet
A node returns a delta. LangGraph merges it and carries every untouched key forward.
Step 3 · still no model

Now it can go back

Same questions from slide 2. Watch the counter — it's what stops the loop.

START retrievetry 0 generate give_up rewritesynonym → keyword END hit cap reached miss retry
Pick a question.
"Opening hours" has no matching document at all. What stops it looping forever?
Step 4 · reducers

Two nodes, one key. Who wins?

Press play. The third node has to answer "what is the user's name?" from the board alone.

plain list · no reducerreplace
empty board
waiting
MessagesState · add_messagesappend
empty board
waiting
Every model node returns one new message. That only works if the board appends.
Step 5

An LLM is not a special node

STEP 2 · PLAIN PYTHON board in add_world() update out deterministic STEP 5 · WITH A MODEL board in write_joke() update out llm.invoke() inside identical shape
what changes
The values on the board
The model decides what gets written. Non-determinism enters the state.
what does not change
The graph structure
Same add_node. Same add_edge. Same delta contract.
Step 6 · branch

Spend the money only when you must

Four questions, one entry point, two very different bills.

START classify— short_answersmall model, no tools detailed_answerbig model, full pipeline END SIMPLE COMPLEX
Pick a question.
Support bot
Order status → one lookup. Refund dispute → full agent.
Cost router
Cheap 8B by default. The 70B only on COMPLEX.
Risk gate
Refund or delete → pause for a human. That's notebook 02.
Step 7 · cycle

Raise the bar, pay the bill

Same topic. Three passing scores. Watch the loop count and the calls.

START writer criticscore — reviser END good enough too low re-score
Pick a threshold.
At pass 9 the loop did not stop because the critic was happy. It stopped because the cap fired.
Step 8 · agents

An agent is two boxes and a loop

One question needing two tools. Step through it.

0 of 4 messages on the board
START agentthe model toolsruns the functions END no tool asked tool asked result
messages · MessagesState0
One tools step added two results. Printing only the last message would have hidden one.
Step 9 · memory

One word on compile()

Noor introduced herself on thread 1. Ask both threads the same question.

thread_id student-12 saved
thread_id student-20 saved
before
self.history = []
A list on one object. Dies with the process. No user isolation.
after
compile(checkpointer=…)
Swap InMemory → Sqlite → Postgres. Nodes never change.
Step 10 · streaming

Three cameras, one graph

"values"
for debugging
"updates"
for a live status trace
"messages"
for a chat box
Only one [agent] line? Then the tool was never called — a prompt bug, not a graph bug.
Recap

Six things a straight line cannot do

Go back
Conditional edge plus a counter on the state.
Branch on output
A router returns the next box's name.
Keep a list
A reducer says how to merge instead of replace.
Remember per user
Checkpointer plus thread_id.
Stream progress
Per node, or per token.
Call tools
Model box, tools box, and a loop. No magic.
Next → notebook 02: add a pause button, and let a human approve the risky step.