Lesson 10.2: Tool gateways, policy and approval gates
Learn how a tool gateway checks policy in code, applies limits, asks for approval and records every agent action, so that rules do not rest on the prompt.
By the end of this lesson you will be able to explain what a tool gateway is and why every action should pass through one, write policy as rules in code instead of text in a prompt, choose among allowing, denying and asking for approval, apply limits such as rates and amounts, plan the load that approvals put on people, and see how a gateway bounds the damage of a runaway agent. You will work out that 10,000 tool calls a day with 6 per cent needing approval keep 3.3 reviewers busy, while a better-tuned policy with 2 per cent needing approval needs 1.1, and that a runaway agent making 120 refund calls a minute for 30 minutes before it is stopped can cause ₹14.4 lakh of harm, against ₹4,000 when a gateway caps calls at 20 an hour. You should have read Lessons 6.1, 6.3, 7.2 and 10.1. No code is needed. The mathematics is multiplication and division.
Lesson 7.2 argued that the important limits on an agent must live where the model cannot undo them. Lesson 10.1 introduced the harness as the structure that holds those limits. This lesson builds the part that does the most work: the tool gateway. It is the single doorway through which every action an agent takes must pass, and it is where policy is enforced.
The problem with scattered controls
In many systems, the controls on an agent's actions are spread about. One tool checks a limit. Another does not. A third was written by a different team and trusts whatever it is given. The system prompt says "never do X" and the model usually listens. Some actions are logged and some are not. When someone asks "what can this agent do, and what did it do last Tuesday?", nobody can answer without reading every tool.
This is fragile in three ways.
Inconsistency. Each tool implements its own idea of a limit, so the rules are uneven and some are missing.
Invisibility. There is no one place to see all the actions, or all the rules.
Drift. A new tool is added and nobody remembers to add the checks. The weakest tool sets the real safety of the system.
The remedy is to put one layer between the agent and every tool, and to require that all calls go through it.
What a tool gateway is
A tool gateway is a layer of code that sits between the model and the tools it can use. The model never calls a tool directly. It proposes a call, and the harness sends that proposal to the gateway. The gateway decides what happens, and, if the call is allowed, makes it and returns the result.
Think of the way a bank handles a payment instruction. The customer does not reach into the ledger. The instruction goes through a payments gateway that authenticates the customer, checks the limits, screens for fraud, asks for confirmation where needed, records everything and only then touches the books. An agent's tool gateway does the same job for actions.
A gateway does about ten things.
Function · What it doesIdentify · Knows which agent, which task and which user's authority the call is made under
Authorise · Checks that this agent may use this tool for this purpose
Validate · Checks the arguments against the tool's schema and sensible ranges
Apply policy · Evaluates the rules and decides: allow, deny or ask
Limit · Enforces rates, quotas and budgets
Request approval · Pauses the call and sends it to a person when the policy says so
Make idempotent · Attaches a key so a repeated call does no harm
Execute · Calls the real tool with credentials the model never sees
Sanitise · Trims, redacts and structures the result before it goes back to the model
Record · Logs the call, the decision, the reason and the result with the trace ID
Because everything passes through, a change to a rule takes effect for every tool at once, and every action is visible in one log.
Credentials stay behind the gateway
The gateway holds the credentials, or fetches them on demand for the right identity. The model sees only the tool's name and the arguments it chooses. This keeps secrets out of the context, as Lesson 6.1 required, and means that even a fully hijacked model has nothing to steal and no direct line to a system.
Dry runs
A useful feature is a dry-run mode. For many actions the gateway can ask the tool to say what it would do without doing it: "this would refund ₹2,400 to the card ending 4417 and close the ticket". The result is shown to the model, or to a person, and the real call goes ahead only after it is confirmed. Dry runs make approvals clearer, and they let the agent check its own plan before it acts.
Policy outside the prompt
The most important idea in this lesson is that the rules should be policy as code: written as explicit, testable rules that the gateway evaluates, and not as sentences in the system prompt.
Consider the rule "refunds above ₹5,000 need a supervisor's approval". In a prompt, it is a request that the model may follow, misread or be talked out of. In the gateway, it is a few lines:
rule: refundoverlimit
when: tool == "issuerefund" and amount > 5000
then: requireapproval(role = "supervisor")
The gateway checks every refund call against it. The model cannot skip it, forget it or be argued out of it. And the rule can be read, tested and changed by a person who does not need to understand the model.
What a rule looks at
Rules can depend on:
The tool and its arguments. Which tool, what amount, which recipient, which table.
The identity. Which agent and whose authority it is acting under.
The context. The task type, the autonomy stage of the agent, the time of day and whether a person is present.
The history. How many such calls have already been made this hour, today or in this task, and the total value so far.
The risk signals. A new payee, an unusual amount, a destination outside the usual list or a flag raised by a monitor.
Three possible decisions
For each call, a rule gives one of three decisions.
Allow. The call goes ahead at once.
Deny. The call is refused, with a reason that goes back to the model so that it can choose differently or stop. A good denial says what was wrong and what is possible: "Refunds above ₹5,000 are not allowed for this agent. Ask a supervisor to approve."
Ask. The call waits for a person's approval, as in Lesson 6.3. The gateway sends the request, with the six-part information, and resumes or cancels according to the reply.
A fourth outcome is sometimes added: allow with a change, such as capping an amount at the limit, or removing a field. Use it sparingly, since the agent should know what really happened.
Order and defaults
Rules are checked in order, and the last resort matters. The safe default is deny unless allowed: anything not matched by an allow rule is refused. The reverse, allow unless denied, means every new tool and every new use is open until someone remembers to block it. Start from deny, and open what is needed.
Also make the strictest decision win. If one rule says allow and another says ask, ask.
Testing policy
Because the policy is code, test it like code. Write test cases: this call should be allowed, this should be denied, this needs approval. Run them whenever a rule changes, and include the cases from past incidents. A change to a rule is a release, and goes through the review of Lesson 8.4.
Limits: rates, quotas and caps
Policy is not only about single calls. Some risks come from volume. The gateway enforces limits over time.
Rate limits. At most N calls of a kind per minute or per hour.
Quotas. At most N calls, or a total value, per task, per day or per user.
Spend caps. A maximum of money or tokens per task and per day, as in Lesson 7.4.
Concurrency limits. At most N tool calls in flight at once, so that parallel agents cannot overwhelm a system.
Blast-radius limits. At most N records changed in one task. A task that wants to change 4,000 records is asked about, even if each single change is allowed.
These limits turn an unbounded failure into a bounded one. Consider an agent that goes wrong and begins issuing refunds at 120 calls a minute, each averaging ₹400. If it takes 30 minutes to notice and stop it, it makes 120 × 30 = 3,600 refunds, worth 3,600 × ₹400 = ₹14,40,000, or ₹14.4 lakh. If the gateway caps refunds at 20 an hour per agent, the agent can make 20 × 0.5 = 10 refunds in the same 30 minutes, worth ₹4,000, and the 11th call is denied. The same fault, the same time to respond, and a loss that is 360 times smaller. The cap cost nothing for normal use, which stays well under 20 an hour.
Predict first: an agent starts issuing refunds at 120 a minute, each worth ₹400, and nobody stops it for 30 minutes. What is the loss without a limit? And what is it if a gateway caps refunds at 20 an hour? Think before reading on.
The losses are ₹14.4 lakh and ₹4,000.
Approvals at the gateway
The gateway is the natural place to enforce approval gates, because it already sees every call. The properties that Lesson 6.3 required are easy to build in here.
The approval is bound to the exact call. The gateway stores the call's arguments with the request, and the approval applies to that call only. If the agent changes the amount, a new approval is needed.
The approval expires. After a set time the call is cancelled.
The approver is checked. The gateway verifies that the person has authority, and is not the person who started the task, where the policy requires separation.
The call is idempotent, so that an approval followed by a retry does not act twice.
The decision is logged, with who approved, when and what they were shown.
The agent, meanwhile, is not blocked while waiting. It can park its state, as Lesson 8.3 described, and resume when the answer comes.
The load that approvals create
Every approval takes a person's time, so the policy sets how many people you need. Suppose an agent makes 10,000 tool calls a day, and the policy sends 6 per cent of them for approval. That is 600 approvals a day. At 2 minutes each it is 1,200 minutes, or 20 hours. A reviewer can give about 6 focused hours a day, so you need 20 ÷ 6 = 3.3 reviewers. That is a heavy cost, and a heavy risk: if approvals pile up, people click through, as Lesson 6.3 warned.
Now suppose that, after looking at the data, you find most approvals are for small amounts that are almost always approved, and you move the threshold so that only 2 per cent need approval. That is 200 approvals, 400 minutes, 6.7 hours and 1.1 reviewers. A few lines of policy changed the staffing need by two thirds. The rule is to ask for approval where a person adds value, and nowhere else, and to use the data to tune the thresholds.
The gateway and the model's view
What the gateway returns to the model matters, because the model reasons from it.
Denials should teach. "Denied" alone invites the model to retry in a different way, or to try another tool to get round the rule. A denial should say what rule applied, in a form that does not give an attacker a map of the system, and what the agent may do instead: ask a person, use a smaller amount, or stop and report.
Do not reveal more than needed. Error messages from the real tools may contain internal details. The gateway should translate them.
Results are sanitised. The gateway trims and structures results, as Lesson 9.1 advised, and marks untrusted content so that the model treats it as data, as Lesson 7.3 described.
Tool discovery is controlled. The gateway tells the model only about the tools it may use for this task, which keeps the context small and removes temptation.
Building a gateway
A gateway need not be large. A small team can begin with a function that all tool calls pass through. It takes the call, the identity and the task, looks up the policy, decides, logs and executes. From that start, you add features as needed. Three design points:
No bypass. The tools should accept calls only from the gateway, by network rules or credentials that only the gateway holds. If the agent can reach a tool directly, the gateway is advice.
Fail closed. If the gateway cannot decide, for example the policy service is down, it denies. A gateway that fails open is no gateway.
Keep it fast. Policy checks are code and should take milliseconds. A slow gateway tempts people to skip it.
Where an agent uses MCP servers, as in Lesson 6.1, the gateway can sit in front of them, so that the same policy covers tools whoever wrote them.
Three worked examples
Example 1: policy that replaced a paragraph
A support agent's system prompt had a paragraph of refund rules: limits by customer tier, exceptions for damaged goods and a ban on refunds to cards not used for the order. The model followed them about 94 per cent of the time. The team moved the rules into the gateway as seven policy rules and tests. The prompt kept one line on the goal. Violations of the refund rules dropped to zero, since a violating call could not execute, and the prompt shrank by 600 tokens, which cut cost slightly.
Example 2: a gateway denial that taught
An agent tried to delete 800 old records in one task. The blast-radius rule allowed at most 50 changes per task without approval, and denied the call with "This would change 800 records. The limit is 50 without approval. Request approval with a list of the records and the reason." The agent prepared an approval request with a summary and a sample. A person reviewed it, saw that 12 of the records were still in use and rejected the deletion. Without the gateway, the delete would have run.
Example 3: a gateway that failed open
A team built a gateway that, when its policy service timed out, allowed the call so as not to interrupt work. During a short outage of the policy service, an agent made 400 calls, many of which the rules would have refused. The team changed the gateway to fail closed, with a short local cache of safe read-only decisions so that reads kept working, and added an alert when the policy service was unavailable.
A checklist for gateways and policy
1. Route every tool call through one gateway, and block direct access to the tools.
2. Have the gateway identify the agent, task and authority, validate arguments and record every call with the trace ID.
3. Keep credentials behind the gateway. The model never sees them.
4. Write policy as code with allow, deny and ask decisions. Default to deny, and let the strictest rule win.
5. Write tests for policy, including past incidents, and release rule changes like code.
6. Add rate limits, quotas, spend caps, concurrency limits and blast-radius limits. Set them just above normal use.
7. Bind approvals to the exact call, expire them and check the approver. Let the agent park while it waits.
8. Tune thresholds with data so that approvals are few enough to be read, and compute the reviewers needed.
9. Write denials that teach, and sanitise results.
10. Fail closed, keep the gateway fast and alert when it is unavailable.
Common mistakes
Rules in the prompt only. The model can forget or be talked out of them.
Controls scattered across tools. The weakest tool sets the real safety.
Allow unless denied. New tools are open by default.
No limits on volume. A fault runs at full speed until someone notices.
Approvals for everything. People stop reading.
Approvals that can be reused. Bind them to the call and expire them.
Bare denials. The agent retries or looks for a way round.
A gateway the agent can bypass. Block direct access.
Failing open. An outage removes your controls when you most need them.
Untested policy. A rule you have not tested may not do what you think.
Key takeaways
A tool gateway is one layer through which every agent action passes. It identifies, authorises, validates, applies policy, limits, asks for approval, makes calls repeatable, executes with hidden credentials, sanitises results and records everything.
Put rules in policy as code, not in the prompt: explicit allow, deny and ask decisions that are tested and versioned. Default to deny.
Add limits on rate, quota, spend, concurrency and blast radius. A cap of 20 refunds an hour turns a ₹14.4 lakh runaway into ₹4,000.
Enforce approvals at the gateway, bound to the exact call, expiring and checked. Tune the thresholds: 6 per cent of 10,000 calls needing approval needs 3.3 reviewers, and 2 per cent needs 1.1.
Write denials that teach, keep the gateway fast, fail closed and allow no bypass.
Check yourself
1. What is a tool gateway?
A layer of code between the model and the tools through which every call must pass, applying identity, policy, limits, approvals, execution and logging.
2. Why is "deny unless allowed" the safer default?
With allow unless denied, every new tool and use is open until someone remembers to block it. With deny by default, nothing works until it is deliberately permitted.
3. A gateway caps an agent at 30 calls an hour, each worth ₹250. A runaway runs for 20 minutes. What is the most it can do?
30 × 20 ÷ 60 = 10 calls, worth ₹2,500.
4. 5,000 calls a day, 4 per cent need approval, 3 minutes each, 6 focused hours per reviewer. How many reviewers?
200 approvals × 3 = 600 minutes, 10 hours, ÷ 6 = 1.67 reviewers.
5. Why should the gateway fail closed?
If it failed open, an outage in the policy service would remove the controls exactly when mistakes are most likely.
6. Why must a denial say what is possible instead?
A bare denial invites retries and workarounds. A useful one points the agent to a permitted route, such as requesting approval or using a smaller amount.
Try this
Set aside seventy-five minutes.
1. List every tool an agent can use. For each, note who could call it today and what checks it makes.
2. Design a gateway: the ten functions and where each would live. Say how you would stop direct access to the tools.
3. Write ten policy rules in plain form: tool, condition and decision. Include a default, a rate limit and a blast-radius limit.
4. Write a test for each rule, including one allowed, one denied and one needing approval.
5. Use the cap simulator with a realistic runaway rate and value, to choose a cap just above normal use.
6. Use the reviewer simulator with your estimated calls and approval share. How many reviewers would you need, and what threshold would you change?
7. Write the denial message for two rules so that each tells the agent what to do instead.
A good result is a gateway design: the functions, the policy rules, the limits, the approval flow and the tests. Lesson 10.3 covers what happens after the actions: how to know the work is really done and right.
Tags: Agentic AI Tool gateway Policy as code Approval gates Harness engineering