Tutorial / intermediate
Build a Python approval gate for agent tool calls
Validate a proposed action and bind approval to its exact arguments in a local mock, with explicit limits on identity, storage and delivery.
This guide is currently available only in English.

A model proposes sending a notification to the operations team. Before the application acts, it needs to check the destination, message and approval. The proposal is not an authorization: a tool call can have the right structure and still request the wrong action. The model may have misunderstood the request, or a retrieved document may have tried to redirect it.
Build that check in Python with a mock notification tool. It records a fictional notice in memory and never sends email or touches a network. The example uses only Python’s standard library, so you can inspect and test the approval flow without an account or API key.
By the end, you can explain and test four decisions independently: which action is permitted, whether its arguments are allowed, who requested it, and whether an independent approval applies to this exact proposal. This is a teaching example, not a production authorization service or a claim that prompt injection has been solved.
What the model can propose—and what it cannot decide
In both Claude client tools and OpenAI function calling, a model can return structured tool requests. For application-executed tools, the host application decides whether to run the operation. Structured arguments help parsing; they do not prove that the action is safe or authorized.
OWASP’s Excessive Agency guidance describes why overly broad tool access can amplify model errors or manipulated instructions. The defensive boundary belongs in application code, not only in the system prompt.
Our example has two inputs with different trust levels:
- Untrusted: the proposed tool name, recipient and message returned by a model (or typed into this demo).
- Trusted in a real deployment: the authenticated requester’s identity and a separate, authenticated reviewer action. This tutorial simulates those trusted contexts; it does not implement login or role verification.
The diagram shows the sequence. The reviewer authorizes a particular proposal fingerprint; the model cannot grant its own approval.
The same diagram is provided in a stacked mobile layout. It is an explanatory schematic, not a screenshot or evidence of a deployed agent.
Start with a narrow action
We will allow exactly one tool, record_notice, and one fictional destination, ops@example.invalid. The .invalid domain is intentionally not a real mailbox. This is an allowlist for the demo, not a complete authorization policy.
Create a temporary directory and save the following as gate.py. Use Python 3.11 or newer; no packages or API keys are required.
# gate.py — educational in-memory mock, NOT a production auth servicefrom dataclasses import dataclassfrom hashlib import sha256import jsonimport secrets
ALLOWED_RECIPIENTS = {"ops@example.invalid"}MAX_MESSAGE_LENGTH = 200
@dataclass(frozen=True)class Proposal: tool: str arguments: dict[str, str]
def validate(proposal: Proposal) -> None: if proposal.tool != "record_notice": raise PermissionError("Tool is not allowed") args = proposal.arguments if not isinstance(args, dict) or set(args) != {"recipient", "message"}: raise ValueError("Unexpected tool arguments") if type(args["recipient"]) is not str or type(args["message"]) is not str: raise ValueError("Arguments must be strings") if args["recipient"] not in ALLOWED_RECIPIENTS: raise PermissionError("Recipient is not allowed") if not 1 <= len(args["message"]) <= MAX_MESSAGE_LENGTH: raise ValueError("Message length is invalid")
def fingerprint(proposal: Proposal) -> str: # Validation also ensures canonical, bounded fields. validate(proposal) payload = {"tool": proposal.tool, "arguments": proposal.arguments} canonical = json.dumps(payload, sort_keys=True, separators=(",", ":")) return sha256(canonical.encode("utf-8")).hexdigest()
class ApprovalGate: def __init__(self) -> None: self._approvals: dict[str, tuple[str, str]] = {} self.mock_log: list[dict[str, str]] = []
def approve( self, proposal: Proposal, requester_id: str, reviewer_id: str ) -> str: # Both identities MUST come from authenticated server context in reality. if not requester_id or not reviewer_id or requester_id == reviewer_id: raise PermissionError("Separate reviewer required") digest = fingerprint(proposal) token = secrets.token_urlsafe(24) self._approvals[token] = (requester_id, digest) return token
def execute( self, proposal: Proposal, requester_id: str, approval_token: str ) -> dict[str, str]: # Reject malformed or unauthorized proposals before any side effect. digest = fingerprint(proposal) approved = self._approvals.get(approval_token) if approved != (requester_id, digest): raise PermissionError("Missing, reused, or mismatched approval") del self._approvals[approval_token] # Mock-only effect: NO email, filesystem write, HTTP request or shell. entry = { "recipient": proposal.arguments["recipient"], "message": proposal.arguments["message"], } self.mock_log.append(entry) return entryWhy a fingerprint? A reviewer may approve “Send the deployment summary to the operations mailbox,” but the model could later propose a different recipient or message. Binding approval to a canonical representation of the exact proposal detects that change. The SHA-256 digest here is an equality key, not a signature or authentication mechanism; approval security depends on the trusted reviewer context and protected approval store.
Why consume approval first? Replaying the same token cannot create a second log entry in this single-process example. But a real action can fail after token consumption; recovery needs a durable transaction/outbox and idempotency keys, not an in-memory dictionary.
Exercise the refusal paths
Save the next complete snippet as test_gate.py in the same directory.
# test_gate.pyimport unittestfrom gate import ApprovalGate, Proposal
def request(message="Deploy completed", recipient="ops@example.invalid"): return Proposal("record_notice", {"recipient": recipient, "message": message})
class ApprovalGateTests(unittest.TestCase): def setUp(self): self.gate = ApprovalGate()
def test_approved_proposal_is_recorded_once(self): proposal = request() token = self.gate.approve(proposal, "requester", "reviewer") result = self.gate.execute(proposal, "requester", token) self.assertEqual(result["recipient"], "ops@example.invalid") self.assertEqual(len(self.gate.mock_log), 1) with self.assertRaises(PermissionError): self.gate.execute(proposal, "requester", token)
def test_model_cannot_execute_without_approval(self): with self.assertRaises(PermissionError): self.gate.execute(request(), "requester", "invented-token") self.assertEqual(self.gate.mock_log, [])
def test_edited_message_invalidates_approval(self): token = self.gate.approve(request(), "requester", "reviewer") with self.assertRaises(PermissionError): self.gate.execute(request("Delete the backups"), "requester", token) self.assertEqual(self.gate.mock_log, [])
def test_other_requester_cannot_reuse_approval(self): proposal = request() token = self.gate.approve(proposal, "requester", "reviewer") with self.assertRaises(PermissionError): self.gate.execute(proposal, "someone-else", token)
def test_recipient_allowlist(self): with self.assertRaises(PermissionError): self.gate.approve(request(recipient="outside@example.invalid"), "requester", "reviewer")
def test_unknown_tool(self): proposal = Proposal("delete_files", {"recipient": "ops@example.invalid", "message": "Oops"}) with self.assertRaises(PermissionError): self.gate.approve(proposal, "requester", "reviewer")
def test_separate_reviewer(self): with self.assertRaises(PermissionError): self.gate.approve(request(), "requester", "requester")
def test_unexpected_argument(self): proposal = Proposal("record_notice", { "recipient": "ops@example.invalid", "message": "Hi", "approved": "yes", }) with self.assertRaises(ValueError): self.gate.approve(proposal, "requester", "reviewer")
if __name__ == "__main__": unittest.main()Run:
python --versionpython -m unittest -v test_gate.pyObserved on 23 October 2026: the exact two Python snippets above passed all eight tests with Python 3.12.14 on Linux, ending in OK. Six additional isolated checks rejected invalid lengths and types, preserved approval after a requester mismatch, and refused a mutated proposal. These checks exercised only the in-memory mock; no model API, authentication service or notification delivery was tested.
Try changing message after approval. The digest changes, and the gate refuses the operation. Try changing the tool name or recipient. Those requests are refused even before approval. Notice that the tests exercise application controls, not whether a real language model follows instructions.
Connecting a real tool-calling API
The model provider’s tool-call response would be parsed into a Proposal. Your server—not the model—supplies requester_id from the authenticated session. It validates the proposed tool name and arguments, stores a pending request for a reviewer, and displays exactly what will happen. A separate authenticated reviewer approves that immutable request. Only then does the server execute the approved action through a narrowly scoped adapter.
Do not pass reviewer_id or approval_token as model-controlled arguments. In this local example, they are plain function parameters so the flow is easy to inspect; a real service must prevent callers from impersonating a reviewer, approving their own requests, or bypassing the gate by calling the tool adapter directly. Model-produced JSON that says "approved": true is just more untrusted text.
This example also does not implement approval expiry, tenant isolation, rate limiting, concurrency control, replay protection across processes, crash recovery, a durable audit trail, or real notification delivery. Those are engineering requirements, not optional polish, for consequential actions.
Failure modes worth testing next
3 columns. Scroll horizontally if needed. Keyboard: focus the table, then use ← and →.
| Failure | This mock | Production control to add |
|---|---|---|
| Model requests an unknown tool | Denied by exact tool allowlist | Per-user, per-tool permissions and service-scoped credentials |
| Prompt injection changes recipient | Denied by recipient allowlist | Trusted destination policy; keep retrieved text out of authorization |
| Arguments change after approval | Denied by fingerprint mismatch | Durable, immutable request record and approval binding |
| Approval token is replayed | Denied after first consumption in one process | Atomic consume + durable idempotency across workers |
| Reviewer identity is spoofed | Not prevented by this mock | Authenticated independent reviewer, CSRF protection and audit |
| External action succeeds but response fails | Not handled | Idempotent downstream API, outbox and recovery procedure |
OWASP’s prompt-injection prevention guidance recommends validating tool calls and enforcing least privilege. This boundary limits what an untrusted model proposal can do; it does not guarantee that the model will not produce manipulated content or that an approved action is desirable.
Security, privacy, cost and alternatives
The example makes no API requests and stores only fictional messages in memory, so it requires no paid service. In production, do not log full prompts, secrets or personal data by default. Store the minimum necessary approval metadata, restrict who can view it, and set a retention policy. Use a dedicated, minimally privileged identity for each external action; never give a model direct access to a broadly privileged API key.
A simpler alternative is to expose read-only tools and omit write tools entirely. Another is a queue of proposed actions that a human performs manually. For irreversible or high-impact actions, those designs may be safer than an automatic executor—even one with approval checks.
If you want to compare agent products’ documented permission controls rather than build a gate, see the guide to coding-agent access, isolation and recovery. This tutorial answers a different question: how does your own application enforce a boundary around one proposed action?
Before you call it production-ready
Run the tests in a clean Python environment. Review the code for malformed inputs and race conditions. Replace the simulated identities with real server-side authentication, the in-memory approvals with an atomic durable store, and the mock log with an idempotent, least-privileged adapter. Add integration tests for concurrent approvals, expired tokens, partial failures, compromised tool responses and real user permissions. Verify the exact model API’s tool-call parsing against its current documentation.
Verification boundary (23 October 2026): The exact Python snippets passed eight unit tests plus six isolated boundary checks. Provider integration, authenticated approval, concurrency, persistence and production behavior have not been verified. The verification label remains researched: successful mock execution is not evidence that a real agent or production authorization service is secure.
VERIFICATION