Sub topic 4
Building an approval gate
This chapter assumes you can read short Python programs, know how an LLM agent calls tools, and understand JSON and dictionaries. It builds on the previous chapter, where each tool call received a risk level.
What a gate does
An approval gate is a checkpoint between the agent's decision to call a tool and the tool's execution. The agent proposes an action. The gate decides whether the action runs now, waits for a person, or is refused.
The gate has three steps:
- Pause: stop before the tool runs and hold the proposed call.
- Decide: ask a human, or skip this step when the risk is low.
- Resume: run the tool, run an edited version, or report the rejection back to the agent.
Representing a call and its risk
The gate needs a plain object for a proposed call. A frozen dataclass works well, because a frozen object cannot be changed after the reviewer sees it.
from dataclasses import dataclass
@dataclass(frozen=True)
class ToolCall:
name: str
args: dict
RISK = {
"search_docs": "low",
"send_email": "high",
"delete_record": "high",
}
def risk_of(call: ToolCall) -> str:
# Unknown tools default to high risk, so new tools need approval until classified.
return RISK.get(call.name, "high")The default in risk_of matters. If someone adds a tool and forgets to classify it, the gate asks for approval. A missing entry then costs a little time instead of causing an unreviewed side effect.
The gate itself
def run_step(call, tools, ask_human):
if risk_of(call) == "low":
return tools[call.name](**call.args)
decision = ask_human(call) # returns a dict such as {"action": "approve"}
action = decision["action"]
if action == "approve":
return tools[call.name](**call.args)
if action == "edit":
edited = ToolCall(call.name, decision["args"])
return tools[edited.name](**edited.args)
return {"rejected": True, "reason": decision.get("reason", "")}Walk through the three paths:
- Low risk: the tool runs with no human involved.
- Approve: the tool runs with the original arguments.
- Edit: the reviewer supplies new arguments, and the tool runs with those. The name stays the same, so a reviewer cannot turn
send_emailinto a different tool. - Anything else: the gate treats the decision as a rejection. It returns the reason as a normal result, so the agent can read it and change its plan.
Notice the last line. An unrecognized action, such as a typo like "aprove", is rejected and not run. Failing closed is the right behavior for a gate.
Trying it
sent = []
tools = {
"search_docs": lambda query: f"results for {query}",
"send_email": lambda to, body: sent.append((to, body)) or "sent",
}
def reviewer(call):
print("Review:", call.name, call.args)
return {"action": "edit", "args": {"to": "[email protected]", "body": call.args["body"]}}
print(run_step(ToolCall("search_docs", {"query": "refunds"}), tools, reviewer))
print(run_step(ToolCall("send_email", {"to": "[email protected]", "body": "Hi"}), tools, reviewer))
print(sent)The search runs without a prompt. The email call reaches the reviewer, who narrows the recipient before it goes out. The sent list shows the edited address, not the one the agent proposed.
Common errors
- Checking risk by tool name only.
delete_recordon a test row and on a customer account share a name but not a risk. If arguments change the risk, classify on both. - Running the tool inside the approval callback. Keep deciding and executing separate, so the gate stays the only path to execution.
- Letting exceptions mean "approved". If
ask_humanfails or times out, do not run the call. - Trusting an edit without checking it. Edited arguments should pass the same validation as the agent's own.
The gate above blocks the whole process while it waits. Later chapters cover saving state so the wait can last hours, and binding an approval to the exact action.
Exercise
In run_step, a reviewer returns {"action": "aprove"} by mistake. What happens, and why is that the right behavior?
Show answer
The call falls through to the final return and is treated as a rejection, so the tool does not run. Failing closed means an unclear decision never causes a side effect, and the agent receives a rejection it can react to.
Exercise
A new tool called transfer_funds is added to the agent but not added to the RISK table. Does it need approval?
Show answer
Yes. risk_of returns "high" for unknown names, so the call goes to the human until someone classifies the tool.