Skip to main content

Crate token_budget_pool

Crate token_budget_pool 

Source
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§

BudgetExceeded
Error returned when a record call would push past a cap.
BudgetPool
Shared budget. Cheap to construct; record() takes a mutex.
Caps
Caps for a single pool. Any cap left as None is unenforced.
Totals
Running totals.