Enforcing AI agent permissions on tool calls in production
How to enforce AI agent permissions before a tool call runs. Grant tools per agent at a gateway, and check each call's arguments inside the agent's own process.
How to enforce AI agent permissions before a tool call runs. Grant tools per agent at a gateway, and check each call's arguments inside the agent's own process.
For most teams, AI agent permissions are a sentence in the system prompt and a person approving the tools that look dangerous. That is where an Ask HN post from January 2026 started. It asked how people enforce permissions on agent tool calls in production, and where the enforcement point is that the agent can't get around. Amiril puts it in two places. A gateway decides which tools each agent can reach at all. The SDK checks each call's arguments inside the agent's process before the tool runs, against rules you publish centrally. Both keep a record of each blocked call apart from the call itself.
The poster said most of the safety patterns they heard were "prompt rules + basic validation + 'human-in-the-loop for risky stuff.'" What they wanted was the thing that guarantees a tool call doesn't run unless it passes policy. Their follow-up questions covered where to enforce, identity when an agent works on a user's behalf, logging decisions apart from execution, rolling out from shadow mode to enforcement, and which failures hurt most.
One reply came from keypost.ai, which describes itself as "policy enforcement for MCP pipelines" and puts a proxy between the agent and its MCP servers. On the model, it said: "If the agent can reason about or around the policy, it's not really enforcement". It also wanted rules on arguments and not only on tools, with the example "github.delete_branch is fine, but only for feature/* branches". The other reply said some teams rely on the services behind the tools, sending them a token that acts for the user, while others check at the tool and keep the policies in one place with OpenFGA.
The thread offered three candidates: inside each tool wrapper, at a gateway or proxy, or in a central policy service. Amiril uses the first two, and takes the useful part of the third, which is that rules are written and published in one place.
The gateway holds the coarse part of AI agent permissions: which tools each agent may call. You register each tool server once, and every tool on it starts denied, including a tool the server adds months later. Each agent has its own grants, a switch per tool on its Access tab, and the agent's MCP client sees only the tools it was granted. A call to anything else comes back as an error the model can read:
This agent doesn't have access to payments__refund_payment. An admin can grant it in Amiril.
The finer question is which calls to a granted tool should go through, and that check runs where the tool runs. The SDK wraps the agent's tools and checks each call against its own copy of the published rules, so a check makes no network request. Every call of a wrapped tool is checked first, whether the model asked for it or your own code made it. Sitting in the process also means it sees the run so far, the earlier calls and what they returned, and a rule can depend on them. A rule you publish reaches every running process within about a minute, with no redeploy. If Amiril can't be reached, the SDK goes on enforcing the last rules it had, and reloads them from disk after a restart.
Deleting a feature branch and deleting main are calls to the same tool. A grant decides whether the agent has the tool at all. To AI agent tool call permissions that stop at the tool name, the two calls look the same; only the branch argument differs. Here is the keypost example as an Amiril rule:
@id("feature-branches-only")
@sentence("{branch} is not a feature branch")
block (
agent == "release",
tool == "delete_branch"
)
unless {
starts_with(call.args.branch, "feature/")
};unless describes what a good call looks like. Any branch name that doesn't start with feature/ is blocked, and so is a call that leaves the branch out. {branch} in the sentence is filled with the branch the call named.
A rule can read earlier calls as well. This one comes from the rule language guide. It covers every agent, which is what agent on its own means, and lets a payment through only when the latest verify_payee call for the same payee said the payee was registered:
@id("verified-payee")
block (
agent,
tool == "issue_payment"
)
unless {
payee = call.args.payee;
registered = latest(verify_payee{ args.payee == payee }).result.registered;
registered == true
};Knowing which rules to write means knowing where the agent goes wrong, and Amiril reads that from the agent's traces. Runs that share a problem become an issue, and when a rule could stop them, Amiril drafts one as a rule suggestion. Drafts that would also fire on the agent's ordinary runs never reach you: one that fires on more than one in a hundred of them is dropped, as is one that catches none of the issue's runs. The suggestion then says what the rule would have done, in a line like "Would have blocked 4 of 5 runs". The gateway has its own kind, the access suggestion, which proposes revoking a tool granted more than 30 days ago that the agent hasn't called in the last 30.
Only the second reply touched agent authorization, and from the service's side. If the model fills in whose account a payment comes from, a user with a good enough story can get it to fill in someone else's. So the identity shouldn't pass through the model at all. The tool the model sees takes an account id. Your code looks up the account's owner, adds the signed-in user it already knows, and hands both to the function the SDK checks:
import amiril
from amiril import plain
amiril.init()
agent = plain.Agent("payments")
def payment_tools(signed_in_user: str) -> list:
@agent.tool(name="issue_payment") # the tool name rules match on
def checked_payment(payee: str, amount: float, account_id: str,
account_owner: str, user_id: str) -> dict:
return payments.send(payee, amount, account_id)
def issue_payment(payee: str, amount: float, account_id: str) -> dict:
"""Pay a payee from one of the user's accounts."""
owner = accounts.owner_of(account_id)
return checked_payment(payee, amount, account_id, owner, signed_in_user)
return [issue_payment]The rule compares the two values:
@id("payer-is-user")
@sentence("The payment comes from an account the user does not own")
block (
agent == "payments",
tool == "issue_payment"
)
unless {
call.args.account_owner == call.args.user_id
};When a user asks the agent to use an account someone else owns, the model gets this in place of a result:
The call to issue_payment was not performed. Rule payer-is-user blocked it: The payment comes from an account the user does not own (account_owner "user_8213"; user_id "user_4790"). Do not retry this call with different arguments or achieve the same outcome another way; tell the user it was not performed.
The values the rule read are added to its sentence, so the record says who asked and whose account it was. As a rule, the check starts in Shadow like any other, and moves to Enforce one environment at a time with no redeploy. If the payments service also accepts a token scoped to the user, as the second reply suggested, send that too, and the service and the rule each check the call.
The keypost reply ended by asking which failure people actually hit, an agent trying something it shouldn't or a policy so strict it blocks real work. On a new rule's first day we'd bet on the second, since nobody can list every legitimate call from memory. When a too-strict rule blocks a real call, the agent tells the user it was not performed, and the record of the block in Amiril shows which of your AI agent permissions to loosen.
A published rule starts in Shadow. The SDK checks every call against it and records what it would have done, and the call runs either way. The rule's page counts the calls it would have fired on in the last 24 hours, once from your traces and once from what the SDK decided in your agents. Test on past calls replays any version of the rule, a draft included, over as much as the last 30 days. It lists each call it would have fired on with the agent, the tool and the sentence. Once every call in that list is one you meant to stop, switch the rule to Enforce on its page. The mode is set per environment, so a rule can enforce in Development while Production stays in Shadow.
The SDK sends Amiril a record of each call a rule blocks, with the values the rule used. In Shadow it sends the same record for calls the rule would have blocked. Flagged and allowed calls arrive as a count per rule. In Traces, a run with a blocked call carries a "1 blocked call" badge, and the With blocked calls filter lists only those runs.
At the gateway the two halves sit in separate columns. An agent's Calls tab lists each call through the gateway, where Decision says whether the gateway let it through and Result says how the tool server answered. For the "why was this allowed?" question, the access log holds every grant and revoke with the person who made it and when, next to each refused call and its reason, such as Not granted or Unknown tool. Each environment keeps the SDK's decisions for its own retention period.
The quickest way to watch the SDK refuse a call is the rules quickstart, which runs a payment cap from a local file on your laptop and needs no account.