feat(ai): Add cache write token cost calculation#5530
Merged
Conversation
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>
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>
loewenheim
approved these changes
Jan 14, 2026
Dav1dde
approved these changes
Jan 14, 2026
constantinius
approved these changes
Jan 14, 2026
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.
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:
Related to getsentry/sentry#106070
Contributes to TET-1674
Co-Authored-By: Claude Sonnet 4.5 noreply@anthropic.com