Skip to content

Cost analysis panel and chat header can show different costs (dual computation path) #80

@holstein13

Description

@holstein13

Problem

The cost analysis panel (sessionAnalyzer.ts) and chat header (calculateMetrics() in jsonl.ts) compute costs independently. Even after #77 fixes the ~2x streaming overcounting, these two paths can still produce slightly different numbers because they run separate calculations with separate pricing logic.

This was observable before #77: the cost analysis panel showed $6.57 while the chat header showed $6.35 for the same session.

Root cause

sessionAnalyzer.ts recomputes costs from token counts using calculateMessageCost() in its message loop. The chat header uses detail.metrics.costUsd from calculateMetrics() in the main process. Two independent computations = two chances to diverge.

Suggested fix

Make calculateMetrics() the single source of truth for cost:

  1. sessionAnalyzer.ts should read detail.metrics.costUsd for parent cost instead of accumulating parentCost += callCost in the loop
  2. sessionAnalyzer.ts should read proc.metrics.costUsd for subagent cost instead of recomputing via calculateMessageCost()
  3. Remove the stale comment "proc.metrics.costUsd is not populated upstream" — it is populated
  4. Keep per-model stats.costUsd computation for the report table breakdown (display-only)

This was implemented in #79 (now closed in favor of #77) and can be layered on top of #77's streaming dedup work.

Reference

Acceptance criteria

  • sessionAnalyzer.ts uses detail.metrics.costUsd ?? 0 for parent cost
  • sessionAnalyzer.ts uses proc.metrics.costUsd ?? 0 for subagent cost
  • Cost analysis panel and chat header show identical costs for the same session
  • All existing tests pass

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions