budget on defineAgent and the runtime automatically tracks cumulative token usage and USD cost, checking the budget before each iteration of the agent loop. When a cap is breached you choose the response: throw an error, switch to a cheaper model, or route the run to human approval.
Configuring a budget on defineAgent
Pass aBudgetConfig to the budget option on defineAgent. The governor is instantiated per request, so limits apply to a single agent run, not across all users.
BudgetConfig fields
Hard cap on the total number of tokens (input + output) consumed by this run. The governor checks cumulative usage before each model call; once the count equals or exceeds this value, the configured
onBreach action fires.Hard cap on the total USD cost for this run. Computed from per-call
costUsd values reported by the LLM client. Like maxTokens, this is checked before each model call.Action to take when a cap is hit:
'block'— throwsBudgetExceededErrorimmediately, stopping the run and returning a 500 to the client.'degrade'— signals that the run should continue on a cheaper model. TheonBreachcallback onCostGovernorreceives the breach info so your code can switch models.'approve'— suspends the run and routes it through the human-in-the-loop approval queue, just like a tool that throwsApprovalRequiredError.
BudgetExceededError
WhenonBreach is 'block', the runtime throws a BudgetExceededError. Catch it in error-handling middleware to return a structured response:
BudgetExceededError exposes three properties:
Total USD cost accumulated by this run at the time of the breach.
Total tokens (input + output) consumed by this run at the time of the breach.
The
BudgetConfig that was in effect — useful for building informative error messages.Using CostGovernor directly
defineAgent wires CostGovernor for you when you pass a budget. For custom agent loops built with runAgent or streamAgent, construct and pass the governor explicitly:
CostGovernor methods
Accumulate usage from a completed model call. Called automatically by the Anvil agent runtime; call it manually only if you are driving the LLM client directly.
Check the current spend against configured caps. Returns
'ok' if within budget, throws BudgetExceededError if onBreach is 'block', or calls the onBreach callback and returns the action for 'degrade' and 'approve'.Returns
true if the current accumulated spend has reached or exceeded any configured cap. Does not throw or trigger the breach action.Read-only accessor for the total USD cost accumulated so far.
Read-only accessor for the total token count accumulated so far.
Tracking cost across LLM calls
The LLM client reportscostUsd per generate call based on the model’s pricing. The governor accumulates these values across every iteration of the agent loop. Aborted streams (client disconnects mid-response) are counted too, so a disconnect never creates a cost blind spot.
Example: degrade to a cheaper model on breach
onBreach is 'degrade', the CostGovernor fires its onBreach callback with { action: 'degrade', spentUsd, spentTokens }. Wire that callback to update request state or swap the active client.