Skip to main content

Module budget

Module budget 

Source
Expand description

Cumulative USD budget tracking for Claude sessions.

A BudgetTracker accumulates cost across turns and fires caller-supplied callbacks when configurable thresholds are crossed. When a max_usd ceiling is set, BudgetTracker::check returns Error::BudgetExceeded once the total is at or above the ceiling, giving callers a hard stop before the next CLI invocation.

§Ownership and sharing

BudgetTracker wraps its state in Arc<Mutex<...>> so clones share the same running total and fire-once flags. Attach one tracker to a Session, or clone it across several sessions to enforce a fleet-wide ceiling.

§Example

use std::sync::Arc;
use claude_wrapper::{Claude, BudgetTracker};
use claude_wrapper::session::Session;

let budget = BudgetTracker::builder()
    .max_usd(5.00)
    .warn_at_usd(4.00)
    .on_warning(|total| eprintln!("warning: ${total:.2} spent"))
    .on_exceeded(|total| eprintln!("budget hit: ${total:.2}"))
    .build();

let claude = Arc::new(Claude::builder().build()?);
let mut session = Session::new(claude).with_budget(budget.clone());

session.send("hello").await?;
println!("spent so far: ${:.4}", budget.total_usd());

Structs§

BudgetBuilder
Builder for BudgetTracker.
BudgetTracker
Cumulative USD budget tracker with threshold callbacks.