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 troupekit::agent::Limits;
use troupekit::prelude::*;

const MODEL: &str = "anthropic/claude-haiku-5-5";

/// Builds an agent whose every model call may cost at most 1 000
/// millionths of a dollar, with room for `max_tokens` output tokens.
/// Input: the output token budget. Output: the built agent.
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
}

/// Runs the same question twice. With 4 096 output tokens the worst case
/// of one call is over the cap, so the run stops before calling the model.
/// With 256 it fits, so the run answers. Input: ANTHROPIC_API_KEY.
/// Output: what happened to each run.
#[tokio::main]
async fn main() -> Result<(), KitError> {
    for max_tokens in [4_096, 256] {
        let agent = capped(max_tokens).await?;
        match agent.run_with("Why do agents need spend limits?").await {
            Ok(outcome) => println!(
                "max_tokens {max_tokens}: answered for {} millionths of a dollar: {}",
                outcome.usage().cost_usd_micros,
                outcome.text()
            ),
            Err(KitError::Paused { run_id, reason }) => {
                println!("max_tokens {max_tokens}: run {run_id} paused before any model call: {reason:?}")
            }
            Err(other) => return Err(other),
        }
    }
    Ok(())
}