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 decides its own steps, and every step can be a paid model call. Most of the time that is fine. The trouble is the run that goes wrong: a loop that keeps calling a tool, or an answer that runs to thousands of tokens. You want a hard stop that does not depend on the model behaving.

Troupekit's stop is Limits. A run checks its limits before every model call, and if the call could cross one, the run pauses instead of making the call.

Before the call, not after

The word that matters is "could". Troupekit cannot know what a reply will cost until it arrives, and by then the money is spent. So it estimates the cost first: the input tokens of everything it is about to send, counted as a quarter of the request's JSON bytes, plus max_tokens output tokens, the most the reply is allowed to be. If that estimate does not fit, the call never happens. The output part is an upper bound; the input part is an approximation, so a token-dense input can cost more than estimated.

course_spend_cap makes this visible. It builds one agent with a cap of 1 000 millionths of a dollar per call:

#![allow(unused)]
fn main() {
async fn capped(max_tokens: u32) -> Result<BuiltAgent, KitError> {
    Agent::new("capped")
        .model(MODEL)
        .instructions("Answer in one sentence.")
        .max_tokens(max_tokens)
        .max_turns(2)
        .limits(Limits {
            call: Some(1_000),
            run: Some(5_000),
            ..Limits::default()
        })
        .build()
        .await
}
}

call caps one model call and run caps the whole run. Both are in millionths of a dollar, and None means no limit.

Then it runs the same question twice, once with the default output budget of 4 096 tokens and once with 256:

#![allow(unused)]
fn main() {
for max_tokens in [4_096, 256] {
    let agent = capped(max_tokens).await?;
    match agent.run_with("Why do agents need spend limits?").await {
}

Work out the first case with the catalog price of claude-haiku-5-5, which is 0.50 dollars per million output tokens. 4 096 output tokens could cost 2 048 millionths of a dollar, over the cap of 1 000 before a single input token is counted. So the first run pauses without calling the model at all. With 256 output tokens the estimate is a little over 128, which fits, so the second run answers.

The run that stops comes back as an error you can match on:

#![allow(unused)]
fn main() {
Err(KitError::Paused { run_id, reason }) => {
    println!("max_tokens {max_tokens}: run {run_id} paused before any model call: {reason:?}")
}
}

reason names the limit that was crossed, here call.

Try it

cargo run -p troupekit --example course_spend_cap

The first line should say the run paused on the call limit. The second should be a one-sentence answer and what it actually cost, a small fraction of the cap.

What to take from this

Set max_tokens to what the answer really needs. It is the number the output part of the estimate is built on, so a generous default makes your caps bite sooner than you expect. And treat the cap as a fence, not a forecast: the real cost of the second run is far below its estimate, and that is the point.

Limits come in two more layers, scope and user, which count spend across many runs. They need a record of past spend that survives a restart, and they get their own lesson later in the course.

Next: Memory.