From 4b601a5d1c5fb3ce08b6dbbc488f89d45da70c22 Mon Sep 17 00:00:00 2001 From: Matthew Charles Busel Date: Fri, 20 Nov 2026 19:08:09 -0500 Subject: [PATCH] fix(cache): price cache rewarm at each model's own cache-write rate cache.cold_cost multiplied a family input price by 1.25x/2x. The family table is right for Opus 4.5-4.8 but not for the models shipped since: Sonnet 5 is $2 (not $3), Opus 5.5 $4 (not $5), Fable 5.2 $8 (not $10), Haiku 5.5 $0.10 (not $1). The Opus 5.5 1-hour test pinned $2.70 for a write that bills $2.16. Prices now come from llm-prices (Anthropic list prices, each row dated and linked to the pricing page), using the model's own 5m/1h cache-write price. The family table stays as the fallback for ids it does not know. --- Cargo.lock | 7 +++++ Cargo.toml | 1 + src/metrics/projection.rs | 14 +++++----- src/models/builtin.rs | 58 +++++++++++++++++++++++++++++++++++++-- 4 files changed, 71 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 02ce5895..b2eef1aa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -142,6 +142,7 @@ dependencies = [ "clap", "dirs", "insta", + "llm-prices", "ratatui", "regex", "serde", @@ -661,6 +662,12 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4744e383959f0db86ede514b809b1c53251889093803c05267acc7d4e7030d70" +[[package]] +name = "llm-prices" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8687beff4d5ceb2b1a5fc5f242df4f2c107ca92609f47d30fab584591157d9dd" + [[package]] name = "lock_api" version = "0.4.14" diff --git a/Cargo.toml b/Cargo.toml index b49882c4..9267d0ca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ toml = "1.0" chrono = "0.4" dirs = "6.0" regex = "1.0" +llm-prices = "1.8" unicode-width = "0.2" unicode-segmentation = "1.12" ratatui = { version = "0.30", default-features = false, features = ["crossterm", "layout-cache"] } diff --git a/src/metrics/projection.rs b/src/metrics/projection.rs index 1f39196e..2d6f2bba 100644 --- a/src/metrics/projection.rs +++ b/src/metrics/projection.rs @@ -264,12 +264,12 @@ fn cost_today(ctx: &Ctx, _: &Options) -> Option { pub(super) fn rewarm_cost(ctx: &Ctx) -> Option<(f64, u64)> { let pc = ctx.input.prompt_cache.as_ref()?; let tokens = pc.recache_tokens_if_cold.filter(|t| *t > 0)?; - let price = builtin::input_price(ctx.input.model_id()?)?; - let write = match pc.ttl.as_deref() { - Some("1h") => 2.0, - _ => 1.25, + let ttl = match pc.ttl.as_deref() { + Some("1h") => "1h", + _ => "5m", }; - Some((tokens as f64 * price * write / 1_000_000.0, tokens)) + let usd = builtin::cache_write_cost(ctx.input.model_id()?, ttl, tokens)?; + Some((usd, tokens)) } fn cold_cost(ctx: &Ctx, _: &Options) -> Option { @@ -463,9 +463,9 @@ mod tests { // 182k tokens of Opus 4.8 at $5/M, written with the 5-minute TTL. let r = reading("cache.cold_cost", "near-full").unwrap(); assert_eq!(field(&r, "usd"), "$1.14"); - // 270k tokens of Opus 5.5 written with the 1-hour TTL. + // 270k tokens of Opus 5.5 written with the 1-hour TTL, at $8/M. let big = reading("cache.cold_cost", "big-context").unwrap(); - assert_eq!(field(&big, "usd"), "$2.70"); + assert_eq!(field(&big, "usd"), "$2.16"); assert!(reading("cache.cold_cost", "third-party").is_none()); } diff --git a/src/models/builtin.rs b/src/models/builtin.rs index 2c5b1e8d..74988296 100644 --- a/src/models/builtin.rs +++ b/src/models/builtin.rs @@ -87,9 +87,46 @@ pub fn claude_name(id: &str) -> Option { }) } +/// The model's row in `llm_prices` (Anthropic's list prices, each row dated and +/// linked to the pricing page). Claude Code's context-window suffix (`[1m]`) is +/// not part of the API id, so it is dropped before the lookup. +fn list_price(id: &str) -> Option<&'static llm_prices::Price> { + let id = id.split('[').next().unwrap_or(id).trim(); + llm_prices::lookup(id) +} + +/// Cost in USD of writing `tokens` to the prompt cache with the given TTL +/// (`"5m"` or `"1h"`). Uses the model's own cache-write price, including its +/// long-context rate when the write crosses the threshold; for models +/// `llm_prices` does not know, 1.25× (5 minutes) or 2× (1 hour) the input price. +pub fn cache_write_cost(id: &str, ttl: &str, tokens: u64) -> Option { + if let Some(p) = list_price(id) { + let usage = if ttl == "1h" { + llm_prices::Usage { + cache_write_1h: tokens, + ..Default::default() + } + } else { + llm_prices::Usage { + cache_write_5m: tokens, + ..Default::default() + } + }; + if let Some(c) = p.cost_usage(&usage) { + return Some(c.total); + } + } + let write = if ttl == "1h" { 2.0 } else { 1.25 }; + Some(tokens as f64 * input_price(id)? * write / 1_000_000.0) +} + /// List price of uncached input tokens in USD per million, for estimates. -/// Cache writes cost 1.25× this with the 5-minute TTL and 2× with 1 hour. +/// Comes from `llm_prices` when it knows the model, else from the family and +/// version below. pub fn input_price(id: &str) -> Option { + if let Some(price) = list_price(id).and_then(|p| p.input) { + return Some(price); + } let m = claude_model(id)?; let version = (m.major, m.minor.unwrap_or(0)); match m.family.as_str() { @@ -133,10 +170,14 @@ mod tests { fn prices_by_family_and_version() { let cases = [ ("claude-opus-4-8", Some(5.0)), + ("claude-opus-5-5[1m]", Some(4.0)), ("claude-opus-4-1-20250805", Some(15.0)), ("claude-3-opus-20240229", Some(15.0)), - ("claude-sonnet-5", Some(3.0)), + ("claude-sonnet-5", Some(2.0)), + ("claude-sonnet-4-5-20250929", Some(3.0)), ("claude-haiku-4-5", Some(1.0)), + ("claude-haiku-5-5", Some(0.1)), + ("claude-fable-5-2", Some(8.0)), ("claude-3-5-haiku-20241022", Some(0.8)), ("claude-3-haiku-20240307", Some(0.25)), ("kimi-k2-turbo-preview", None), @@ -146,6 +187,19 @@ mod tests { } } + #[test] + fn prices_cache_writes_at_the_model_rate() { + // Opus 4.8: $6.25/M for 5-minute writes, $10/M for 1-hour writes. + let five = cache_write_cost("claude-opus-4-8", "5m", 1_000_000).unwrap(); + assert!((five - 6.25).abs() < 1e-9, "{five}"); + let hour = cache_write_cost("claude-opus-4-8", "1h", 1_000_000).unwrap(); + assert!((hour - 10.0).abs() < 1e-9, "{hour}"); + // Not in the list-price table: falls back to the family input price. + let old = cache_write_cost("claude-3-haiku-20240307", "5m", 1_000_000).unwrap(); + assert!((old - 0.3125).abs() < 1e-9, "{old}"); + assert!(cache_write_cost("kimi-k2-turbo-preview", "5m", 1_000).is_none()); + } + #[test] fn ignores_non_claude_ids() { assert_eq!(claude_name("kimi-k2-0905-preview"), None);