By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
The support graph may not refund 84.00 without a human saying yes. That requirement turns one request into two turns: the first run prepares the exact proposed action, pauses inside interrupt(), and ends with the payload in the result; the second run happens later, possibly on another worker process, when a caller resumes the same thread with Command(resume=...). The checkpointer is what makes the second turn possible. In this lab you build that flow with InMemorySaver (a lab convenience — production swaps in a durable saver) and test five behaviours: pause, interruption payload, approval resume, rejection, and duplicate prevention.
The subtle part is that resuming re-runs the interrupted node from its beginning. The runtime stores the checkpoint and replays the pending task; this time interrupt() returns the human's value instead of pausing. Anything the node did before interrupt() happens a second time, so side effects belong after the pause or behind an idempotency key. A duplicate client retry can also send the same resume twice, which is why the write itself must be safe to repeat.
The graph below is the smallest complete version: an approval node that interrupts, a router that turns the decision into a label, an execute node for the approved path and a decline node for everything else. The execute node derives its idempotency key from the thread's order id and consults a local ledger, which is the same shape as the production intent-key check from the specialization module.
from typing import Literal, TypedDict
from langgraph.checkpoint.memory import InMemorySaver
from langgraph.graph import END, START, StateGraph
from langgraph.types import Command, interrupt
class RefundState(TypedDict):
order_id: str
amount: float
decision: str | None
refund_id: str | None
LEDGER: dict[str, str] = {}
def approve(state: RefundState) -> dict:
"""Turn 1 pauses here; turn 2 re-runs this node."""
decision = interrupt(
{
"action": "issue_refund",
"order_id": state["order_id"],
"amount": state["amount"],
"question": "Approve this refund?",
}
)
return {"decision": decision}
def route_after_approval(
state: RefundState,
) -> Literal["execute_refund", "decline"]:
return "execute_refund" if state["decision"] == "approved" else "decline"
def execute_refund(state: RefundState) -> dict:
key = f"{state['order_id']}:refund"
if key not in LEDGER:
LEDGER[key] = f"RF-{state['order_id']}"
return {"refund_id": LEDGER[key]}
def decline(state: RefundState) -> dict:
return {"refund_id": None}
builder = StateGraph(RefundState)
builder.add_node("approve", approve)
builder.add_node("execute_refund", execute_refund)
builder.add_node("decline", decline)
builder.add_edge(START, "approve")
builder.add_conditional_edges(
"approve",
route_after_approval,
{"execute_refund": "execute_refund", "decline": "decline"},
)
builder.add_edge("execute_refund", END)
builder.add_edge("decline", END)
graph = builder.compile(checkpointer=InMemorySaver())
config = {"configurable": {"thread_id": "refund-A17"}}
paused = graph.invoke(
{"order_id": "A17", "amount": 84.0, "decision": None, "refund_id": None},
config,
)
print("paused:", "__interrupt__" in paused)
print("pending:", graph.get_state(config).next)
print("payload:", paused["__interrupt__"][0].value["action"])
resumed = graph.invoke(Command(resume="approved"), config)
print("decision:", resumed["decision"])
print("refund:", resumed["refund_id"])A pause that looks right in a demo can still lose the decision or write twice. The test file below treats each behaviour as a separate contract, and the ledger check makes duplicate prevention observable instead of theoretical. The duplicate test calls the write function directly because that is what a retried resume does to the runtime: the pending task is re-executed, not skipped.
| Behaviour | How the test triggers it | Assertion |
|---|---|---|
| Pause | First invoke on a fresh thread | __interrupt__ is in the result and get_state(config).next == ('approve',) |
| Interruption | Read result['__interrupt__'][0].value | The payload names the action, order and amount the human is approving |
| Resume | invoke(Command(resume='approved'), config) | decision == 'approved' and the ledger holds exactly one refund |
| Rejection | Command(resume='rejected') on a fresh thread | refund_id is None and the ledger stayed empty |
| Duplicate prevention | Run execute_refund twice with the same intent | Both calls return the same id and the ledger holds one entry |
import pytest
from langgraph.types import Command
from refund_flow import LEDGER, build_refund_graph, execute_refund
INPUT = {"order_id": "A17", "amount": 84.0, "decision": None, "refund_id": None}
@pytest.fixture(autouse=True)
def clear_ledger():
LEDGER.clear()
yield
@pytest.fixture
def graph():
return build_refund_graph()
def test_pause_leaves_the_approval_pending(graph):
config = {"configurable": {"thread_id": "refund-pause"}}
paused = graph.invoke(INPUT, config)
assert "__interrupt__" in paused
assert graph.get_state(config).next == ("approve",)
def test_interrupt_payload_names_the_exact_action(graph):
config = {"configurable": {"thread_id": "refund-payload"}}
paused = graph.invoke(INPUT, config)
payload = paused["__interrupt__"][0].value
assert payload["action"] == "issue_refund"
assert payload["order_id"] == "A17"
assert payload["amount"] == 84.0
def test_approval_resumes_and_writes_once(graph):
config = {"configurable": {"thread_id": "refund-approve"}}
graph.invoke(INPUT, config)
resumed = graph.invoke(Command(resume="approved"), config)
assert resumed["decision"] == "approved"
assert resumed["refund_id"] == "RF-A17"
assert list(LEDGER.values()) == ["RF-A17"]
def test_rejection_completes_without_a_write(graph):
config = {"configurable": {"thread_id": "refund-reject"}}
graph.invoke(INPUT, config)
resumed = graph.invoke(Command(resume="rejected"), config)
assert resumed["decision"] == "rejected"
assert resumed["refund_id"] is None
assert LEDGER == {}
def test_duplicate_resume_reuses_the_intent_key():
first = execute_refund({**INPUT, "decision": "approved"})
second = execute_refund({**INPUT, "decision": "approved"})
assert first["refund_id"] == second["refund_id"] == "RF-A17"
assert list(LEDGER.values()) == ["RF-A17"]InMemorySaver for a durable saver whose migrations run before traffic; the lab's in-memory saver is a deliberate simplification.Key takeaways
An interrupt is a checkpointed pause: the run returns the payload as data and the thread waits for Command(resume=...) on the same thread_id.
Resuming re-runs the interrupted node from its start; anything before interrupt() happens again, so side effects belong after the pause or behind an intent key.
Test five contracts, not one: pause, interruption payload, approval resume, rejection and duplicate prevention.
Duplicate prevention is a write property, not a UI property: key the write by the intent and return the recorded result when the key already exists.
The approval flow is a two-turn conversation; both turns need a durable store in production, and the decision is evidence worth auditing.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.