Courses / Agentic AI from Scratch (Python)
Session 26 of 40
Human in the loop: pause, approve, resume
Notes
How to Pause a LangGraph Graph for Human Approval With interrupt() and Command(resume)
Agentic AI Foundations · Session 26 — Human in the loop: pause for approval, then resume
Before you start: what do I need before I add an approval step to a LangGraph graph?
You need the code folder for this session open in your terminal. It holds approval_graph.py, the carried-over files it uses (tools.py, helpdesk_rag.py, search.py, build_index.py, load_policies.py, make_tickets_db.py), the prompts in model_prompts/, the sample data in data/ and one build prompt in prompts/. Install the libraries with pip install langchain langchain-openai langchain-chroma langchain-community langchain-text-splitters langgraph langgraph-checkpoint-sqlite, set LLM_BASE_URL, LLM_API_KEY, LLM_MODEL and EMBED_MODEL as in earlier sessions, then run python build_index.py and python make_tickets_db.py. The checkpoint file copilot_checkpoints.db is not in the lesson files: the first run creates it. Recap of what this session assumes: a checkpointer saves a graph’s state after each step, and a thread id names one saved conversation, so the same id loads the same state (session 25). The ticket path also relies on the three tools of session 24, of which create_ticket writes a new row to tickets.db. Have paper or a notes app ready for the Flashcards and the Model answer.
Summary: what is human in the loop in LangGraph?
Human in the loop means a person reviews an action before the program carries it out. In LangGraph a node calls interrupt(), which saves the graph’s state and stops the run, handing a draft to whoever called the graph. The person answers, and the program calls the graph again on the same thread with Command(resume=...); the value inside becomes what interrupt() returns in the paused node. Because a resumed node starts again from its top, the node that asks must not be the node that writes: in approval_graph.py the ticket is created by a separate node after the approval, and a rejected request goes to a node that writes nothing. You run it twice, approving one request and rejecting another, and compare the ticket counts.
Key concepts: human-in-the-loop terms in LangGraph explained in plain English
| Term | Definition |
|---|---|
| Human in the loop | A design where a person reviews, and can stop, an action before the program carries it out. |
| Approval node | A node that only asks a person for a decision and records the answer; it changes no data. |
interrupt() |
A function that, called inside a node, saves the graph’s state and stops the run, passing a payload to the caller. |
| Interrupt payload | The value given to interrupt(), such as a draft to show a person; it comes back to the caller under __interrupt__. |
Command(resume=...) |
The input that wakes a paused graph; its resume value becomes the return value of interrupt(). |
| Resume | Calling the graph again on the same thread with a Command, so a paused run carries on. |
| Side effect | An action that changes something outside the graph’s state, such as adding a row to a table. |
| Idempotent step | A step that has the same effect when it runs twice. |
__interrupt__ |
The key in the result of invoke that shows the graph stopped to ask something. |
| Draft | The plan the program has understood and shown to a person, before anything is written. |
Explanations: how does human in the loop work in LangGraph?
What does this session add to the Copilot?
Until now the ticket path of the Copilot created a ticket the moment it was asked. Today the ticket path has several nodes, and a pause in the middle. Session 25’s checkpointer is what makes the pause possible: it saves the state where the graph stopped.
START --> receive --> sort --> policy_node -----------+
| |
+--> ticket path (CHANGES) --+--> save_turn --> END
| |
+--> fallback_node ----------+
The policy and fallback paths are unchanged. Think of the ticket path as an order desk: a clerk drafts the order, a manager signs it, and only then does the buyer place it.
What is human in the loop, and where does the yes belong?
Failure first. An employee mentions a broken monitor in passing. The Copilot guesses the employee wants a ticket, and creates one. The ticket is in the table before anyone has checked the guess, and deleting it afterwards is a repair, not a prevention.
Think of a second signature on a cheque: the cheque is written, but nothing moves until someone countersigns. Or a manager’s sign-off on a purchase: the buyer drafts the order and the money is spent after approval. Human in the loop is that countersignature for a program. LangGraph’s documentation describes approval workflows that pause before critical actions such as API calls, database changes and financial transactions.
Not every step needs one. Reading a ticket changes nothing, so a lookup goes straight through; creating a ticket changes data, so it waits for a yes. The rule is about what the step does, not how important it feels.
How does the ticket path draft a request?
draft_node (reads the request)
|
+-- lookup --> lookup_node --------------------+
| |
+-- create --> approval_node (PAUSES here) |
| |
approved? |
/ \ |
create_node cancel_node |
(writes) (writes nothing) |
\ / v
+------+---------------------> save_turn
The first node, draft_node, reads the message with a structured-output call and fills three new state keys. It changes no data:
def draft_node(state):
request = reader.invoke([("system", ticket_text),
("human", state["question"])])
return {"action": request.action,
"ticket_id": request.ticket_id, "title": request.title}
def route_after_draft(state):
return ("lookup_node" if state["action"] == "lookup"
else "approval_node")
model_prompts/ticket.md tells the model to decide between a lookup, with a ticket id, and a create, with a short title. route_after_draft is a conditional edge: a lookup goes to lookup_node, and a new ticket goes to approval_node. A draft is like a plan on paper: nothing exists yet.
What is interrupt(), and how does the graph pause?
Think of a bookmark in a book: the page is saved, and you come back to it later. Or a form left on a desk: it waits until someone signs. interrupt() does this for a graph. LangGraph’s documentation says that calling it inside a node saves the current graph state and waits for you to resume with input. It needs a checkpointer and a thread id, and the payload you give it must be JSON-serializable.
def approval_node(state):
decision = interrupt({"action": "create a ticket",
"title": state["title"]})
return {"approved": str(decision).strip().lower()
in ("approve", "approved", "yes", "y")}
The payload is the draft: the action and the title. When the graph is run with graph.invoke, a pause shows up as __interrupt__ in the result, and the caller reads the payload from it. The graph then waits, with no deadline, until someone resumes it.
What is Command(resume=...), and how does the graph carry on?
The program shows the draft, asks the person, and calls the graph a second time:
config = {"configurable": {"thread_id": thread_id}}
result = graph.invoke({"question": question}, config)
if "__interrupt__" in result:
draft = result["__interrupt__"][0].value
decision = input("approve or reject? ")
result = graph.invoke(Command(resume=decision), config)
The second invoke takes a Command instead of a question. Its resume value is what interrupt() returns inside approval_node, so decision holds the person’s text and the node turns it into approved, true or false. The config is the same one, with the same thread id: that is how LangGraph knows which paused graph to wake up. A new thread id would start a different, empty conversation.
A reply is counted as a yes only if it is approve, approved, yes or y. Anything else, including maybe, sets approved to false. Think of a bouncer who admits only guests on the list: an unclear name does not get in.
Why must the node that asks not be the node that writes?
Failure first. Suppose approval_node created the ticket first and then called interrupt(). The graph would write a row, stop, and wait. When the person approved, the node would start again, and the write above the interrupt() would run a second time. The table would hold two tickets for one request. Worse, a rejected request would already have left its row behind.
The cause is how resuming works. LangGraph’s documentation says the node restarts from the beginning of the node where interrupt() was called, so code before the call runs again. It advises that side effects before an interrupt be idempotent, be placed after it, or sit in a separate node. Think of a form that is shredded and refilled when it is resumed: stamping it with ink before the pause would stamp it twice.
approval_node (asks) |
create_node (writes) |
|
|---|---|---|
| What it does | Calls interrupt(), then sets approved |
Calls create_ticket |
| How many times it starts | Twice: once to pause, once on resume | Once, after approval |
| A write here | Would run again on resume | Is where the write belongs |
So approval_graph.py keeps the asking and the writing apart:
def create_node(state):
return {"answer": create_ticket.invoke(
{"title": state["title"]})}
def cancel_node(state):
return {"answer": "Okay, I did not create a ticket."}
def route_after_approval(state):
return "create_node" if state["approved"] else "cancel_node"
Both branches join save_turn, so the thread records what happened.
How do you run it, and what should you look for?
Build the data, then run the file:
python build_index.py
python make_tickets_db.py
python approval_graph.py
It runs two requests on two threads: one for a monitor that flickers, one for a standing desk. At each pause it asks approve or reject?. Type approve the first time and reject the second. Look for each run stopping and showing the draft before anything is written. The program also calls count_tickets, which counts the rows in tickets.db, once while paused and once at the end. The paused count matches the count before the request, because the pause is before the write. After approve the final count goes up by one; after reject it stays where it was. Comparing the two final counts is the proof that a no really wrote nothing.
What goes wrong with approval, and what does it mean for the design?
Before the design table, three situations you will meet:
- An approved request creates two tickets. The write sat in the asking node, and the resume started that node again from its top.
- A rejected request still leaves a new row in the table. The write happened before the pause, so rejecting could not undo it.
- The graph never pauses for a request. The sorter did not label it a ticket, or the request was a lookup, which goes straight through.
What these mean for the design:
| What you learned | The design decision | Where it shows |
|---|---|---|
| A resumed node starts again | The asking node only asks | approval_node |
| A no cannot undo a write | Write in the node after the approval | create_node |
| Reading is safe, writing is not | Pause only on the branch that writes | route_after_draft |
| A pause needs saved state | Compile with a checkpointer and reuse the thread id | config in run |
Basic assignment: run approval_graph.py, approve one request and reject the other, and write down the ticket count while paused and at the end of each run. Explain why the counts differ.
Project step: change one thing and see. Reply maybe instead of approve and say which node runs and why. Press Ctrl+C at the prompt, then start the file again, and look at whether the earlier thread is still paused in copilot_checkpoints.db. Move the create_ticket call into approval_node, before interrupt(), approve a request and count the tickets. Decide whether pausing before a lookup is a good idea. The build prompt is prompts/1-write-approval-graph.md. Stop when you can say why the ticket is created in the node after the approval.
Flashcards: test yourself on human in the loop in LangGraph
Say the answer aloud before revealing it. Speaking is a different skill from reading, so practise both.
1. What is human in the loop?
A design where a person reviews, and can stop, an action before the program carries it out. In `approval_graph.py` the Copilot drafts a ticket and waits for approve or reject.2. Which actions should wait for a person's yes?
Actions that change data, such as creating a ticket. A lookup only reads, so it goes straight through, and pausing it would only slow people down.3. What does `interrupt()` do?
Called inside a node, it saves the graph's state and stops the run, handing a payload such as the draft to the caller. It needs a checkpointer and a thread id.4. How do you notice a pause in the result of `invoke`?
The result contains the key `__interrupt__`, and the draft is the value of its first item. If the key is missing, the graph ran to the end.5. How do you resume a paused graph?
Call `invoke` again with `Command(resume=answer)` and the same config, so the same thread id. A different thread id would not wake the paused graph.6. What does `interrupt()` return after a resume?
The value passed in `Command(resume=...)`. In `approval_node` that is the person's text, which the node turns into `approved`.7. Why must the node that asks not be the node that writes?
A resumed node starts again from its top, so a write above `interrupt()` would run a second time and could create a duplicate. A write in the next node runs once, after the yes.8. What happens when the person replies `maybe`?
`approved` is false, because only approve, approved, yes or y count as a yes, so the graph goes to `cancel_node` and writes nothing. An unclear answer is treated as a no.9. Why does the ticket count not change while the graph is paused?
The pause comes before `create_node`, the only node that writes. The count rises only after an approval, and stays the same after a reject.10. Why does a pause need the checkpointer from session 25?
The state must be saved where the graph stopped so it can be loaded again on resume. Without a checkpointer and a thread id there is nothing to wake up.Model answer: how do I explain human-in-the-loop approval in LangGraph in an interview?
The core question this session answers: “A teammate asks: the assistant created a ticket nobody wanted. How do we make it ask first?”
Four beats a teammate listens for, in this order: decide what needs a yes · pause · resume · write after the yes. Skip any of these four and the answer is incomplete.
- Decide what needs a yes: the action that writes, not the one that reads.
- Pause:
interrupt()in a node that only asks, with the draft as its payload. - Resume: the same thread, with
Command(resume=...)carrying the answer. - Write after the yes: a separate node writes, because a resumed node starts again.
The full answer, said aloud:
(Decide what needs a yes) “I would separate actions that read from actions that change data. A lookup is safe, but creating a ticket writes a row, so that step waits for a person.
(Pause) The ticket path drafts the request, then a node calls
interrupt()with the draft. That saves the state with the checkpointer and stops the graph, and the caller sees the draft.(Resume) A person answers, and I call the graph again on the same thread with
Command(resume=answer). The answer is whatinterrupt()returns inside the node.(Write after the yes) The ticket is created in a separate node after the approval, because a resumed node starts again from its top and an earlier write would run twice. A reject goes to a node that writes nothing.“
Missing a beat is a failure state. The follow-up question lands where you went quiet: leave out the last beat and the next question is how you know the ticket was not created twice. Practise until the four come out without prompting, then stop talking and let the questions come.
Why this matters: why does human-in-the-loop approval matter at work?
In a conversation with a teammate or manager: “what is the worst thing it can do on its own?” is a fair question about any assistant that can write. A pause before the write gives you a clear answer.
On the job: requests that create records, send messages or spend money are the ones a person should see first. The same pause also serves a reviewer who wants to edit a draft before it goes ahead.
In the rest of this course: session 27 adds a self-check on policy answers and a memory that outlives one thread, session 28 reviews the routed, saved, approved Copilot, and sessions 35 and 36 cover securing the Copilot and keeping an audit trail of what it does.
After this unit: what do I do after this lesson, and what comes next?
Assignment. Run approval_graph.py, approve one request and reject one, and explain why the ticket count differs. The brief for the project work, changing one thing and seeing the effect, is the project file. Keep approval_graph.py: the next session builds on it.
What’s next. Session 27 adds a self-check step on policy answers, and a long-term memory that keeps one fact for a user across threads.
Want help building this with someone?
Tell us what you are working on and we will write back about a mentor-led cohort, live sessions or 1:1 training. It is only an enquiry: you choose whether to go further.
Ask about mentor-led trainingTechEazy Consulting training material — not affiliated with or endorsed by OpenAI, Anthropic, LangChain or any other company named in this course.