Skip to content

fix(spans): Stop double-billing AI cache-write tokens in cost calculation#6090

Merged
Dav1dde merged 3 commits into
getsentry:masterfrom
tianwater-QAQ:fix/ai-cache-write-double-billing
Jun 16, 2026
Merged

fix(spans): Stop double-billing AI cache-write tokens in cost calculation#6090
Dav1dde merged 3 commits into
getsentry:masterfrom
tianwater-QAQ:fix/ai-cache-write-double-billing

Conversation

@tianwater-QAQ

@tianwater-QAQ tianwater-QAQ commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Fixes #6084

UsedTokens::raw_input_tokens() subtracted only input_cached_tokens from input_tokens. Per the field docs, input_cache_write_tokens is also a subset of input_tokens, so cache-write tokens were billed twice: once at the standard input rate (they stayed inside raw_input_tokens) and again at the cache-write rate in calculate_costs.

This subtracts input_cache_write_tokens as well, mirroring the existing cached-token handling, so each token class is charged exactly once at its own rate.

Change

  • raw_input_tokens() now returns input_tokens - input_cached_tokens - input_cache_write_tokens.
  • Updated the test_calculate_cost_with_cache_writes snapshot to the corrected value, with the arithmetic inline:
    (100 - 20 - 30) * 1.0 + 20 * 0.5 + 30 * 0.75 = 82.5 (was 112.5).

Other cost tests are unaffected because they use input_cache_write_tokens: 0.0.


Transparency note: I don't currently have a local Rust toolchain set up, so I haven't run the test suite locally — I'm relying on CI. The change is a one-line arithmetic fix plus the corresponding snapshot update shown above; happy to iterate if anything in CI is off.

Legal Boilerplate

Look, I get it. The entity doing business as "Sentry" was incorporated in the State of Delaware in 2015 as Functional Software, Inc. and is gonna need some rights from me in order to utilize my contributions in this here PR. So here's the deal: I retain all rights, title and interest in and to my contributions, and by keeping this boilerplate intact I confirm that Sentry can use, modify, copy, and redistribute my contributions, under Sentry's choice of terms.

`UsedTokens::raw_input_tokens()` subtracted only `input_cached_tokens`
from `input_tokens`, but `input_cache_write_tokens` is also a subset of
`input_tokens`. As a result cache-write tokens were billed twice: once at
the standard input rate (because they remained inside `raw_input_tokens`)
and again at the cache-write rate in `calculate_costs`.

Subtract `input_cache_write_tokens` as well so each token class is charged
exactly once at its own rate, mirroring the existing cached-token handling.

Updates the `test_calculate_cost_with_cache_writes` snapshot accordingly
(input 112.5 -> 82.5).

Fixes getsentry#6084

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@tianwater-QAQ tianwater-QAQ requested a review from a team as a code owner June 15, 2026 10:39
@Dav1dde Dav1dde requested a review from a team June 15, 2026 11:11

@Dav1dde Dav1dde left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Legal boilerplate missing in the description.

@tianwater-QAQ

Copy link
Copy Markdown
Contributor Author

Thanks for the review @Dav1dde — I've added the Legal Boilerplate to the description.

@Dav1dde

Dav1dde commented Jun 15, 2026

Copy link
Copy Markdown
Member

@tianwater-QAQ thanks! Will wait for a review from @getsentry/telemetry-experience team as they know the implications of that change.

@tianwater-QAQ tianwater-QAQ changed the title fix(spans): stop double-billing AI cache-write tokens in cost calculation fix(spans): Stop double-billing AI cache-write tokens in cost calculation Jun 15, 2026
@vgrozdanic vgrozdanic self-assigned this Jun 15, 2026

@vgrozdanic vgrozdanic left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @tianwater-QAQ 🙏

@Dav1dde we still need your approval because of codeowners, but the change is correct and fixes the bug

@Dav1dde Dav1dde added this pull request to the merge queue Jun 16, 2026
@Dav1dde

Dav1dde commented Jun 16, 2026

Copy link
Copy Markdown
Member

@tianwater-QAQ thanks!

Merged via the queue into getsentry:master with commit 3694b33 Jun 16, 2026
31 checks passed
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.

AI cost calc double-counts cache-write tokens in relay-event-normalization

3 participants