llm_cost_cap/lib.rs
1//! Pre-flight USD cost gate for a single LLM call.
2//!
3//! Before sending a request, estimate the worst-case cost (input tokens
4//! at the input rate plus the requested `max_output_tokens` at the
5//! output rate) and reject if it would exceed a configured per-call
6//! cap. Catches the failure mode where an agent runs away and burns $20
7//! on a single call before the call ever leaves the process.
8//!
9//! # Example
10//!
11//! ```
12//! use llm_cost_cap::{CostCap, CheckError};
13//!
14//! let cap = CostCap::new(0.50);
15//! match cap.check("claude-opus-4-7", 10_000, 4_000) {
16//! Ok(est) => println!("ok, projected ${:.4}", est.total_usd),
17//! Err(CheckError::CapExceeded(e)) => {
18//! println!("rejected {}: ${:.4} > ${:.4}", e.model, e.projected_usd, e.cap_usd);
19//! }
20//! Err(CheckError::UnknownModel(e)) => panic!("missing model: {}", e.model),
21//! }
22//! ```
23//!
24//! # Wrap a call
25//!
26//! ```
27//! use llm_cost_cap::CostCap;
28//!
29//! let cap = CostCap::new(10.0);
30//! let result = cap.run("claude-sonnet-4-6", 500, 500, || "fake-response").unwrap();
31//! assert_eq!(result, "fake-response");
32//! ```
33
34mod cap;
35mod prices;
36
37pub use cap::{
38 CapExceeded, CheckError, CostCap, EstimateError, EstimatedCost, UnknownModel,
39};
40pub use prices::{builtin_prices, known_models, ModelPrice, MODEL_PRICES};