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

Build a spend-capped team of agents

One agent is easy to keep an eye on. A team of them is not. Three agents passing work along means three times the model calls, and a cap on each agent does not tell you what the team as a whole has spent this week. What you want is one budget for the team.

course_app_team builds a small release-notes team: a planner, a writer and a reviewer. The whole team runs under one cap, and the example keeps handing it work until the cap stops it.

The members

#![allow(unused)]
fn main() {
fn member(name: &str, instructions: &str) -> Agent {
    Agent::new(name)
        .model(MODEL)
        .instructions(instructions)
        .max_turns(2)
        .max_tokens(500)
}
}

Each member is a small agent with one job. None of them has its own limits. That is not an oversight: a graph refuses an agent that brings its own limits or ledger, so the team's cap is never quietly replaced by a member's.

max_tokens(500) matters more here than it looks. Every model call is checked against an estimate of its cost, and the estimate counts the full max_tokens of output. With the default of 4 096, one call's estimate is 2 048 millionths of a dollar, so this team would stop at its cap before its first call.

The team

#![allow(unused)]
fn main() {
async fn team(ledger: Arc<SqliteStore>) -> Result<BuiltGraph, KitError> {
    Graph::new("release-team")
        .agent("plan", member("planner", "Turn the change into three bullet points a user cares about."))
        .agent("write", member("writer", "Write a release note of at most 60 words from the bullet points."))
        .agent("review", member("reviewer", "Return the release note with any jargon replaced by plain words, and nothing else."))
        .limits(Limits {
            run: Some(10_000),
            scope: Some(PeriodLimit { key: TEAM.into(), max_usd_micros: TEAM_CAP }),
            ..Limits::default()
        })
        .ledger(ledger)
        .build()
        .await
}
}

The team is a graph of three agents in a line: the planner's bullets go to the writer, the writer's note goes to the reviewer. The limits sit on the graph and cover every member. run caps one release note. scope caps the team across every run, under the ledger key team:release-notes, at 600 millionths of a dollar here, small on purpose so the example reaches it. .ledger(..) keeps the team's total in a SQLite file.

Work until the money runs out

#![allow(unused)]
fn main() {
for change in changes {
    let spent = ledger.period_total(TEAM).await?;
    println!("team has spent {spent} of {TEAM_CAP}");
    match team.run_with(change).await {
}

Four changes, one run each. Before every run the example prints the team's total from the ledger. When a run would cross the team cap, it stops before its next model call, and the example prints that and ends.

Try it

cargo run -p troupekit --example course_app_team

The first changes come back as short release notes, each with its step count and cost. Then the team's total gets close to the cap, and a run stops. Look at where: the cap is checked before every model call, so a run can stop between members, with the planner done and the writer never called. A call the estimate says would cross the cap is not made, not even halfway through a team's work. The estimate counts the full max_tokens of output but only approximates input tokens, so a call can still cost more than estimated.

Taking it further

  • Use a durable ledger file, and the cap holds across restarts, as the budgets lesson showed. Runs in one process that share the ledger hold each call's estimate until its cost is recorded, so they cannot both take the last room. Runs in separate processes do not see each other's holds, so the cap is not enforced across them: run the team from one process (issue #79).
  • Give each customer or project its own scope key, and every team you run for them shares that budget.
  • A run stopped by its budget can be given more room and continued: for hosted runs, Runner::raise_budget(run_id, limits) does that. Tracing and budgets has the details.

Team, crew and handoff patterns in TOML are planned for 0.5. Until then a graph of agents, as here, is how you build a team.

Next: Build an offline assistant.