Sub topic 16
Human approval and escalation
This chapter adds a person to a multi-agent workflow. It covers two cases. In an approval, the system pauses before a risky action and waits for a yes or no. In an escalation, the agents stop working on a task and pass it to a human. It assumes you have read the earlier chapters on handoffs, orchestrators, turn limits, and trust boundaries. You should also be comfortable with Python dataclasses and JSON.
Why agents need a human checkpoint
Turn limits and retries stop a run from looping forever. They do not stop a run from doing the wrong thing quickly. An agent that issues refunds, sends email, or changes a database can make an error that cannot be undone. Multi-agent systems make this worse in two ways:
- A specialist agent acts on a task description written by another agent. If the orchestrator misread the user, the specialist carries out the misreading with full confidence.
- The person who started the run is usually not watching. Nobody notices a bad intermediate step until the final result arrives.
A human checkpoint is a deliberate pause at the point where the cost of a mistake is higher than the cost of waiting.
Approval versus escalation
The two mechanisms look alike but answer different questions.
| Row | Approval | Escalation |
|---|---|---|
| Question | "May I do this specific action?" | "I cannot finish this. Who should?" |
| Trigger | A tool call that matches a risk policy | Low confidence, repeated failure, a user request, or a limit reached |
| Human's choices | Approve, reject, or edit | Take over, give guidance, or close the task |
| After the human acts | The same run usually resumes | The agents often stop, and the human owns the case |
Approval is a gate on a tool call. Escalation is closer to a handoff whose target is a person or a queue instead of another agent.
Where to put the gate
Put the gate in the code that executes tools, not in the agent's instructions. A line in a prompt such as "ask before refunding over $100" is a request the model may ignore. A check in the tool-execution function cannot be ignored, because the tool does not run unless the check passes.
This also fits the trust boundaries from the previous chapter. The gate sits outside every agent's control, so a manipulated agent cannot talk its way past it.
A gate in code
The following code is a complete, runnable gate. It wraps tool execution, asks a human reviewer when the policy flags a call, and returns a plain string in every case so the agent loop can continue.
import hashlib
import json
from dataclasses import dataclass
from typing import Callable, Optional
@dataclass
class ApprovalRequest:
agent: str
tool: str
args: dict
reason: str
def fingerprint(self) -> str:
raw = json.dumps({"tool": self.tool, "args": self.args}, sort_keys=True)
return hashlib.sha256(raw.encode()).hexdigest()
def guarded_call(
agent_name: str,
tools: dict[str, Callable],
name: str,
args: dict,
ask_human: Callable[[ApprovalRequest], Optional[dict]],
policy: Callable[[str, dict], Optional[str]],
) -> str:
reason = policy(name, args)
if reason is None:
return str(tools[name](**args))
request = ApprovalRequest(agent_name, name, args, reason)
answer = ask_human(request)
if answer is None:
return "DENIED: no human response before the timeout. Do not retry. Escalate."
if answer["decision"] == "approve":
if answer.get("fingerprint") != request.fingerprint():
return "DENIED: approval does not match the requested action."
return str(tools[name](**args))
if answer["decision"] == "edit":
return str(tools[name](**answer["args"]))
return f"REJECTED: {answer.get('note', 'no reason given')}"
def issue_refund(order_id: int, amount: float) -> str:
return f"Refunded {amount:.2f} for order {order_id}"
def policy(name: str, args: dict) -> Optional[str]:
if name == "issue_refund" and args["amount"] > 100:
return "refund above the auto-approval limit of 100"
return None
def reviewer(request: ApprovalRequest) -> Optional[dict]:
print("Review needed:", request.reason, request.args)
return {
"decision": "edit",
"args": {"order_id": request.args["order_id"], "amount": 100.0},
"note": "cap at the limit",
}
tools = {"issue_refund": issue_refund}
print(guarded_call("billing", tools, "issue_refund",
{"order_id": 1042, "amount": 40.0}, reviewer, policy))
print(guarded_call("billing", tools, "issue_refund",
{"order_id": 1042, "amount": 250.0}, reviewer, policy))Running it prints Refunded 40.00 for order 1042. The second call prints the review line, then Refunded 100.00 for order 1042, because the reviewer edited the amount.
Several details in this code matter:
- The fingerprint binds an approval to one action. A hash of the tool name and arguments means a stored approval cannot be reused for a different call. This matters when approvals wait in a queue for hours.
- A missing answer is a denial. If the reviewer never responds, the safe default is to not act. The message tells the agent not to retry, so it does not spend turns asking again.
- Rejections are returned as tool results. The agent sees the reason and can adapt. It might offer the customer a smaller refund or hand the case off.
- The policy sees the real arguments. It checks the values that will execute, not the model's description of them.
What the reviewer should see
An approval is only as good as the information shown. Show the exact tool name and arguments, the reason the policy fired, and a short summary of the goal. A structured payload like the one from the context-passing chapter works well here. Do not show only the agent's own explanation of what it is doing. A confused or manipulated agent produces a confident explanation that hides the problem. When the arguments contain text that came from outside, such as a customer message or a web page, display it as quoted data.
Pausing and resuming
A reviewer may take minutes or days to respond. Holding a Python function open that long is fragile. For anything slower than a terminal prompt, the run needs to be saved and resumed:
- When the gate fires, save the message history, the active agent's name, the pending tool call, and the fingerprint.
- End the current process and notify the reviewer.
- When the decision arrives, load the saved state and check the fingerprint.
- Execute or reject the call, append the result as a tool message, and continue the loop.
In an orchestrator design, decide which agent owns the pause. If a specialist hits a gate, the usual choice is to pause the whole run and keep the specialist's state with it. Returning "waiting for approval" as a string result leaves the orchestrator to guess what to do next.
Escalation to a person
Escalation is a handoff whose target is a human queue. Common triggers include:
- The user asks for a person.
- The turn limit or retry budget is reached.
- Two specialists return conflicting results that the orchestrator cannot resolve.
- A guardrail detects something outside the system's scope, such as a legal threat.
An escalation should carry a summary, not a transcript. Reuse the handoff payload: goal, facts established, open questions, and constraints. Add what was already tried, so the person does not repeat failed steps. State clearly to the user that the case is moving to a human, and do not let an agent keep improvising in parallel with the person.
Design rules
Choose gates by the cost of the action, not by the agent that proposes it.
| Action type | Suggested gate |
|---|---|
| Read-only lookups | None |
| Reversible writes, such as drafts or tags | Log only |
| Costly or hard-to-reverse actions, such as payments or sent email | Approval before running |
| Destructive or legal actions | Approval by a named role, with an audit record |
Record every request and decision in your traces. They show how often the gate fires, how often humans edit or reject, and which agents trigger it most.
Common mistakes
- Gating in the prompt only. The model can skip an instruction. Enforce the gate in code.
- Gating the orchestrator but not the specialists. A specialist with a payment tool needs its own gate, because the orchestrator never sees the call.
- Approving a summary. The human must approve the arguments that will run.
- Retrying a denied call. Without the "do not retry" signal, an agent may rephrase the request and ask again, using up turns and annoying reviewers.
- Escalating without context. A person who receives only "customer is upset" must start over.
Exercise
In the code above, the reviewer returns {"decision": "approve", "fingerprint": ...} for a 250.00 refund request. Before the tool runs, an agent bug changes the amount in the pending call to 2500.00. What does guarded_call return, and why? Then name one additional change you would make if approvals waited in a queue for several hours.
Show answer
It returns "DENIED: approval does not match the requested action." The fingerprint is a hash of the tool name and arguments, so a changed amount produces a different hash from the one the reviewer approved. For long waits, save the run state (message history, active agent, pending tool call, fingerprint) to durable storage and resume from it when the decision arrives. Also add an expiry time to the stored request, so an old approval cannot be used long after the situation changed.