Keep a task safe across pauses, approvals and failures
Durable agents persist progress so a task can survive restarts, wait for an external event or human decision, and resume from a checkpoint without repeating completed side effects. Human-in-the-loop checkpoints pause automation when risk, uncertainty, or policy requires an authorized person to approve, reject, edit, or clarify the next action.
What You Will Learn
- Persist checkpoints so work survives restarts and long waits.
- Pause on events instead of holding a request open.
- Design meaningful human approval with evidence and choices.
- Resume only after expiry, authorization, and external state are revalidated.
Waiting is a stored state, not a sleeping request
A task that may wait hours for a person or external event should save a checkpoint, release the worker, and resume from a queue, schedule, or event. Keeping one web request or process open wastes resources and is easily lost during restarts.
An approval can expire or be cancelled. Before resuming a consequential action, reload the checkpoint, confirm that the reviewer still has authority, verify the decision has not expired, and recheck prices, permissions, inventory, or other external conditions that may have changed while the task waited.
Checkpoint, wait and resume from recorded state
Persist typed task state and a checkpoint after meaningful work, including receipts for completed side effects.
Use queues, schedules, or events instead of keeping one request alive while waiting for a reply, approval, or external system.
Pause before high-risk or irreversible actions and show the reviewer the evidence, proposed action, consequences, and permitted choices.
Record approval, rejection, edits, clarification, expiry, cancellation, and timeout as explicit events in the task state.
Resume from the saved checkpoint, verify authorization and current conditions again, and use idempotency keys so completed actions are not repeated.
Instructional figure
A durable task can pause and safely resume
What must be saved before a task waits for a human decision?
Designing useful human intervention
Worked Example: Resume a supplier payment without replaying stale authority
Task PAY-204 pauses with quote Q-17 for $4,800, reviewer requirement 'finance', approval A-51 expiring at 17:00, attempt count 0, and idempotency key PAY-204 stored in checkpoint version 3.
- Pause: the workflow persists the checkpoint and releases its worker while a finance reviewer inspects the quoted amount and evidence.
- Wait: approval A-51 arrives, but the task resumes after 17:00. The supplier has also replaced Q-17 with Q-22 for $5,050.
- Revalidate: the runtime reloads version 3, re-authenticates requester U-17, confirms reviewer F-09's current authority, detects the expired approval and changed quote, and refuses to execute the old proposal.
- Resume: reviewer F-09 approves the revised amount as A-62. The payment executes with the original logical key PAY-204; receipt P-880 is recorded and any retry returns that receipt instead of paying twice.
What the result means: Durability preserves progress, not stale permission. Version 4 records the fresh quote, revised approval, one execution attempt, and receipt, making the eventual side effect auditable and exactly-once from the workflow's perspective.
Checkpoint before pause and authoritative state after resume
Instructional figure
Checkpoint, wait, revalidate, then resume exactly once
Why can an old approval not be replayed after external state changes?
A stale approval never authorizes the changed action; the checkpoint records both decisions and the one execution receipt.
A Realistic Example: A document review that pauses for an authorized decision
The agent reviews many documents and checkpoints each completed result. A sensitive finding pauses with supporting evidence for a reviewer. After approval or a process restart, the worker resumes at the first unfinished document and does not resend notifications already recorded as delivered.
Read the Mechanism in Code
Use the label and note to understand what this block represents before reading each line.
state = load_checkpoint(task_id)
for document in pending_documents(state):
result = review(document)
save_result(task_id, document.id, result)
if result.requires_human_decision:
checkpoint(task_id, status="waiting_for_review")
return approval_queue.submit(result, required_role="reviewer")
checkpoint(task_id, completed=document.id)Hands-On Runnable Python Lab: Reload a SQLite Checkpoint and Reject a Changed Quote
This example writes a real SQLite file inside an automatically cleaned temporary directory, closes the connection, then opens it again to represent a replacement worker. The original approval covers $4,800 and expires at time 100.
- Store the original pending payment and approval in SQLite, then close the first connection.
- Reopen the same file and reload the saved state to prove it survives the connection lifecycle.
- Compare the old approval with time=110 and the new 5,050 quote. Deny execution.
- Record a revised approval valid until time 200. Revalidation now permits execution, but the example still performs no real payment.
Saved progress survives the worker; old approval does not authorize new terms
The dashed divider separates worker lifetimes. SQLite carries the checkpoint across them, but the replacement worker must revalidate time and amount.
import json
import sqlite3
from pathlib import Path
from tempfile import TemporaryDirectory
def authorized(record, now, current_amount):
return (now < record["expires"]
and record["approved_amount"] == current_amount)
with TemporaryDirectory() as folder:
path = Path(folder) / "checkpoint.db"
record = {"task": "PAY-204", "stage": "WAIT_APPROVAL",
"approved_amount": 4800, "expires": 100, "key": "PAY-204"}
db = sqlite3.connect(path)
db.execute("CREATE TABLE checkpoints (id TEXT PRIMARY KEY, payload TEXT)")
db.execute("INSERT INTO checkpoints VALUES (?, ?)",
(record["task"], json.dumps(record)))
db.commit()
db.close()
db = sqlite3.connect(path)
saved = db.execute("SELECT payload FROM checkpoints WHERE id = ?",
("PAY-204",)).fetchone()[0]
restored = json.loads(saved)
print("restored:", restored["task"], restored["stage"])
current_amount, now = 5050, 110
print("old approval permits write:", authorized(restored, now, current_amount))
restored.update(approved_amount=5050, expires=200)
db.execute("UPDATE checkpoints SET payload = ? WHERE id = ?",
(json.dumps(restored), restored["task"]))
db.commit()
print("renewed approval permits write:", authorized(restored, now, current_amount))
print("logical operation key:", restored["key"])
db.close()
print("real payments executed: 0")
restored: PAY-204 WAIT_APPROVAL
old approval permits write: False
renewed approval permits write: True
logical operation key: PAY-204
real payments executed: 0What This Run Shows
- The restored values come from disk, not a dictionary shared by two functions.
- Changing a quote does not silently change the old approval; the revised record represents a separate reviewer decision.
- The logical operation key is retained because no payment was executed before the pause. Once a key has executed, it must not be reused for a different payload.
Recovery is successful only when progress and authority are both correct. A resumed agent should be able to explain what was restored, what was rechecked and why the next action is still permitted.
From the Exercise to an Application
Experiment: Set now=90 but leave the current quote at 5,050.
The old approval is still denied. Being unexpired is necessary but insufficient: it covers 4,800, not the new amount.
Summary / Key Takeaways
- Durable agents persist task progress so work can survive process restarts, long waits, and external events.
- Checkpoints should capture typed state and receipts for completed side effects so resume does not repeat finished actions.
- Waiting should be event-driven rather than keeping a request or model call alive indefinitely.
- Human-in-the-loop checkpoints belong before high-risk, irreversible, low-confidence, or policy-sensitive actions.
- A reviewer needs evidence, proposed consequences, authority, and meaningful approve/reject/edit choices—not a decorative approval button.
- After a pause, the system should revalidate authorization and current conditions before continuing.
- Timeouts, expiry, cancellation, idempotency, and audit events are part of durable execution, not optional operational details.
Verified Further Reading
These primary research papers and current official specifications support the concepts and implementation guidance in this lesson. The explanations, analogies, exercises, and code above remain original teaching material.
LangGraph: Persistence
Official guidance on checkpoints, resuming runs, and durable execution.
CrewAI: Introduction
Current framework documentation describing agents, crews, flows, state, and execution control.
OpenAI API: Conversation State
Explains conversation persistence, context-window accounting, input/output/reasoning tokens, and truncation risks.