Tool budget
tool-budget blocks a request whose output-token cap would exceed the limit you set for a tool that the request declares. For example, with a 100-token limit for web_search, a request that declares web_search with max_tokens: 250 is blocked.
The gateway also records output tokens for tools called in a session and checks later requests against that total. This is a token control, not a financial spending budget. For spending limits, see Usage and budgets.
Configuration
pack:
name: tool-budget-example-1
version: "1.0.0"
enabled: true
policies:
chain:
- tool-budget
policy:
tool-budget:
budgets:
web_search:
max_tokens: 100Fields
| Field | Type | Default | Description |
|---|---|---|---|
budgets | object | {} | Limits by tool name. The key must equal the name that the client sends. |
budgets.<tool>.max_tokens | integer, at least 1 | Maximum recorded session output tokens for the tool; also checked against a request's output-token cap. An entry without this field has no token limit. |
Lint rejects the removed field max_cost_usd.
Phase and verdicts
| Item | Value |
|---|---|
| Input phase | Compares the largest valid output-token cap plus recorded session use with each declared tool limit |
| Output phase | Records provider-reported output tokens for called tools when a session ID is present |
| Verdicts | allow, block |
| No exceeded tool | tool-budget.ok |
| An exceeded tool | tool-budget.exceeded |
An allowed decision keeps the top-level reason ok. The nested tool-budget result shows tool-budget.ok.
Verify the block with a pack test
Create a JSON test next to the configuration. The request goes in input.request:
{
"name": "blocks-search-over-budget",
"input": {
"messages": [
{
"role": "user",
"content": "Search the documentation."
}
],
"request": {
"model": "replace-with-model-id",
"max_tokens": 250,
"tools": [
{
"type": "function",
"function": {
"name": "web_search"
}
}
]
}
},
"expected": {
"verdict": "block",
"reason_code": "tool-budget.exceeded"
}
}verdictan policy lint --file policy-config.yaml
verdictan policy test --jsonThis isolated test starts without recorded session use. Its details.policy_results entry shows exceeded_tools: ["web_search"]. Copy the file for the boundary cases:
| Request | Result |
|---|---|
max_tokens: 100 | allow, ok |
max_tokens: 101 | block, tool-budget.exceeded |
| A tool without a budget | allow, ok |
No max_tokens | allow, ok |
How it works
- The gateway reads
max_tokens,max_completion_tokens, andmax_output_tokens. It uses the largest valid unsigned integer. Missing or invalid values supply no requested cap. - It reads tool names from
tools[*].function.name, ortools[*].name. - For each budgeted tool, it reads the recorded session total. Without a session, that total is zero.
- A tool is exceeded when its recorded total has reached the limit, or when the total plus the requested cap is greater than the limit. Reaching the limit exactly is allowed when the recorded total is still below it.
- Any exceeded tool blocks before the provider call with HTTP
400andtool-budget.exceeded. Otherwise, the request is allowed withtool-budget.ok. - After a response with tool calls and usage, the gateway adds the reported output-token count to each tool called in that response. It does not divide the tokens among the tools.
The tools array is what the client offers the model, not what the model called. Both name forms are read:
{
"max_tokens": 250,
"tools": [
{
"type": "function",
"function": {
"name": "web_search"
}
},
{
"name": "database_query"
}
]
}Limits and failure behavior
- Send a stable
x-session-idto accumulate use across requests. No session ID or no provider-reported output-token usage means no charge is recorded. - The full response output-token count is charged to each called tool. This is a conservative total, not exact per-tool attribution.
- Local totals reset on restart and can be evicted after 65,536 tool-session entries. Distributed totals use the configured Redis or Valkey backend and expire after 24 hours without an update. See Rate limits for shared-state configuration and outage behavior.
- Without a top-level
toolsarray, the model-request admission check has no tool to check. A tool without a budget passes. - Budget keys match declared request names exactly, including case.
- Omitting the cap does not bypass a session total that has already reached its limit. Below that limit, a missing cap cannot reserve unknown future output.
- This control does not validate arguments or enforce tool permissions. Use Tool validation, Agent firewall, and your tool executor.
- An isolated policy test checks the request cap. It does not prove accumulation across live session requests.
| Problem | Cause | Action |
|---|---|---|
| A large request passed | The cap was absent or invalid, or the request did not declare the budgeted tool | Send a valid integer cap and the expected tool name |
| A request below the cap was blocked | Earlier calls used the session budget | Read the session's usage and use the correct session ID |
| Blocked although the model called no tool | Admission checks declared tools before dispatch | Declare only tools available to that request |
| Totals differ across replicas | Shared state is not configured, or its outage policy uses local totals | Check the shared backend and failure mode |
Lint rejects max_cost_usd | That field is not supported | Use token limits, or financial budgets for spend |
Next steps
- Tool validation: declared tools and argument schemas.
- Agent firewall: tool access and actions.
- Rate limit configuration: request and token rates.