Skip to content

fix(cache): cold-cache cost is wrong for Sonnet 5, Opus 5.5, Fable 5.2 and Haiku 5.5 - #147

Open
Mattbusel wants to merge 1 commit into
Haleclipse:masterfrom
tensorust-ai:fix/cache-write-list-prices
Open

Mattbusel wants to merge 1 commit into
Haleclipse:masterfrom
tensorust-ai:fix/cache-write-list-prices

Conversation

@Mattbusel

@Mattbusel Mattbusel commented Nov 21, 2026 •

Copy link
Copy Markdown

cache.cold_cost prices a rewarm as input_price(family, version) x 1.25 (5m) or x 2 (1h) (projection.rs#L267). The family table in builtin.rs is right through Opus 4.8, then the newer models break it:

model input_price today list input price 1h write today 1h write, list
claude-sonnet-5, claude-sonnet-5-5 $3 $2 $6 $4
claude-opus-5-5 $5 $4 $10 $8
claude-fable-5-2 $10 $8 $20 $16
claude-haiku-5-5 $1 (the haiku >= 4.0 arm) $0.10 $2 $0.20

The big-context test 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:

  • new 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_price asks llm-prices first; the family table stays as the fallback, so ids it does not know (claude-3-haiku-20240307, -thinking variants) price exactly as before;
  • Claude Code's [1m] suffix is dropped before the lookup;
  • third-party models (Kimi, GLM) still return None.

llm-prices has no dependencies; Cargo.lock gains one 6-line entry.

Tests

cargo test: 195 lib tests pass, the 2 snapshot tests pass. prices_a_cold_cache now expects $2.16 for the Opus 5.5 case (the Opus 4.8 case is unchanged at $1.14), prices_by_family_and_version gains 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 on master too (it asserts the forward-slash path presets/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:

  • Correct prompt-cache cold and rewarm cost estimates by using model-specific cache-write pricing for supported Claude models.
  • Preserve the existing family/version pricing fallback for models not covered by the external price table.

Enhancements:

  • Source Claude input and cache-write prices from the llm-prices catalog, including long-context rates and normalized Claude Code model identifiers.

Build:

  • Add the llm-prices dependency.

Tests:

  • Update cache cost expectations and expand pricing coverage for newer Claude models, cache TTLs, long-context pricing, and fallback behavior.

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.
@sourcery-ai

sourcery-ai Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

API

Add model-specific cache-write pricing and prefer list prices for input estimates, retaining the family/version fallback for unknown model IDs.

  • src/models/builtin.rs L90-129

Uses of the API

Have 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.

  • src/metrics/projection.rs L267-272

Configuration

Add llm-prices as a dependency for model list-price lookups.

  • Cargo.toml L22

Tests

Update pricing expectations and add coverage for newer model list prices, cache-write rates, and fallback behavior.

  • src/metrics/projection.rs L466-468
  • src/models/builtin.rs L173-180, L190-202

Lock files and vendored code

Record the dependency addition in the lockfile.

  • Cargo.lock L145, L665-670

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread src/models/builtin.rs
/// 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)

Copy link
Copy Markdown

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::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`.

Comment thread src/models/builtin.rs
/// (`"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> {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 "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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant