Sub topic 8
Waiting for humans: timeouts and escalation
This chapter assumes you can read short Python programs, know how an agent calls tools, and understand what a background job or web request is. It also assumes you have seen an approval gate that pauses a run until a person decides.
Why waiting is a design problem
A human may answer in ten seconds, ten hours, or never. If the agent process holds a thread open while it waits, one slow reviewer can use up your workers. The workflow should save its state, return, and resume when a decision arrives. This is the asynchronous approval pattern.
The pending request needs three things:
- A unique approval ID that the reviewer's response refers to.
- The proposed tool call.
- An expiry time, so a request cannot stay open forever.
A pending store with expiry
The store below keeps pending requests in memory. A real system would use a database table with the same fields.
import time
PENDING = {} # approval_id -> {"call": ..., "expires_at": ...}
def request_approval(approval_id, call, ttl_seconds=3600):
PENDING[approval_id] = {"call": call, "expires_at": time.time() + ttl_seconds}
def resolve(approval_id, decision):
item = PENDING.pop(approval_id, None)
if item is None:
raise KeyError("Unknown or already resolved approval")
if time.time() > item["expires_at"]:
return {"status": "expired"}
return {"status": decision, "call": item["call"]}Two details matter here. pop removes the entry as it reads it, so a second click on the same link raises an error instead of running the action twice. The expiry check means a decision that arrives late is treated as expired, not as consent.
What happens on timeout
When a request expires, the agent must not run the action. Doing nothing silently is also a mistake, because the run would hang. Choose an explicit outcome:
| Timeout outcome | Behavior | Good for |
|---|---|---|
| Fail closed | Treat as rejected and tell the agent | Sending money, deleting data |
| Escalate | Send to another reviewer | Urgent actions with an owner on call |
| Cancel the run | Stop and report the reason | Tasks that lose value when late |
Never use "approve on timeout" for risky actions. A silent reviewer has not agreed to anything.
Escalation paths
Escalation moves a request to someone else when the first reviewer does not respond. A typical chain has steps with their own waits:
- Notify the primary reviewer and wait 15 minutes.
- Remind them and notify the backup reviewer. Wait 1 hour.
- Notify a team lead. Wait until the final expiry.
- Fail closed and record the outcome.
A scheduler or periodic job drives this chain. It scans pending requests, finds those past a step's deadline, and sends the next notification.
Common errors
- Checking expiry only at decision time. Requests that nobody opens never get cleaned up. Run a sweep that marks them expired and wakes the agent with a rejection.
- Letting an expired request be approved later. The reviewer may have seen stale context. Require a new request.
- No record of who was notified. Log each escalation step so you can explain delays later.
- Reusing approval IDs. Generate a random ID for each request, for example with
uuid.uuid4().
Exercise
A reviewer clicks "approve" on a link, and the same link is clicked again two minutes later. How does the resolve function above prevent the action from running twice, and what should a timeout do for a "delete_record" call?
Show answer
resolve uses PENDING.pop, which removes the entry on first use. The second click finds nothing and raises KeyError. For a high-risk call like delete_record, a timeout should fail closed: treat the request as rejected (or escalate), and never run the action.