feat: recognize provider cost data from usage.cost_details block#439
Merged
Conversation
Some providers include detailed cost breakdowns directly in the response usage object (e.g. usage.cost_details.input_cost, usage.cost_details.output_cost, etc.) rather than via SSE `: cost` comment lines. When present, these per-bucket breakdowns are more accurate than our proportional distribution from a total. Changes: - Add extractUsageCostDetails() to usage-normalizer for safe extraction of cost_details from provider usage blocks; returns null when absent - Add applyUsageCostDetails() to provider-cost for applying the per-bucket breakdown when available, with proportional fallback - Update normalizeOpenAIChatUsage() to extract cache_write_tokens from prompt_tokens_details (previously always 0) - Wire cost_details extraction into both streaming (UsageInspector) and non-streaming (finalizeUsage) cost paths - SSE `: cost` comments still take precedence over cost_details - Comprehensive test coverage for all new functions and edge cases
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.
Summary
Some providers include detailed cost breakdowns directly in the response
usageobject rather than via SSE: costcomment lines. This PR adds support for recognizing and using that cost data when available.The new format
Providers may return a
usageblock like:When
cost_detailsis present, we use the provider's actual per-bucket breakdown (input_cost,output_cost,cached_input_cost,cache_write_input_cost) directly instead of proportionally distributing a total.Changes
utils/usage-normalizer.ts: AddextractUsageCostDetails()— safely extractscost_detailsfrom provider usage blocks; returnsnullwhen absent (providers that don't use this format are unaffected). Also updatednormalizeOpenAIChatUsage()to extractcache_write_tokensfromprompt_tokens_details.utils/provider-cost.ts: AddapplyUsageCostDetails()— applies per-bucket breakdown when available, falls back to proportional distribution otherwise.services/inspectors/usage-logging.ts: Wirecost_detailsextraction into the streaming cost path (only applies if no SSE-reported cost was found).services/response-handler.ts: Same for the non-streaming (unary) path.Key design decisions
extractUsageCostDetails()returnsnullwhenusage.cost_detailsdoesn't exist — providers that don't use this format are completely unaffected: costcomments take precedence: The!providerReportedCostguard ensurescost_detailsonly applies when no SSE-reported cost was foundinput_cost/output_cost/etc., we use those directly instead of proportional splittingTest plan
extractUsageCostDetails— extracts from the new format, falls back tousage.cost/usage.estimated_cost, returns null for missing/invalid dataapplyUsageCostDetails— uses per-bucket breakdown, falls back to proportional, handles zero/null costsnormalizeOpenAIChatUsage— extractscache_write_tokensfromprompt_tokens_details: costcomments >cost_details> calculated costs