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;
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
}
#[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(())
}