troupekit 0.4.0

Rust framework for LLM agents that run inside your own program: #[agent] functions or persona TOML files, tools, memory, document search and graph workflows that survive a restart.
Documentation
use std::error::Error;
use std::sync::Arc;
use troupekit::agent::{Limits, PeriodLimit};
use troupekit::core::store::UsageLedger;
use troupekit::prelude::*;
use troupekit::SqliteStore;

const MODEL: &str = "anthropic/claude-haiku-5-5";
const TEAM: &str = "team:release-notes";
const TEAM_CAP: u64 = 600;

/// One member of the team: a small agent with its own job.
/// Input: its name and instructions. Output: the unbuilt agent.
fn member(name: &str, instructions: &str) -> Agent {
    Agent::new(name)
        .model(MODEL)
        .instructions(instructions)
        .max_turns(2)
        .max_tokens(500)
}

/// Builds the team as a graph of three agents: plan, write, review. The
/// cap sits on the graph: per run, and for the whole team across runs
/// under one ledger key. Input: the ledger. Output: the built graph.
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
}

/// Gives the team one change after another until the team's cap stops a
/// run, printing the team's total before each. Input: ANTHROPIC_API_KEY.
/// Output: each release note, or the step where the cap stopped the team.
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let dir = tempfile::tempdir()?;
    let ledger = Arc::new(SqliteStore::open(dir.path().join("team.db")).await?);
    let team = team(ledger.clone()).await?;
    let changes = [
        "Runs now resume after the process is killed.",
        "Tool arguments are checked against their schema before the tool runs.",
        "Agents can search a folder of Markdown files.",
        "Traces record sizes by default and text only when asked.",
    ];
    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 {
            Ok(report) => println!(
                "{}\n({} steps, {} millionths of a dollar)\n",
                report.output().as_str().unwrap_or_default(),
                report.steps(),
                report.usage().cost_usd_micros
            ),
            Err(KitError::Paused { run_id, reason }) => {
                println!("run {run_id} paused before its next model call: {reason:?}");
                return Ok(());
            }
            Err(KitError::Failed { run_id, reason }) => {
                println!("run {run_id} stopped before its next model call: {reason}");
                return Ok(());
            }
            Err(other) => return Err(other.into()),
        }
    }
    println!("all changes written under the cap");
    Ok(())
}