Sub topic 9
Policy rules and safe auto-approval
This chapter assumes you can read short Python programs, know how an agent calls tools, and have seen the approval gate from earlier chapters. Here you will replace the single per-tool risk table with policy rules. Rules let some calls run without a human while the dangerous ones still stop.
Why a risk table is not enough
A risk table looks only at the tool name. But send_email to yourself is not the same as send_email to 5,000 customers. A policy rule looks at the tool name and the arguments, and returns one of three outcomes:
allow: run the call without asking.ask: pause and request human approval.deny: refuse the call, and send the reason back to the agent.
Auto-approval is just the allow outcome. It is safe only when the rules are narrow, explicit, and checked in code instead of by the model.
A small policy engine
Each rule is a function that inspects a call. It returns a decision, or None if the rule does not apply. The engine tries rules in order, and the first match wins. If nothing matches, the result is ask, so the default stays safe.
from dataclasses import dataclass
@dataclass(frozen=True)
class ToolCall:
name: str
args: dict
INTERNAL_DOMAIN = "@example.com"
def deny_prod_delete(call):
if call.name == "delete_record" and call.args.get("env") == "prod":
return "deny"
def allow_read_only(call):
if call.name == "search_docs":
return "allow"
def allow_internal_email(call):
if call.name == "send_email":
recipients = call.args.get("to", [])
if recipients and all(r.endswith(INTERNAL_DOMAIN) for r in recipients):
if len(recipients) <= 3:
return "allow"
RULES = [deny_prod_delete, allow_read_only, allow_internal_email]
def decide(call: ToolCall) -> str:
for rule in RULES:
outcome = rule(call)
if outcome is not None:
return outcome
return "ask"
print(decide(ToolCall("search_docs", {"q": "refunds"})))
print(decide(ToolCall("send_email", {"to": ["[email protected]"]})))
print(decide(ToolCall("send_email", {"to": ["[email protected]"]})))
print(decide(ToolCall("delete_record", {"id": 7, "env": "prod"})))This prints allow, allow, ask, and deny. Two details matter. The deny rule comes first, so no later allow rule can override it. The email rule needs a non-empty recipient list, so a missing to field falls through to ask.
Rules for writing safe allow rules
Follow these guidelines when you add an auto-approval rule.
- Allow by exact match, not by absence of danger. Check that every recipient ends with your domain. Do not check that none looks external.
- Put limits on size. Cap recipient counts, row counts, and amounts. A bulk action is a different risk from a single one.
- Keep deny rules first. Order is part of the policy.
- Never let the model write the rules. Rules live in code or reviewed configuration. If the agent can change them, they protect nothing.
- Check normalized values.
"[email protected]"and"[email protected]"should compare equal, and a lookalike such as[email protected]must not pass. Theendswithcheck above is deliberately strict for that reason.
Common errors
- Matching on the tool name only. The
send_emailexample shows why arguments must be part of the rule. - Defaulting to
allow. A new tool then runs unreviewed. Keepaskas the fallback. - Rules that are never reviewed. Treat a new allow rule like a code change that expands what the agent can do, and review it as one.
- Trusting content inside arguments. A recipient list that came from a web page the agent read is still untrusted. A rule that checks the list is fine, but a rule that trusts the source is not.
Auto-approved calls should still be logged. Later chapters cover the audit trail, which should record the rule that allowed each call.
Exercise
Your policy has an allow rule for send_email when all recipients end with "@example.com". An agent proposes sending to ["[email protected]"]. Does the rule allow it? Why does the rule's style of check matter?
Show answer
The rule does not allow it. The address does not end with "@example.com", so the rule returns None and the engine falls through to ask. An exact-match check on the suffix rejects lookalike domains. A check such as "example.com" in address would have allowed it.