Module 4 · Human-in-the-loop
interrupt() and Command: pause and resume
5 min read
Some decisions should not be made by a machine alone: refunding €800, emailing an important customer, deleting data. Human-in-the-loop means suspending the run at those precise moments, presenting the situation to a human, then resuming with their decision — whether three seconds or three days later, on the same server or another. LangGraph does this with two primitives: interrupt() and Command(resume=...).
interrupt(): suspending from a node
from langgraph.types import interrupt
def supervisor_review(state: RefundState):
refund = state["refund"]
answer = interrupt({"step": "supervisor", "order_id": refund["order_id"], "amount": refund["amount"]})
# ... this code only runs on resume, with the human's answer in `answer`
When a node calls interrupt(value):
- the node's execution stops immediately;
- LangGraph saves the state in a checkpoint, with the pending interrupt;
- the
invoke/ainvokecall returns, handing the interrupt's value to the caller.
The value passed to interrupt() is what the human (or the interface representing them) will see: it must contain everything needed to decide, and be JSON-serializable.
On the caller's side, with version="v2", interrupts are separated from the state:
result = await graph.ainvoke({"messages": [("user", text)]}, config, context=ctx, version="v2")
if result.interrupts:
question = result.interrupts[0].value
# {"step": "supervisor", "order_id": "ORD-1003", "amount": 848.0, ...}
Nothing runs while waiting: no blocked process, no open connection. The state is in the database, and the server can restart without consequence.
Command(resume=...): resuming
To resume, invoke the graph on the same thread with a Command:
from langgraph.types import Command
result = await graph.ainvoke(
Command(resume={"decision": "approve", "reviewer": "alice"}), config, context=ctx, version="v2"
)
LangGraph loads the checkpoint, runs the interrupted node again from the start, and this time interrupt() doesn't suspend: it returns the resume value. The node continues with the human's decision.
The fundamental rule: the node starts over
This is the most important point of this module, and the source of most bugs: on resume, all the code before interrupt() in the node runs again.
# ❌ Dangerous
def review(state):
send_email_to_manager(state) # runs at the pause… AND on resume: two emails
decision = interrupt({"amount": state["refund"]["amount"]})
...
# ✅ Correct: the side effect is in a separate node, before
def notify_manager(state):
send_email_to_manager(state)
return {}
def review(state):
decision = interrupt({"amount": state["refund"]["amount"]})
...
Practical rules:
- Put
interrupt()at the start of the node, or precede it only with code without side effects (computations, reading the state). - Side effects (notification, writes) go in a separate node before or after, or must be idempotent.
- LLM calls before an
interrupt()are made again on resume: double cost, and a possibly different answer. Compute them in a previous node and store the result in the state.
Multiple interrupts
A node can call interrupt() several times. On resume, LangGraph matches answers in call order: the first interrupt() gets the first answer provided, and so on. This makes it possible, for example, to ask again as long as the answer is invalid (lesson 4.2). Corollary: the order and number of interrupt() calls in a node must be deterministic. Don't put them in a loop whose number of turns depends on data that changes between resumes.
If several parallel nodes interrupt in the same step, several interrupts are pending at once. They are resumed by associating each answer with the interrupt's id:
result = graph.invoke(inputs, config, version="v2")
# two pending interrupts: legal approval and security approval, in parallel
answers = {i.id: decide(i.value) for i in result.interrupts}
result = graph.invoke(Command(resume=answers), config, version="v2")
Interrupts and subgraphs
An interrupt raised in a subgraph suspends the whole parent graph. For the caller, nothing changes: result.interrupts contains the interrupt, and Command(resume=...) on the parent's thread resumes the subgraph where it stopped. That is Relay's case: supervisor_review is a node of the refund subgraph.
To list pending interrupts from outside (an admin interface, for instance), use the state with subgraphs:
snapshot = await graph.aget_state(config, subgraphs=True)
for task in snapshot.tasks:
pending = list(task.interrupts)
if task.state is not None: # interrupts raised inside the subgraph
pending += [i for t in task.state.tasks for i in t.interrupts]
Static breakpoints
Alongside interrupt(), LangGraph offers static breakpoints, declared at compile time or at run time:
await graph.ainvoke(inputs, config, context=ctx, interrupt_before=["tools"])
# execution stops BEFORE the tools node; snapshot.next == ("tools",)
await graph.ainvoke(None, config, context=ctx) # resume: None as input
interrupt() | interrupt_before / interrupt_after | |
|---|---|---|
| Where | In a node's code, at the exact spot | Before or after a whole node |
| Condition | Can depend on the state (amount, risk…) | Systematic |
| Data passed | A rich value, shown to the human | None |
| Resume | Command(resume=value) | invoke(None, config) |
| Use | Business approvals in production | Debugging, step-by-step inspection |
In production, prefer interrupt(): the decision to interrupt is part of the business logic ("above €50") and the question asked to the human is explicit. Static breakpoints are an excellent debugging tool: stop before tools, inspect the planned tool calls, change the state if needed, and resume.
Key takeaways
interrupt(value)suspends the graph, saves the state and returns the value to the caller; a checkpointer is required.Command(resume=answer)on the same thread resumes:interrupt()then returns the answer.- The interrupted node starts over: no side effect or LLM call before
interrupt().- Several
interrupt()calls in a node are matched by order; parallel interrupts are resumed by id.interrupt_before/interrupt_after: static breakpoints, mostly useful for debugging.
Practice
- Run
examples/05_human_in_the_loop.pyand play the supervisor and the finance department in the terminal. - Add a
print()before theinterrupt()call in_ask_reviewer: how many times does it print for a two-level approval? Explain. - Run an order-tracking conversation with
interrupt_before=["tools"], inspect the planned tool calls, then resume.