Sub topic 13
Guardrails, permissions, and human approval
This chapter assumes you have read the agent loop and error handling chapters. You should know how run_tool dispatches calls and how the loop stops. Guardrails sit in that dispatch path. They decide whether a call the model requested is allowed to run.
Why the model cannot police itself
A model chooses tool calls based on text. That text may include user input, web pages, or file contents that contain instructions. A model can be talked into a harmful call, or it can simply make a mistake. Rules enforced in code do not depend on the model's judgment. Prompt instructions such as "never delete files" help, but they are not a control.
Three layers of control
| Layer | Question it answers | Example |
|---|---|---|
| Input validation | Are the arguments well formed and in range? | City name is a non-empty string |
| Permissions | Is this tool allowed for this agent and user? | Read-only agent cannot call `delete_file` |
| Human approval | Should a person confirm this specific call? | Sending an email or issuing a refund |
Adding the checks to run_tool
Put the checks in one place, so the model cannot bypass them. This version wraps the registry from the earlier chapter. Refusals come back as JSON text, so the model can read them and choose another step.
import json
def get_weather(city: str) -> str:
return json.dumps({"city": city, "temp_c": 18})
def send_email(to: str, body: str) -> str:
return json.dumps({"sent_to": to})
TOOLS = {"get_weather": get_weather, "send_email": send_email}
ALLOWED = {"get_weather", "send_email"} # per-agent allowlist
NEEDS_APPROVAL = {"send_email"} # risky tools
def validate(name: str, arguments: dict) -> str | None:
if name == "get_weather":
city = arguments.get("city")
if not isinstance(city, str) or not city.strip():
return "city must be a non-empty string"
return None
def ask_human(name: str, arguments: dict) -> bool:
answer = input(f"Allow {name} with {arguments}? [y/N] ")
return answer.strip().lower() == "y"
def run_tool(name: str, arguments: dict) -> str:
if name not in TOOLS:
return json.dumps({"error": f"unknown tool: {name}"})
if name not in ALLOWED:
return json.dumps({"error": f"tool not permitted: {name}"})
problem = validate(name, arguments)
if problem:
return json.dumps({"error": f"invalid arguments: {problem}"})
if name in NEEDS_APPROVAL and not ask_human(name, arguments):
return json.dumps({"error": "denied by human reviewer"})
try:
return TOOLS[name](**arguments)
except Exception as exc:
return json.dumps({"error": str(exc)})The agent loop does not change. A denial is an observation, like any other result.
Common mistakes
- Checking permissions in the prompt only, with no code check.
- Approving a vague summary. Show the reviewer the exact tool name and arguments.
- Requiring approval for every call. Reviewers then approve without reading. Reserve it for actions that are hard to undo, cost money, or reach outside your system.
- Giving tools broader access than the task needs, such as a database account with write rights for a read-only agent.
Practical rules
- Default to deny. Add a tool to the allowlist deliberately.
- Grant the narrowest credentials the tool needs.
- Log every refusal and approval so you can trace them later.
Exercise
An agent has a delete_record tool. The system prompt says "Only delete records when the user asks." Which controls would you add in code, and where?
Show answer
Put delete_record on the approval list so a person sees the exact record ID before it runs. Validate that the ID is well formed and refers to a record the user may modify. Add a permission check so only agents and users with delete rights can call it. Do all of this in run_tool, and return refusals as error text so the model can respond.