Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Spend limits

An agent that loops on a tool can burn through a budget in minutes, and a turn cap does not help much: ten turns on a large model with a long context cost far more than ten turns on a small one. Troupekit caps money, not turns.

How it works

Before every model call, a run estimates the cost of that call and checks it against the limits you set. If the estimate says the call would cross one, the run pauses or fails instead of making the call. With no limits set, an agent's default, nothing is checked. The check lives in budget::check.

  • The estimate never under-counts output. It counts the input as a quarter of the JSON bytes of the system text, messages and tool specs, rounded up, plus the full max_tokens of output and every web search the hosted tools allow. The input count is an approximation, not a bound (estimate).
  • Four layers. Limits holds a cap for one call, for the whole run, and two period caps that count spend across runs: one for a scope, such as a goal's month, and one for a user. Amounts are in millionths of a US dollar.
  • The ledger. Period caps read their totals from a usage ledger. The SQLite store has one, so a cap holds across restarts, and agents that share the file see each other's recorded spend. Before each call a run holds the call's estimate under the period keys until its cost is recorded, so runs in one process that share the ledger cannot both take the last room.
  • Unknown prices stop the run. If a model's price is not verified and the run has limits, the check reports unverified_price and the run pauses, rather than guessing.
  • Personas only tighten. A persona's budget = { call_usd = 0.05, run_usd = 0.5 } combines with limits set in code, and the stricter of the two wins.
  • Routing respects money too. A route policy's [budget] cloud_usd_per_day caps remote spend per day; at the cap, remote targets are skipped and the agent stays on the device.

Here is an agent with a run cap of 5 000 millionths of a dollar, half a cent, set on the #[agent] macro in a test of the kit:

#![allow(unused)]
fn main() {
/// Answer briefly.
/// Never guess.
#[agent(
    model = "fake/m",
    role = "Checks facts.",
    goal = "A short answer.",
    display_name = "Fact Checker",
    evals = ["min_items(items, 1)"],
    limits = Limits { run: Some(5_000), ..Limits::default() },
    checkpoints = Storage::memory(),
    max_turns = 3,
    max_tokens = 256,
    retries = 1,
    laws = "tests/fixtures/rules.bend",
    auto_laws = false,
)]
pub(crate) fn fact_checker() {}
}

A hosted graph run that paused on its budget continues once you raise it with Runner::raise_budget(run_id, limits). Tracing and budgets has the full API.

What it does not guarantee

  • The caps are in US dollars, not tokens. Token counts are recorded in the usage of every call, but no limit reads them.
  • The estimate counts the full max_tokens of output, so a call that would have fit can be refused near the cap. Its input count is approximate, so a token-dense input can cost more than estimated.
  • The cost recorded after a call is what the provider reported, priced by the catalog. A wrong catalog price gives a wrong total. A routed agent estimates each call at the highest price it can reach, then bills the reply at the price of the target that served it, so a call served on the device is not billed the cloud cost.
  • A shared period cap is not enforced across processes. Holds live in one process: runs in separate processes, or on two ledgers opened over the same store, do not see each other's holds, so together they can spend past the cap. Use one process per shared cap (issue #79).
  • An agent's default ledger lives in memory and is lost on restart. Pass the SQLite store's ledger when a cap must hold across restarts.

Proof

Elsewhere

Some others cap spend too: PydanticAI and Mastra in USD, yoagent in USD on what has been spent so far, ADK-Rust for team runs, AutoGen in tokens. The comparison lists which framework caps what, and when the check happens.