use crate::error::LlmWasmError;
use crate::types::Usage;
#[path = "../prices_data.rs"]
mod data;
pub const PRICES_SNAPSHOT_DATE: &str = data::SNAPSHOT_DATE;
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct ModelPricing {
pub provider: &'static str,
pub input_per_million: f64,
pub output_per_million: f64,
pub cache_read_per_million: Option<f64>,
pub cache_write_per_million: Option<f64>,
pub max_input_tokens: Option<u32>,
}
impl ModelPricing {
pub fn new(input_per_million: f64, output_per_million: f64) -> Self {
Self {
provider: "",
input_per_million,
output_per_million,
cache_read_per_million: None,
cache_write_per_million: None,
max_input_tokens: None,
}
}
pub fn with_provider(mut self, provider: &'static str) -> Self {
self.provider = provider;
self
}
pub fn with_cache_read(mut self, usd_per_million: f64) -> Self {
self.cache_read_per_million = Some(usd_per_million);
self
}
pub fn with_cache_write(mut self, usd_per_million: f64) -> Self {
self.cache_write_per_million = Some(usd_per_million);
self
}
pub fn with_max_input_tokens(mut self, tokens: u32) -> Self {
self.max_input_tokens = Some(tokens);
self
}
#[cfg(feature = "llm-prices")]
#[cfg_attr(docsrs, doc(cfg(feature = "llm-prices")))]
pub fn from_llm_prices(price: &llm_prices::Price) -> Option<Self> {
Some(Self {
provider: price.provider,
input_per_million: price.input?,
output_per_million: price.output.unwrap_or(0.0),
cache_read_per_million: price.cache_read,
cache_write_per_million: price
.cache_write("5m")
.or_else(|| price.cache_write.first().map(|&(_, usd)| usd)),
max_input_tokens: None,
})
}
pub fn cost_usd(&self, input_tokens: u32, output_tokens: u32) -> f64 {
self.cost(&Usage::new(
u64::from(input_tokens),
u64::from(output_tokens),
))
}
pub fn cost(&self, usage: &Usage) -> f64 {
let per = |tokens: u64, usd_per_million: f64| tokens as f64 * usd_per_million / 1_000_000.0;
per(usage.input, self.input_per_million)
+ per(
usage.cache_read,
self.cache_read_per_million
.unwrap_or(self.input_per_million),
)
+ per(
usage.cache_write,
self.cache_write_per_million
.unwrap_or(self.input_per_million),
)
+ per(usage.output, self.output_per_million)
+ per(usage.reasoning, self.output_per_million)
}
}
fn from_row(row: &data::Row) -> ModelPricing {
let &(_, provider, input, output, cache_read, cache_write, max_input) = row;
let listed = |v: f64| (!v.is_nan()).then_some(v);
ModelPricing {
provider,
input_per_million: input,
output_per_million: output,
cache_read_per_million: listed(cache_read),
cache_write_per_million: listed(cache_write),
max_input_tokens: (max_input != 0).then_some(max_input),
}
}
fn builtin(model: &str) -> Option<ModelPricing> {
let rows = data::ROWS;
let find = |name: &str| {
rows.binary_search_by(|row| row.0.cmp(name))
.ok()
.map(|i| from_row(&rows[i]))
};
find(model).or_else(|| model.split_once('/').and_then(|(_, bare)| find(bare)))
}
#[cfg(feature = "llm-prices")]
fn llm_prices_row(model: &str) -> Option<(&'static llm_prices::Price, ModelPricing)> {
let row = llm_prices::lookup(model)?;
let mut pricing = ModelPricing::from_llm_prices(row)?;
pricing.max_input_tokens = builtin(model).and_then(|p| p.max_input_tokens);
Some((row, pricing))
}
fn lookup(model: &str) -> Option<ModelPricing> {
#[cfg(feature = "llm-prices")]
if let Some((_, pricing)) = llm_prices_row(model) {
return Some(pricing);
}
builtin(model)
}
pub fn pricing_for_model(model: &str) -> Result<ModelPricing, LlmWasmError> {
lookup(model).ok_or_else(|| LlmWasmError::UnknownModel(model.to_string()))
}
pub fn cost_for_model(model: &str, usage: &Usage) -> Result<f64, LlmWasmError> {
#[cfg(feature = "llm-prices")]
if let Some((row, pricing)) = llm_prices_row(model) {
let lp = llm_prices::Usage {
input: usage.input,
output: usage.output.saturating_add(usage.reasoning),
cache_read: usage.cache_read,
cache_write_5m: usage.cache_write,
..llm_prices::Usage::default()
};
return Ok(row
.cost_usage(&lp)
.map_or_else(|| pricing.cost(usage), |c| c.total));
}
Ok(pricing_for_model(model)?.cost(usage))
}
pub fn context_window(model: &str) -> Option<u32> {
builtin(model).and_then(|p| p.max_input_tokens)
}
pub fn known_models() -> impl Iterator<Item = &'static str> {
data::ROWS.iter().map(|row| row.0)
}
#[derive(Debug, Default)]
pub struct CostLedger {
total_usd: f64,
entries: u32,
budget_usd: Option<f64>,
}
impl CostLedger {
pub fn new() -> Self {
Self::default()
}
pub fn with_budget(budget_usd: f64) -> Self {
Self {
budget_usd: Some(budget_usd),
..Self::default()
}
}
pub fn record(
&mut self,
model: &str,
input_tokens: u32,
output_tokens: u32,
) -> Result<f64, LlmWasmError> {
let cost = cost_for_model(
model,
&Usage::new(u64::from(input_tokens), u64::from(output_tokens)),
)?;
self.record_usd(cost)
}
pub fn record_usage(&mut self, model: &str, usage: &Usage) -> Result<f64, LlmWasmError> {
let cost = cost_for_model(model, usage)?;
self.record_usd(cost)
}
pub fn record_usd(&mut self, cost_usd: f64) -> Result<f64, LlmWasmError> {
check_cost(cost_usd)?;
let new_total = self.total_usd + cost_usd;
if let Some(budget) = self.budget_usd {
if new_total > budget {
return Err(LlmWasmError::BudgetExceeded {
used: new_total,
limit: budget,
});
}
}
self.total_usd = new_total;
self.entries = self.entries.saturating_add(1);
Ok(new_total)
}
pub fn record_paid_usd(&mut self, cost_usd: f64) -> Result<f64, LlmWasmError> {
check_cost(cost_usd)?;
self.total_usd += cost_usd;
self.entries = self.entries.saturating_add(1);
Ok(self.total_usd)
}
pub fn budget_usd(&self) -> Option<f64> {
self.budget_usd
}
pub fn total_usd(&self) -> f64 {
self.total_usd
}
pub fn remaining_usd(&self) -> Option<f64> {
self.budget_usd.map(|b| (b - self.total_usd).max(0.0))
}
pub fn exceeded_budget(&self) -> bool {
match self.budget_usd {
Some(budget) => self.total_usd > budget,
None => false,
}
}
pub fn entry_count(&self) -> u32 {
self.entries
}
}
fn check_cost(cost_usd: f64) -> Result<(), LlmWasmError> {
if cost_usd.is_finite() && cost_usd >= 0.0 {
return Ok(());
}
Err(LlmWasmError::InvalidConfig {
field: "cost_usd".into(),
reason: format!("must be a non-negative number, got {cost_usd}"),
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cost_ledger_record_known_model_ok() {
let mut ledger = CostLedger::new();
assert!(ledger.record("gpt-4o-mini", 1_000, 500).is_ok());
}
#[test]
fn test_cost_ledger_record_unknown_model_err() {
let mut ledger = CostLedger::new();
let result = ledger.record("unknown-model-xyz", 100, 50);
assert!(matches!(result, Err(LlmWasmError::UnknownModel(m)) if m == "unknown-model-xyz"));
}
#[test]
fn test_cost_ledger_total_accumulates() {
let mut ledger = CostLedger::new();
ledger.record("gpt-4o-mini", 1_000_000, 0).unwrap(); ledger.record("gpt-4o-mini", 1_000_000, 0).unwrap(); let total = ledger.total_usd();
assert!((total - 0.30).abs() < 1e-9, "expected 0.30, got {total}");
}
#[test]
fn test_cost_ledger_total_always_non_negative() {
let ledger = CostLedger::new();
assert!(ledger.total_usd() >= 0.0);
let mut ledger2 = CostLedger::new();
ledger2.record("gpt-4o-mini", 100, 50).unwrap();
assert!(ledger2.total_usd() >= 0.0);
}
#[test]
fn test_cost_ledger_exceeded_budget_false_under_limit() {
let mut ledger = CostLedger::with_budget(1.00);
ledger.record("gpt-4o-mini", 1_000, 500).unwrap();
assert!(!ledger.exceeded_budget());
}
#[test]
fn test_cost_ledger_exceeded_budget_true_over_limit() {
let mut ledger = CostLedger::with_budget(0.001);
let result = ledger.record("gpt-4o", 0, 1_000_000);
assert!(matches!(result, Err(LlmWasmError::BudgetExceeded { .. })));
}
#[test]
fn test_cost_ledger_no_budget_never_exceeded() {
let mut ledger = CostLedger::new();
ledger
.record("claude-opus-4-6", 100_000_000, 100_000_000)
.unwrap();
assert!(!ledger.exceeded_budget());
}
#[test]
fn test_cost_ledger_entry_count() {
let mut ledger = CostLedger::new();
assert_eq!(ledger.entry_count(), 0);
ledger.record("gpt-4o-mini", 100, 50).unwrap();
ledger.record("gpt-4o", 200, 100).unwrap();
assert_eq!(ledger.entry_count(), 2);
}
#[test]
fn test_pricing_for_all_known_models() {
for model in &[
"claude-opus-4-6",
"claude-sonnet-4-6",
"claude-haiku-4-5-20251001",
"gpt-4o",
"gpt-4o-mini",
] {
assert!(
pricing_for_model(model).is_ok(),
"expected pricing for {model}"
);
}
}
#[test]
fn test_pricing_cost_usd_calculation() {
let p = ModelPricing::new(3.0, 15.0);
let cost = p.cost_usd(1_000_000, 1_000_000);
assert!((cost - 18.0).abs() < 1e-9);
}
#[test]
fn test_rows_map_to_pricing_fields() {
let opus = pricing_for_model("claude-opus-5-5").unwrap();
assert_eq!(opus.provider, "anthropic");
assert_eq!(
(opus.input_per_million, opus.output_per_million),
(4.0, 20.0)
);
assert_eq!(
(opus.cache_read_per_million, opus.cache_write_per_million),
(Some(0.2), Some(5.0))
);
assert_eq!(opus.max_input_tokens, Some(1_000_000));
let gpt = pricing_for_model("gpt-4o").unwrap();
assert_eq!(
(
gpt.provider,
gpt.cache_read_per_million,
gpt.cache_write_per_million
),
("openai", Some(1.25), None)
);
for m in known_models() {
let p = pricing_for_model(m).unwrap();
assert!(
p.input_per_million.is_finite() && p.output_per_million.is_finite(),
"{m}"
);
assert!(p.cache_read_per_million.is_none_or(f64::is_finite), "{m}");
assert!(p.cache_write_per_million.is_none_or(f64::is_finite), "{m}");
assert!(!p.provider.is_empty(), "{m}");
}
}
#[test]
fn test_cost_fallbacks_and_reasoning() {
let plain = ModelPricing::new(2.0, 8.0);
let u = Usage::new(0, 0)
.with_cache_read(1_000_000)
.with_cache_write(1_000_000);
assert!((plain.cost(&u) - 4.0).abs() < 1e-12);
assert!(
(plain.cost(&Usage::new(0, 1_000_000).with_reasoning(1_000_000)) - 16.0).abs() < 1e-12
);
let cached = plain
.with_cache_read(0.5)
.with_cache_write(2.5)
.with_provider("x")
.with_max_input_tokens(9);
assert!((cached.cost(&u) - 3.0).abs() < 1e-12);
assert_eq!((cached.provider, cached.max_input_tokens), ("x", Some(9)));
assert_eq!(plain.cost(&Usage::default()), 0.0);
}
#[test]
fn test_prices_match_published_rates() {
let opus = pricing_for_model("claude-opus-4-6").unwrap();
assert_eq!(
(opus.input_per_million, opus.output_per_million),
(5.0, 25.0)
);
let haiku = pricing_for_model("claude-haiku-4-5-20251001").unwrap();
assert_eq!(
(haiku.input_per_million, haiku.output_per_million),
(1.0, 5.0)
);
let gpt = pricing_for_model("gpt-4o").unwrap();
assert_eq!((gpt.input_per_million, gpt.output_per_million), (2.5, 10.0));
}
#[test]
fn test_table_is_sorted_and_large() {
let names: Vec<&str> = known_models().collect();
assert!(names.len() > 100);
assert!(names.windows(2).all(|w| w[0] < w[1]));
assert_eq!(PRICES_SNAPSHOT_DATE.len(), 10);
}
#[test]
fn test_provider_prefix_and_other_vendors() {
assert_eq!(
pricing_for_model("anthropic/claude-sonnet-4-5").unwrap(),
pricing_for_model("claude-sonnet-4-5").unwrap()
);
assert!(known_models().any(|m| m.starts_with("gemini-")));
assert!(known_models().any(|m| m.starts_with("mistral-")));
assert!(pricing_for_model("nobody/nothing").is_err());
}
#[test]
fn test_record_usd_and_remaining() {
let mut ledger = CostLedger::with_budget(1.0);
assert!((ledger.record_usd(0.4).unwrap() - 0.4).abs() < 1e-12);
assert!(ledger.record_usd(-1.0).is_err());
assert!(ledger.record_usd(f64::NAN).is_err());
assert!(matches!(
ledger.record_usd(0.7),
Err(LlmWasmError::BudgetExceeded { .. })
));
assert!((ledger.remaining_usd().unwrap() - 0.6).abs() < 1e-12);
assert_eq!(ledger.entry_count(), 1);
assert_eq!(CostLedger::new().remaining_usd(), None);
}
#[test]
fn test_record_paid_usd_books_past_the_budget() {
let mut ledger = CostLedger::with_budget(0.10);
assert_eq!(ledger.budget_usd(), Some(0.10));
ledger.record_paid_usd(0.08).unwrap();
assert!(matches!(
ledger.record_usd(0.05),
Err(LlmWasmError::BudgetExceeded { .. })
));
assert!((ledger.record_paid_usd(0.05).unwrap() - 0.13).abs() < 1e-12);
assert!(ledger.exceeded_budget());
assert_eq!(ledger.remaining_usd(), Some(0.0));
assert_eq!(ledger.entry_count(), 2);
assert!(ledger.record_paid_usd(-0.01).is_err());
assert!(ledger.record_paid_usd(f64::INFINITY).is_err());
assert_eq!(ledger.entry_count(), 2);
assert_eq!(CostLedger::new().budget_usd(), None);
}
#[test]
fn test_cost_for_model_matches_ledger_and_errors() {
let u = Usage::new(3_000, 1_200)
.with_cache_read(180_000)
.with_cache_write(20_000);
let usd = cost_for_model("claude-opus-5-5", &u).unwrap();
assert!((usd - pricing_for_model("claude-opus-5-5").unwrap().cost(&u)).abs() < 1e-12);
let mut ledger = CostLedger::new();
assert!((ledger.record_usage("claude-opus-5-5", &u).unwrap() - usd).abs() < 1e-12);
assert!(matches!(
cost_for_model("no-such-model", &u),
Err(LlmWasmError::UnknownModel(_))
));
}
#[cfg(feature = "llm-prices")]
mod llm_prices_source {
use super::*;
#[test]
fn long_context_tier_is_billed_above_the_threshold() {
let long = Usage::new(300_000, 1_000);
let usd = cost_for_model("gpt-5.4", &long).unwrap();
assert!((usd - (1.5 + 0.0225)).abs() < 1e-12, "{usd}");
let flat = pricing_for_model("gpt-5.4").unwrap().cost(&long);
assert!((flat - (0.75 + 0.015)).abs() < 1e-12, "{flat}");
let mut ledger = CostLedger::new();
assert!((ledger.record("gpt-5.4", 300_000, 1_000).unwrap() - usd).abs() < 1e-12);
let short = Usage::new(1_000, 1_000).with_reasoning(500);
assert!(
(cost_for_model("gpt-5.4", &short).unwrap()
- pricing_for_model("gpt-5.4").unwrap().cost(&short))
.abs()
< 1e-12
);
}
#[test]
fn rows_keep_the_builtin_context_window() {
let p = pricing_for_model("gpt-5.4").unwrap();
assert_eq!(p.max_input_tokens, Some(1_050_000));
assert_eq!(context_window("gpt-5.4"), Some(1_050_000));
assert!(pricing_for_model("anthropic/claude-haiku-4-5-20251001").is_ok());
}
#[test]
fn models_llm_prices_lacks_fall_back_to_the_builtin_table() {
assert!(llm_prices::lookup("open-mistral-nemo").is_none());
let p = pricing_for_model("open-mistral-nemo").unwrap();
assert_eq!((p.provider, p.input_per_million), ("mistral", 0.3));
assert!(cost_for_model("open-mistral-nemo", &Usage::new(1_000_000, 0)).is_ok());
}
#[test]
fn cache_writes_on_rows_without_a_5m_price_use_the_flat_rate() {
let u = Usage::new(0, 0).with_cache_write(1_000_000);
assert!((cost_for_model("gpt-6-sol", &u).unwrap() - 2.5).abs() < 1e-12);
}
#[test]
fn from_llm_prices_maps_fields() {
let haiku =
ModelPricing::from_llm_prices(llm_prices::lookup("claude-haiku-4-5").unwrap())
.unwrap();
assert_eq!(
(haiku.cache_read_per_million, haiku.cache_write_per_million),
(Some(0.1), Some(1.25))
);
assert_eq!(haiku.max_input_tokens, None);
let embed = ModelPricing::from_llm_prices(
llm_prices::lookup("text-embedding-3-small").unwrap(),
)
.unwrap();
assert_eq!(embed.output_per_million, 0.0);
let mut no_input = *llm_prices::lookup("gpt-4o").unwrap();
no_input.input = None;
assert!(ModelPricing::from_llm_prices(&no_input).is_none());
}
}
}