Expand description
Pre-flight USD cost gate for a single LLM call.
Before sending a request, estimate the worst-case cost (input tokens
at the input rate plus the requested max_output_tokens at the
output rate) and reject if it would exceed a configured per-call
cap. Catches the failure mode where an agent runs away and burns $20
on a single call before the call ever leaves the process.
§Example
use llm_cost_cap::{CostCap, CheckError};
let cap = CostCap::new(0.50);
match cap.check("claude-opus-4-7", 10_000, 4_000) {
Ok(est) => println!("ok, projected ${:.4}", est.total_usd),
Err(CheckError::CapExceeded(e)) => {
println!("rejected {}: ${:.4} > ${:.4}", e.model, e.projected_usd, e.cap_usd);
}
Err(CheckError::UnknownModel(e)) => panic!("missing model: {}", e.model),
}§Wrap a call
use llm_cost_cap::CostCap;
let cap = CostCap::new(10.0);
let result = cap.run("claude-sonnet-4-6", 500, 500, || "fake-response").unwrap();
assert_eq!(result, "fake-response");Structs§
- CapExceeded
- Returned when the estimated cost of a single call exceeds the cap.
- CostCap
- Pre-flight cost gate for a single LLM call.
- Estimated
Cost - A breakdown of the per-call estimate for one model. All four fields
are USD;
total_usd == input_usd + output_usd + cached_input_usd. - Model
Price - Per-million-token pricing for one model.
- Unknown
Model - Returned when a model id is not in the price table.
Enums§
- Check
Error - Errors returned by
CostCap::check. - Estimate
Error - Errors returned by
CostCap::estimatewhen the model is unknown or token counts are negative. (Negative counts are not representable withu64, but the model lookup can still fail.)
Constants§
- MODEL_
PRICES - Flat slice of (model, input_per_million_usd, output_per_million_usd)
for callers that want a quick read of the canonical table. Cached
rates are dropped; use
builtin_pricesfor the full picture.
Functions§
- builtin_
prices - Return a fresh map of the built-in price table with aliases resolved.
- known_
models - Sorted list of model ids (including aliases) the built-in table knows.