Expand description
§token-budget-pool
Shared token + dollar budget across N concurrent LLM tasks.
Drop a BudgetPool at the top of an agent run; pass &pool to every
task that issues LLM calls; call BudgetPool::record after each
response. The pool serializes the updates and returns
BudgetExceeded when a record would push past any cap.
§Example
use token_budget_pool::{BudgetPool, Caps};
let pool = BudgetPool::with_caps(Caps {
max_input_tokens: Some(10_000),
max_output_tokens: Some(5_000),
max_total_tokens: None,
max_cost_usd: Some(1.0),
});
pool.record(1_000, 500, 0.05).unwrap(); // fits
let err = pool.record(20_000, 0, 0.0).unwrap_err(); // input cap blown
assert!(format!("{err}").contains("input_tokens"));Structs§
- Budget
Exceeded - Error returned when a
recordcall would push past a cap. - Budget
Pool - Shared budget. Cheap to construct; record() takes a mutex.
- Caps
- Caps for a single pool. Any cap left as
Noneis unenforced. - Totals
- Running totals.