Repository navigation
fix(cache): cold-cache cost is wrong for Sonnet 5, Opus 5.5, Fable 5.2 and Haiku 5.5 #147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -87,9 +87,46 @@ pub fn claude_name(id: &str) -> Option<String> { | |
| }) | ||
| } | ||
|
|
||
| /// 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<f64> { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium · Invalid TTL silently uses five minutes When a caller passes a TTL other than Represent TTL as an enum (or reject unrecognized strings) so unsupported values cannot be priced as 5-minute writes. Prompt for AI agentsIn `src/models/builtin.rs` at line 102:
**Invalid TTL silently uses five minutes**
When a caller passes a TTL other than `"1h"` or `"5m"`, `cache_write_cost` treats every value other than `"1h"` as `"5m"` and returns a price, so a mistyped or unsupported TTL gives the caller a plausible but incorrect cost instead of signaling invalid input.
Represent TTL as an enum (or reject unrecognized strings) so unsupported values cannot be priced as 5-minute writes. |
||
| 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<f64> { | ||
| 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); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Medium · New Claude costs use old rates
When the model ID uses a provider-qualified spelling that
llm_prices::lookupdoes not recognize,list_pricestrips only the bracket suffix and passes the provider-qualified ID tollm_prices::lookup, which misses the canonical row;input_pricethen falls back to the family table, so newer Claude cache-write and cold-cache estimates use the old rate.Normalize provider-qualified IDs to their canonical Claude IDs before calling
llm_prices::lookup.Prompt for AI agents