fix(spans): Stop double-billing AI cache-write tokens in cost calculation#6090
Merged
Dav1dde merged 3 commits intoJun 16, 2026
Merged
Conversation
`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>
Dav1dde
requested changes
Jun 15, 2026
Dav1dde
left a comment
Member
There was a problem hiding this comment.
Legal boilerplate missing in the description.
Contributor
Author
|
Thanks for the review @Dav1dde — I've added the Legal Boilerplate to the description. |
Member
|
@tianwater-QAQ thanks! Will wait for a review from @getsentry/telemetry-experience team as they know the implications of that change. |
vgrozdanic
approved these changes
Jun 15, 2026
vgrozdanic
left a comment
Member
There was a problem hiding this comment.
Thanks @tianwater-QAQ 🙏
@Dav1dde we still need your approval because of codeowners, but the change is correct and fixes the bug
Dav1dde
approved these changes
Jun 16, 2026
Member
|
@tianwater-QAQ thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #6084
UsedTokens::raw_input_tokens()subtracted onlyinput_cached_tokensfrominput_tokens. Per the field docs,input_cache_write_tokensis also a subset ofinput_tokens, so cache-write tokens were billed twice: once at the standard input rate (they stayed insideraw_input_tokens) and again at the cache-write rate incalculate_costs.This subtracts
input_cache_write_tokensas well, mirroring the existing cached-token handling, so each token class is charged exactly once at its own rate.Change
raw_input_tokens()now returnsinput_tokens - input_cached_tokens - input_cache_write_tokens.test_calculate_cost_with_cache_writessnapshot to the corrected value, with the arithmetic inline:(100 - 20 - 30) * 1.0 + 20 * 0.5 + 30 * 0.75 = 82.5(was112.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.