Scoped tokens for LLM tool calling: least privilege for agents
Give a language model a tool and you have given it a way to be wrong with real consequences. A model that can call send_email can email the wrong person; a model that can call run_query can run the query an attacker slipped into a document it was summarizing. The model is not malicious, but its inputs are attacker-influenceable, and prompt injection turns "the model decides what to do" into "whoever controls the model's context decides what to do." The defense is not a better prompt. It is making sure that even a fully hijacked model holds only enough authority to do the one narrow thing the current step legitimately requires.
Why a broad agent token is the wrong default
The common pattern is one access token per agent session, carrying the union of every scope any tool might need. It is convenient and it is exactly the capability an injection attack wants to steal or abuse. If the agent holds db:write, email:send, and files:delete for the whole session, then a single poisoned input can reach any of those, regardless of what the user actually asked for. Session-wide scope means the blast radius of one bad step is the entire mandate.
Least privilege for agents means tightening two axes at once:
- Scope — the token for a tool call should authorize that tool and the specific resource it touches, nothing else.
- Time — the token should live only as long as the call, so a leaked or logged token is useless minutes later.
Mint a token per tool call
Instead of handing the model a powerful session token, keep the broad grant server-side and exchange it for a narrow, short-lived token at the moment a tool is invoked. OAuth token exchange (RFC 8693) is built for exactly this: downscope a token to a tighter audience and scope set.
async function mintToolToken(session: AgentSession, call: ToolCall) {
// The session grant stays on the server; the model never sees it.
const policy = toolPolicies[call.name];
if (!policy) throw new Error(`no policy for tool ${call.name}`);
// Downscope: this token can do ONE thing, briefly.
return tokenExchange({
subject_token: session.grant, // held server-side
audience: policy.resource, // e.g. "orders-api"
scope: policy.scopes.join(" "), // e.g. "orders:refund"
// Bind to the concrete target when the tool acts on one resource.
resource_constraint: call.args.orderId,
expires_in: 60, // seconds
});
}The model proposes a tool call; your orchestrator looks up the policy for that tool, mints a token constrained to the specific resource and scope, uses it for exactly that call, and discards it. The model never holds a credential broader than the current step.
Never let the model choose its own scopes. If the token request is assembled from the model's output, prompt injection can simply ask for more. The mapping from tool name to allowed scopes must live in server-side policy the model cannot edit. The model picks which tool to call; your code decides what authority that call gets.
Constrain to the resource, not just the action
Scoping to orders:refund still lets a confused model refund the wrong order. Where the tool acts on a specific object, bind the token to that object. A token that authorizes "refund order 88213" cannot be redirected to order 90000 even if the model, mid-hijack, decides it wants to. Resource-bound, action-scoped, time-boxed tokens shrink the blast radius of any single step to that step alone.
Confirmation for the irreversible
Some actions should not be reachable by a token the model obtained on its own say-so. Classify tools by reversibility:
- Read / reversible — the model may trigger these within its session mandate.
- Consequential / irreversible — deleting data, moving money, sending external messages — require a fresh human confirmation that mints the token, and record that confirmation in the audit trail.
The point is to make the dangerous path require a signal that cannot be produced by text in the model's context window.
Validate the call, not just the token
A short-lived token is necessary but not sufficient. The tool handler should still check that the call is coherent: that the resource named in the token matches the resource in the arguments, that rate limits for this agent and this tool are respected, and that the action is within any numeric constraints the grant carried.
function authorizeCall(token: ToolToken, call: ToolCall) {
if (token.audience !== toolPolicies[call.name].resource)
throw new Forbidden("token audience mismatch");
if (token.resource_constraint && token.resource_constraint !== call.args.orderId)
throw new Forbidden("token not valid for this resource");
if (call.name === "refund" && call.args.amount > token.limit)
throw new Forbidden("amount exceeds grant");
return true;
}What this buys you
With per-call scoped tokens, the worst case of a successful prompt injection changes shape. The attacker can make the model call a tool it was allowed to call, on a resource the current step legitimately targets, for a bounded duration — and nothing else. They cannot pivot to a different tool, a different resource, or a persistent credential. You have turned "the model has broad standing authority" into "the model has to earn narrow authority, one verifiable step at a time."
You cannot make a language model trustworthy by asking it nicely; you contain it by ensuring the authority available at any moment is only what that moment requires. Minting those narrow, time-boxed tokens means running an issuer that can downscope on demand and revoke on command — which is far easier when that issuer is part of an identity layer you control rather than a feature you are waiting on a vendor to ship. Owning your auth is what makes per-call least privilege a configuration choice instead of a roadmap item.
Run it yourself
Bastionary is free, open-source software. Follow the quick start to have it running on your own server, or browse the releases on Git.