Skip to main content

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​

policy-config.yaml
pack:
  name: tool-budget-example-1
  version: "1.0.0"
  enabled: true

policies:
  chain:
    - tool-budget

policy:
  tool-budget:
    budgets:
      web_search:
        max_tokens: 100

Fields​

FieldTypeDefaultDescription
budgetsobject{}Limits by tool name. The key must equal the name that the client sends.
budgets.<tool>.max_tokensinteger, at least 1Maximum 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​

ItemValue
Input phaseCompares the largest valid output-token cap plus recorded session use with each declared tool limit
Output phaseRecords provider-reported output tokens for called tools when a session ID is present
Verdictsallow, block
No exceeded tooltool-budget.ok
An exceeded tooltool-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:

tests/blocks-search-over-budget.json
{
  "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 --json

This isolated test starts without recorded session use. Its details.policy_results entry shows exceeded_tools: ["web_search"]. Copy the file for the boundary cases:

RequestResult
max_tokens: 100allow, ok
max_tokens: 101block, tool-budget.exceeded
A tool without a budgetallow, ok
No max_tokensallow, ok

How it works​

  1. The gateway reads max_tokens, max_completion_tokens, and max_output_tokens. It uses the largest valid unsigned integer. Missing or invalid values supply no requested cap.
  2. It reads tool names from tools[*].function.name, or tools[*].name.
  3. For each budgeted tool, it reads the recorded session total. Without a session, that total is zero.
  4. 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.
  5. Any exceeded tool blocks before the provider call with HTTP 400 and tool-budget.exceeded. Otherwise, the request is allowed with tool-budget.ok.
  6. 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:

Request fragment
{
  "max_tokens": 250,
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "web_search"
      }
    },
    {
      "name": "database_query"
    }
  ]
}

Limits and failure behavior​

  • Send a stable x-session-id to 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 tools array, 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.
ProblemCauseAction
A large request passedThe cap was absent or invalid, or the request did not declare the budgeted toolSend a valid integer cap and the expected tool name
A request below the cap was blockedEarlier calls used the session budgetRead the session's usage and use the correct session ID
Blocked although the model called no toolAdmission checks declared tools before dispatchDeclare only tools available to that request
Totals differ across replicasShared state is not configured, or its outage policy uses local totalsCheck the shared backend and failure mode
Lint rejects max_cost_usdThat field is not supportedUse token limits, or financial budgets for spend

Next steps​