-
Notifications
You must be signed in to change notification settings - Fork 210
Cost analysis panel and chat header can show different costs (dual computation path) #80
Description
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:
sessionAnalyzer.tsshould readdetail.metrics.costUsdfor parent cost instead of accumulatingparentCost += callCostin the loopsessionAnalyzer.tsshould readproc.metrics.costUsdfor subagent cost instead of recomputing viacalculateMessageCost()- Remove the stale comment "proc.metrics.costUsd is not populated upstream" — it is populated
- Keep per-model
stats.costUsdcomputation 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
- Fix cost overcounting — deduplicate streaming JSONL entries #74 — original cost overcounting issue
- fix: deduplicate streaming JSONL entries to prevent ~2x cost overcounting #77 — streaming dedup fix (addresses the ~2x inflation)
- fix: deduplicate streaming costs and unify cost source of truth #79 — streaming dedup + unified source of truth (closed, but contains the implementation)
Acceptance criteria
-
sessionAnalyzer.tsusesdetail.metrics.costUsd ?? 0for parent cost -
sessionAnalyzer.tsusesproc.metrics.costUsd ?? 0for subagent cost - Cost analysis panel and chat header show identical costs for the same session
- All existing tests pass