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 ADA: &str = "user:ada";
const CAP: u64 = 300;

/// Builds a helper whose spend for ada, across every run and every
/// restart, is capped at CAP millionths of a dollar, counted in the SQLite
/// ledger `store`. Input: the store. Output: the built agent.
async fn helper(store: Arc<SqliteStore>) -> Result<BuiltAgent, KitError> {
    Agent::new("helper")
        .model(MODEL)
        .instructions("Answer in one short sentence.")
        .max_tokens(256)
        .max_turns(2)
        .limits(Limits {
            run: Some(5_000),
            user: Some(PeriodLimit {
                key: ADA.into(),
                max_usd_micros: CAP,
            }),
            ..Limits::default()
        })
        .ledger(store)
        .build()
        .await
}

/// Runs the helper for ada until her cap pauses a run, printing the ledger
/// total before each run. The ledger file stays in the temp folder, so a
/// second start of the program pauses at once. Input: ANTHROPIC_API_KEY,
/// optional COURSE_LEDGER_DB. Output: one line per run on stdout.
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let db = std::env::var_os("COURSE_LEDGER_DB")
        .map_or_else(|| std::env::temp_dir().join("course-ledger.db"), Into::into);
    let store = Arc::new(SqliteStore::open(&db).await?);
    let agent = helper(store.clone()).await?;
    for run in 1..=12 {
        let spent = store.period_total(ADA).await?;
        match agent.run_with("Name one thing a spend cap protects.").await {
            Ok(outcome) => println!(
                "run {run}: ada had spent {spent} of {CAP}; this run cost {}",
                outcome.usage().cost_usd_micros
            ),
            Err(KitError::Paused { reason, .. }) => {
                println!(
                    "run {run}: ada had spent {spent} of {CAP}; paused before the call: {reason:?}"
                );
                return Ok(());
            }
            Err(other) => return Err(other.into()),
        }
    }
    println!("ada is still under her cap after 12 runs");
    Ok(())
}