Skip to content

feat(ai): Add cache write token cost calculation#5530

Merged
shellmayr merged 9 commits intomasterfrom
shellmayr/feat/gen-ai-use-cache-write-for-costs
Jan 14, 2026
Merged

feat(ai): Add cache write token cost calculation#5530
shellmayr merged 9 commits intomasterfrom
shellmayr/feat/gen-ai-use-cache-write-for-costs

Conversation

@shellmayr
Copy link
Copy Markdown
Member

Add support for calculating costs based on cache write tokens in addition to existing cache read tokens. This allows for more accurate cost tracking when AI models write data to their prompt cache.

Changes:

  • Add input_cache_write_per_token field to ModelCostV2 struct
  • Add input_cache_write_tokens field to UsedTokens struct
  • Update cost calculation to include cache write token costs
  • Add GEN_AI_USAGE_INPUT_CACHE_WRITE_TOKENS constant
  • Add test coverage for cache write cost calculation

Related to getsentry/sentry#106070
Contributes to TET-1674

Co-Authored-By: Claude Sonnet 4.5 noreply@anthropic.com

Add support for calculating costs based on cache write tokens in addition
to existing cache read tokens. This allows for more accurate cost tracking
when AI models write data to their prompt cache.

Changes:
- Add input_cache_write_per_token field to ModelCostV2 struct
- Add input_cache_write_tokens field to UsedTokens struct
- Update cost calculation to include cache write token costs
- Add GEN_AI_USAGE_INPUT_CACHE_WRITE_TOKENS constant
- Add test coverage for cache write cost calculation

The cost calculation now accounts for three types of input tokens:
- Raw input tokens (non-cached)
- Cached tokens (cache reads)
- Cache write tokens (writing to cache)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@linear
Copy link
Copy Markdown

linear bot commented Jan 12, 2026

shellmayr and others added 7 commits January 13, 2026 08:19
Ensure cost calculation works correctly when cache_write field is not
present in span data. This happens during rollout when old data doesn't
have cache_write tokens yet.

The system defaults cache_write_tokens to 0.0 when missing, ensuring no
impact on cost calculation during the transition period.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Fix clippy field_reassign_with_default warning by using struct
initialization syntax. Also update integration test to include the new
inputCacheWritePerToken field in model cost configuration.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@shellmayr shellmayr marked this pull request as ready for review January 14, 2026 12:10
@shellmayr shellmayr requested a review from a team as a code owner January 14, 2026 12:10
@shellmayr shellmayr requested a review from a team January 14, 2026 12:10
@shellmayr shellmayr added this pull request to the merge queue Jan 14, 2026
Merged via the queue into master with commit abbbf64 Jan 14, 2026
29 checks passed
@shellmayr shellmayr deleted the shellmayr/feat/gen-ai-use-cache-write-for-costs branch January 14, 2026 14:29
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.

4 participants