Skip to content

🐛 Fix token usage cost calculation with granular input/output breakdown#80

Open
Robdel12 wants to merge 1 commit intomainfrom
mission/robdel12-orbitdock#74
Open

🐛 Fix token usage cost calculation with granular input/output breakdown#80
Robdel12 wants to merge 1 commit intomainfrom
mission/robdel12-orbitdock#74

Conversation

@Robdel12
Copy link
Owner

Summary

  • Server now sends input_tokens, output_tokens, and cached_tokens alongside total_tokens in SessionListItem, giving the client the breakdown it needs for accurate cost math.
  • Client resolvedCost now uses the granular breakdown to price input/output/cached tokens at their respective rates, with a legacy fallback for older servers that only send total_tokens.
  • Tests added for the corrected cost path and the legacy fallback.

Root cause

SessionListItem merged input+output into a single total_tokens field and hardcoded total_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

Concept Definition
Today Sessions whose startedAt is today (client-side filter) — matches user expectation
Token totals Lifetime input + output for the session (cumulative from usage_session_state)
Cached tokens Reported separately; billed at cache-read rate, not double-counted in input
Cost (input × input_rate) + (output × output_rate) + (cached × cache_read_rate)

Closes #74

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.
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.

🔬 Scope and fix confusing token usage accounting and breakdown

1 participant