Skip to content

fix(cost): session cost shows $0.00 or nothing — possibly usage-parsing gap with V4 reasoning modes #754

@Hmbown

Description

@Hmbown

Description

Session cost in the footer shows $0.00 or nothing. The user reports this happens specifically with reasoning_effort = "auto" but may be broader — cost may not calculate at all in some configurations. Needs investigation and fix for v0.8.14.

Investigation so far

The cost pipeline is:

  1. parse_usage() in client.rs:854 extracts input_tokens, output_tokens, prompt_cache_hit_tokens, prompt_cache_miss_tokens, reasoning_tokens from API response JSON
  2. Turn loop in turn_loop.rs:728 calls turn.add_usage(&usage) after each API call
  3. EngineEvent::TurnComplete { usage, .. } is emitted with the accumulated usage
  4. UI handler at ui.rs:945 calls calculate_turn_cost_from_usage(&app.model, &usage)
  5. pricing_for_model() in pricing.rs matches the model name to pricing tiers

What I verified works:

  • parse_usage handles both input_tokens/prompt_tokens and output_tokens/completion_tokens
  • stream_options.include_usage: true is set (verified at chat.rs:107)
  • Usage-only SSE chunks are correctly parsed (verified at chat.rs:1070)
  • pricing_for_model matches dated snapshots like deepseek-v4-pro-20260423 (contains "v4-pro")
  • auto_reasoning.rs does NOT touch model name or cost — it only selects reasoning effort tier
  • Tests pass for parse_usage with both legacy and V4 cached_tokens format

Potential causes to investigate:

  1. V4 with reasoning may not include thought tokens in completion_tokens. If reasoning tokens are billed but reported in a separate field that parse_usage does not capture into output_tokens, the cost will be undercounted. parse_usage reads reasoning_tokens from completion_tokens_details.reasoning_tokens but does NOT add them to output_tokens. If the API reports reasoning tokens exclusively in completion_tokens_details.reasoning_tokens and NOT in completion_tokens, output_tokens will be 0 → cost = $0.00.

  2. pricing_for_model returns None. If app.model does not contain "deepseek" (e.g. proxy setups, custom endpoints), cost silently becomes None and accrue_session_cost is never called.

  3. Usage from resumed sessions. Resumed sessions may have session_cost but the per-turn cost accrual might not trigger on the first resumed turn.

  4. Footer rendering. Cost might be calculated correctly but the footer rendering path (footer.rs) might not display $0.00 values, making it look like nothing.

Recommended fix approach

  1. Add reasoning_tokens to output_tokens in parse_usage if the field exists and output_tokens is 0 or does not include them
  2. Add a log/trace when cost calculation produces None (model not matched)
  3. Add a regression test: API response with only completion_tokens_details.reasoning_tokens and zero completion_tokens should produce non-zero cost

Environment

  • Version: v0.8.13
  • Model: deepseek-v4-pro with reasoning_effort = "auto"

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingrelease-blockerMust be fixed before the next releasev0.8.14Release v0.8.14

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions