Expert about 5 h
The picture
01
Run to a gate
before the risky step
02
Interrupt
persist state
03
Human decides
approve, edit, reject
04
Resume
same thread
05
Retry safely
idempotency keys
Why this matters
Real agents run for minutes or hours, call flaky services, and sometimes need a person. If the process dies at step 7 of 10, you want to resume at step 7, not start over and not repeat an action that already happened.
Approval gates
from langgraph.types import interrupt, Command
from langgraph.checkpoint.sqlite import SqliteSaver
def request_refund(state):
if state["amount"] > 100: # policy in code, not in the prompt
decision = interrupt({"question": "Approve refund?", "amount": state["amount"], "order": state["order_id"]})
if decision["action"] != "approve":
return {"status": "rejected", "note": decision.get("note", "")}
issue_refund(state["order_id"], state["amount"], idempotency_key=state["run_id"])
return {"status": "refunded"}
# ... build the graph, then compile with a durable checkpointer
app = graph.compile(checkpointer=SqliteSaver.from_conn_string("agent.db"))
cfg = {"configurable": {"thread_id": "refund-8841"}}
first = app.invoke({"order_id": "8841", "amount": 250, "run_id": "refund-8841"}, cfg)
# The run stops at the interrupt and returns; state is saved.
# Later, possibly in another process:
app.invoke(Command(resume={"action": "approve"}), cfg)
The rule that matters: the decision to require approval lives in your code, based on facts (amount, action type), not in a model's judgement. Show the approver the exact action and arguments, and let them edit as well as approve.
Idempotency
A retry after a timeout may repeat a call that actually succeeded. Give every side-effecting action an idempotency key derived from the run (and step), and make the receiving side ignore duplicates.
def with_idempotency(fn):
def wrapper(*args, run_id: str, step: str, **kw):
key = f"{run_id}:{step}"
if store.exists(key):
return store.get(key) # return the earlier result
result = fn(*args, **kw)
store.put(key, result)
return result
return wrapper
Retries and failure policy
- Retry transient errors (timeouts, 429, 5xx) with exponential backoff and jitter, capped.
- Do not retry deterministic failures (validation errors, 4xx); route them to repair or to a person.
- Put failed items in a dead-letter queue with the context needed to diagnose them.
- Set a deadline for the whole run, not only for each call.
Checkpointing long jobs
For bulk work (thousands of items), checkpoint progress to durable storage after each chunk so a restart skips completed chunks. The batch-pipelines topic in the production stage builds this fully.
When to use a workflow engine
A framework checkpointer is enough for many agents. Reach for a dedicated durable-execution system (Temporal, Azure Durable Functions, a queue plus workers) when you need timers and long waits (days), exactly-once-style guarantees, high volume, or operations tooling for retries and visibility. The agent then becomes one activity inside a larger workflow.
Common mistakes
- Approval implemented as "ask the model whether this is risky".
- Retrying a non-idempotent call.
- Losing in-flight state on deploy because the run lived only in memory.
Exercises
- Add an approval gate to an agent that sends emails; kill the process while waiting and resume.
- Write a test that retries a tool after a simulated timeout and proves the action happens once.
You are done when you can…
- Pause a graph for approval, persist it, and resume it later from a different process
- Design retries that cannot duplicate side effects
- Explain when a queue or workflow engine is better than a long-lived process