Exercise: from a chain to a graph with an approval gate
- Track
- LLM Application Frameworks: LangChain & LangGraph
- By
- Sourav Banerjee
- Last updated
- 30 Nov 2026
01 Context
Problem statement
Larkspur Cloud's handbook assistant is an LCEL chain. It retrieves three passages, fills a prompt and returns the model's answer in one call. Billing now wants to read every reply about money before it reaches a customer, and a reply may wait hours for that review. A chain cannot stop halfway and pick up later, so you turn it into a LangGraph graph with a checkpoint and a human approval step.
Two rules apply. From lesson 4 of T4.2, a migration keeps the old behaviour, so the graph's drafts must match the chain's answers on a fixed question set before the chain is retired. From lesson 2 of T4.4, the gate approves only an exact value and treats anything else as no.
02 Outcomes
What you will learn
- Rebuild an LCEL chain as graph nodes that each return only the keys they change.
- Check a migration by comparing the graph's drafts with the chain's answers as exact strings.
- Route a draft to a review node with a conditional edge, and pause it there with
interrupt(). - Read a parked thread's
nextand checkpoint count, and resume it from a second compiled graph. - Count what a resumed node does twice, and move it out of the gate.
03 Setup
Before you start
Lessons
- T4.2 LCEL, Runnables & Chains: lesson 1 of T4.2 LCEL and the Runnable interface for
RunnableLambdaandRunnablePassthrough, lesson 2 of T4.2 LCEL deep dive for the Larkspur RAG chain, and lesson 4 of T4.2 Legacy chains and migration to LCEL for checking a migration on a fixed question set. - T4.3 LangGraph Core: lesson 1 of T4.3 State graphs and reducers, lesson 2 of T4.3 Conditional edges and LLM nodes and lesson 4 of T4.3 Nodes, runtime context and Command.
- T4.4 LangGraph Runtime: lesson 1 of T4.4 Checkpointing and threads, lesson 2 of T4.4 Interrupts and human-in-the-loop and lesson 6 of T4.4 State management in practice for the double-run gate.
Dataset
The Larkspur Cloud handbook (13 files) and its golden set, in data/. The exercise runs the first 10 questions that have evidence spans, g01 to g10.
Code
load_handbook, load_jsonl and tfidf_retriever from src/lab_kit.py, the recursive splitter from langchain_text_splitters, ChatOllama from langchain_ollama, and StateGraph, InMemorySaver, interrupt and Command from langgraph.
Environment
The course's default environment (uv run jupyter lab) and a local Ollama server with qwen2.5:3b pulled (ollama pull qwen2.5:3b). Everything runs offline, with no API keys. The reviewer is scripted: the notebook resumes each paused thread with Command(resume=...) and a fixed decision, where an app would pass a person's click.
04 Architecture
Experiment workflow
The questions, the retriever, the prompt, the model and its temperature stay fixed. Only the wrapper changes, from one chain call to a graph that can stop between calls.
Extension: move the review notice into its own node and count it again.
05 Build
Implementation tasks
Task 1: Load the data
Load the handbook and the golden set from
data/larkspur/. Split the handbook with the recursive splitter at 500 characters with 50 of overlap, keep the file name of every chunk, and build a TF-IDF retriever withk = 3.Task 2: Build the chain
Build the T4.2 chain: a dict whose
contextbranch retrieves and joins the passages, each tagged with its file name, and whosequestionbranch passes the question through, then the prompt, the model and a string parser. Run it on the 10 questions and keep the answers.answer_step = ANSWER | ChatOllama(model="qwen2.5:3b", temperature=0) | StrOutputParser() chain = {"context": RunnableLambda(context_of), "question": RunnablePassthrough()} | answer_stepTask 3: Turn it into a graph
Give each step of the chain its own node.
retrievewritescontextandsources, anddraftruns the sameanswer_stepon them. A conditional edge afterdraftgoes toreviewwhen the top passage comes from Billing, Refunds or Sla credits, and tosendotherwise.reviewcallsinterrupt()with the question, the draft and the sources, then returnsCommand(goto="send")only for the exact value"approve"andCommand(goto="hold")for anything else. Add one line aboveinterrupt()that counts a review notice per thread. Compile withInMemorySaver.Task 4: Run every thread once
Invoke the graph once per question, with the question id as the
thread_id. For each thread, record whether the result holds__interrupt__, thenextofget_state, the number of checkpoints inget_state_history, and whether the draft equals the chain's answer as an exact string.Task 5: Resume as the reviewer
Compile a second graph,
worker_b, from the same builder and the same saver. Resume the parked threads on it, in order, with these values, and record the status, the checkpoints and the review notices of each thread.DECISIONS = ["approve", "Approve", "yes", "no"] worker_b.invoke(Command(resume=decision), {"configurable": {"thread_id": qid}})Task 6: Extension: send the notice once
Build the same graph with a
notifynode betweendraftandreview, and remove the counter from the gate. Copy each reviewed thread's saved values into a fresh thread withupdate_state(..., as_node="draft"), continue it withinvoke(None, ...), resume it with the same decision and count the notices again.New here: lesson 1 of T4.4 copies state into a new thread with
update_stateand explainsas_node. Passingas_node="draft"makes the copied state continue from the edge afterdraft, so the model is not called again.
06 Measure
Record results
What you enter stays in this browser only.
| Measure | Value |
|---|---|
| Questions run | 10 |
| Drafts identical to the chain's answers | |
| Sent without review | |
| Paused at review | |
| Checkpoints per thread: sent without review, paused, after resume | |
| Sent after review, resumed with "approve" | |
| Held after review, resumed with "Approve", "yes" or "no" | |
| Review notices per reviewed reply, notice above `interrupt()` | |
| Review notices per reviewed reply, notice in its own node (extension) |
A thread is paused when the first call returns __interrupt__ and next is ("review",). A checkpoint count comes from get_state_history on that thread.
07 Verify
Done when
0 of 4 done
What you enter stays in this browser only.
08 Debug
Common mistakes
Resuming with the question again
invoke({"question": ...}) on a parked thread starts a new run on it. Resume with Command(resume=value).
Compiling without a checkpointer
The first call still returns __interrupt__, and the resume raises RuntimeError because nothing was saved (lesson 2 of T4.4).
Approving on anything that looks like yes
A gate that never reads the resume value approves "no" as well, the rubber-stamp gate of lesson 2 of T4.4. Compare with one exact value.
Work above `interrupt()`
The gate runs again from its first line on resume, so a notice, a counter or a model call above interrupt() happens twice (lessons 2 and 6 of T4.4).
A new thread id for the resume
A different thread_id opens an empty thread, and nothing raises an error (lesson 1 of T4.4).
09 Transfer
Interview angle
“When would you move an LCEL chain to LangGraph?”
Show a model answer
When the work has to stop and continue later, for example to wait for a person, retry, or loop. Keep the chain's steps as nodes, check the new path against the old answers on a fixed set, then add a checkpointer and a thread id so a parked run survives the wait and any worker can resume it. Approve only on an exact value, and keep side effects out of the gate.
10 Review
Worked solution
The worked solution is solution/p03_exercise_solution.ipynb (open in Colab). Locally, open it from exercise/solution/ so ../data/ resolves, and run it top to bottom with the default environment and Ollama running. In Colab, clone the repository first, because the notebook reads the corpus and src/lab_kit.py from it, and install and start Ollama in the runtime. The human approval step is scripted: the notebook resumes each paused thread with Command(resume=...) and a fixed decision from a list.
Show our run and the reasoning
Our run: qwen2.5:3b through Ollama 0.40.6 at temperature 0, LangGraph 1.2.18, langchain-core 1.7.0, langchain-ollama 1.1.0 and scikit-learn 1.9.1, offline.
| Measure | Value |
|---|---|
| Questions run | 10 |
| Drafts identical to the chain's answers | 10 of 10 |
| Sent without review | 6 (g02, g03, g04, g05, g07, g10) |
| Paused at review | 4 (g01, g06, g08, g09) |
| Checkpoints per thread: sent without review, paused, after resume | 5, 4, 6 |
| Sent after review, resumed with "approve" | 1 (g01) |
| Held after review, resumed with "Approve", "yes" or "no" | 3 (g06, g08, g09) |
Review notices per reviewed reply, notice above interrupt() |
2 |
| Review notices per reviewed reply, notice in its own node (extension) | 1 |
All 10 drafts match the chain's answers word for word. The draft node runs the chain's own answer_step on the same prompt and passages, and the model is deterministic at temperature 0, so the migration changed the wrapper and nothing else.
The gate stopped the four replies whose top passage came from a billing file. Each parked thread held 4 checkpoints with next set to ("review",), and worker_b, a separate compile on the same saver, resumed all four. Only "approve" reached send. "Approve", "yes" and "no" all went to hold, the fail-closed rule from lesson 2 of T4.4.
The router has a blind spot. g02, "What happens if my server goes down?", went out without review, but half of its answer is the service-credit rule in Sla credits. None of its three passages came from that file, so the gate never saw it. A gate that routes on retrieval inherits retrieval's misses. g03 shows the other edge of the rule: Billing was its second passage, so a rule on any passage would have sent it to review. The reviewer also has work to do. g01's approved draft gives the 5 to 10 business days and leaves out the annual-plan full refund.
Every reviewed reply produced two notices, because review ran once to pause and once more on resume. With the notice in its own notify node, each reply produced one.