🐛 Fix token usage cost calculation with granular input/output breakdown#80
Open
🐛 Fix token usage cost calculation with granular input/output breakdown#80
Conversation
The cost estimator was treating all tokens as input tokens because SessionListItem only sent a combined total_tokens field. Output tokens (3-5x more expensive) were priced at the input rate, understating cost. Server now sends input_tokens, output_tokens, and cached_tokens alongside total_tokens. Client uses the breakdown for accurate per-rate cost math with a legacy fallback for older servers.
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
input_tokens,output_tokens, andcached_tokensalongsidetotal_tokensinSessionListItem, giving the client the breakdown it needs for accurate cost math.resolvedCostnow uses the granular breakdown to price input/output/cached tokens at their respective rates, with a legacy fallback for older servers that only sendtotal_tokens.Root cause
SessionListItemmerged input+output into a singletotal_tokensfield and hardcodedtotal_cost_usd: 0.0. The client re-derived cost by treating the combined total as input tokens (inputTokens: totalTokens, outputTokens: 0), pricing everything at the input rate. Since output tokens are 3–5× more expensive, cost was systematically understated.Semantics decisions
startedAtis today (client-side filter) — matches user expectationusage_session_state)(input × input_rate) + (output × output_rate) + (cached × cache_read_rate)Closes #74