Sub topic 16
Human approval and escalation
This chapter assumes you have read the chapters on handoffs, orchestrators, turn limits, and trust boundaries. You should be comfortable with the Agent and run code from earlier. Here you add a person to the loop: first as an approver for risky actions, then as the destination when the agents cannot finish the job.
Why a human belongs in the loop
Agents make mistakes, and a delegated agent can act on instructions that came from untrusted text. Some actions are hard to undo: refunds, emails, deletions, deployments. For those, a model's judgment is not enough. A human checkpoint limits the damage of a wrong decision.
There are two separate uses, and it helps to keep them apart:
- Approval: the workflow can continue, but a person must say yes before one specific action runs.
- Escalation: the workflow cannot or should not continue, so it stops and hands the case to a person.
Approval gates on tools
Put the gate in the tool, not in the prompt. An instruction such as "ask before refunding" can be ignored or talked around. Code that refuses to run without an approval cannot.
from typing import Callable
class ApprovalRequired(Exception):
def __init__(self, action: str, args: dict):
super().__init__(f"Approval required for {action}")
self.action = action
self.args = args
def issue_refund(order_id: int, amount: float) -> str:
return f"Refunded {amount:.2f} for order {order_id}"
def gated(tool: Callable, needs_approval: Callable[..., bool], name: str) -> Callable:
def wrapper(approved: bool = False, **args):
if needs_approval(**args) and not approved:
raise ApprovalRequired(name, args)
return tool(**args)
return wrapper
refund = gated(issue_refund, lambda order_id, amount: amount > 100, "issue_refund")
print(refund(order_id=1042, amount=40.0))
try:
refund(order_id=1042, amount=250.0)
except ApprovalRequired as e:
print("Paused:", e.action, e.args)
print(refund(order_id=1042, amount=250.0, approved=True))The wrapper sets the threshold in code. The model never supplies approved. Your application sets it after a person responds. In the loop from earlier chapters, catch ApprovalRequired where the tool is called, save the state, and return control to the caller instead of treating it as a failure.
Pausing and resuming
An approval can take minutes or days, so do not block a process waiting for it. Save the message list, the current agent's name, and the pending action. When the person answers, reload that state and continue.
On rejection, return the refusal as a tool result such as "Refund declined by reviewer: reason". The agent can then explain the outcome or choose another path.
Escalation
Escalate when the agents hit a limit they cannot fix. Common triggers:
- The turn limit is reached or a loop is detected.
- A specialist fails repeatedly after retries.
- The request is outside every agent's scope.
- A guardrail flags suspicious or sensitive content.
- The customer asks for a person.
Treat escalation as a handoff whose target is a human queue. Use the structured payload from the context chapter so the person does not reread the whole conversation.
Common errors
- Gating by prompt instead of in code.
- Approving a vague request, then executing different arguments. Bind the approval to the exact arguments.
- Asking for approval so often that reviewers click yes without reading. Gate only actions that are costly or irreversible.
- Escalating without context, so the human starts from zero.
Exercise
A refund tool is gated at amounts over 100. A reviewer approves a refund of 250 for order 1042. Before the tool runs, the agent changes the amount to 2500. What design change prevents this, and why?
Show answer
Bind the approval to the exact action and arguments, for example by storing the pending call and executing only that stored call, or by comparing the arguments to the approved ones before running. Then an approval for 250 cannot be reused for 2500.