Skip to main content

Crate llm_cost_cap

Crate llm_cost_cap 

Source
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.
EstimatedCost
A breakdown of the per-call estimate for one model. All four fields are USD; total_usd == input_usd + output_usd + cached_input_usd.
ModelPrice
Per-million-token pricing for one model.
UnknownModel
Returned when a model id is not in the price table.

Enums§

CheckError
Errors returned by CostCap::check.
EstimateError
Errors returned by CostCap::estimate when the model is unknown or token counts are negative. (Negative counts are not representable with u64, 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_prices for 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.