Repository navigation
Conversation
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.
Reviewer's GuideAPIAdd model-specific cache-write pricing and prefer list prices for input estimates, retaining the family/version fallback for unknown model IDs.
Uses of the APIHave cache rewarm estimates use the model’s 5-minute or 1-hour cache-write rate instead of multiplying its input price by a fixed factor.
ConfigurationAdd
TestsUpdate pricing expectations and add coverage for newer model list prices, cache-write rates, and fallback behavior.
Lock files and vendored codeRecord the dependency addition in the lockfile.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 2 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="src/models/builtin.rs" line_range="95" />
<code_context>
+/// 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)
+}
+
</code_context>
<issue_to_address>
**New Claude costs use old rates**
When the model ID uses a provider-qualified spelling that `llm_prices::lookup` does not recognize, `list_price` strips only the bracket suffix and passes the provider-qualified ID to `llm_prices::lookup`, which misses the canonical row; `input_price` then 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`.
</issue_to_address>
### Comment 2
<location path="src/models/builtin.rs" line_range="102" />
<code_context>
+/// (`"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> {
+ if let Some(p) = list_price(id) {
+ let usage = if ttl == "1h" {
</code_context>
<issue_to_address>
**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.
</issue_to_address>Sourcery assessment
Approval pending. 2 findings to address first.
Blocking findings: src/models/builtin.rs:95, src/models/builtin.rs:102
| /// 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) |
There was a problem hiding this comment.
🟡 Medium · New Claude costs use old rates
When the model ID uses a provider-qualified spelling that llm_prices::lookup does not recognize, list_price strips only the bracket suffix and passes the provider-qualified ID to llm_prices::lookup, which misses the canonical row; input_price then 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
In `src/models/builtin.rs` at line 95:
**New Claude costs use old rates**
When the model ID uses a provider-qualified spelling that `llm_prices::lookup` does not recognize, `list_price` strips only the bracket suffix and passes the provider-qualified ID to `llm_prices::lookup`, which misses the canonical row; `input_price` then 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`.| /// (`"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.
🟡 Medium · 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.
Prompt for AI agents
In `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.
cache.cold_costprices a rewarm asinput_price(family, version) x 1.25(5m) orx 2(1h) (projection.rs#L267). The family table in builtin.rs is right through Opus 4.8, then the newer models break it:input_pricetodayclaude-sonnet-5,claude-sonnet-5-5claude-opus-5-5claude-fable-5-2claude-haiku-5-5haiku >= 4.0arm)The
big-contexttest pins it: 270k tokens of Opus 5.5 with the 1-hour TTL asserts$2.70; Anthropic bills that write at $8/M, so $2.16.A family/version match cannot keep up, because Anthropic no longer prices by family: Opus went $15 -> $5 -> $4 within one family name, and Haiku 5.5 is a tenth of Haiku 4.5. This PR reads the price from
llm-prices, a table of list prices in which every row links the provider's pricing page and the date it was checked:builtin::cache_write_cost(id, ttl, tokens)uses the model's own 5m / 1h cache-write price, including the long-context rate when a write crosses the model's threshold;input_priceasksllm-pricesfirst; the family table stays as the fallback, so ids it does not know (claude-3-haiku-20240307,-thinkingvariants) price exactly as before;[1m]suffix is dropped before the lookup;None.llm-priceshas no dependencies; Cargo.lock gains one 6-line entry.Tests
cargo test: 195 lib tests pass, the 2 snapshot tests pass.prices_a_cold_cachenow expects $2.16 for the Opus 5.5 case (the Opus 4.8 case is unchanged at $1.14),prices_by_family_and_versiongains Sonnet 5, Opus 5.5[1m], Haiku 5.5 and Fable 5.2 rows, and a new test pins Opus 4.8 cache writes at $6.25 / $10 per 1M and the fallback path. One lib test,tui::pages::presets::tests::saves_renames_replaces_and_deletes_your_presets, fails on my Windows machine onmastertoo (it asserts the forward-slash pathpresets/work-laptop.toml); this PR does not touch it.Disclosure: I maintain llm-prices.
Summary by Sourcery
Use model-specific pricing to calculate accurate Claude prompt-cache costs while retaining compatibility for unknown model identifiers.
Bug Fixes:
Enhancements:
Build:
Tests: