Skip to content

fix: dedupe transcript-based token counts and use real input/output cost ratios - #13

Merged
coldhighsun merged 4 commits into
mainfrom
fix/dedupe-hourly-and-cost-ratio
Aug 11, 2026
Merged

fix: dedupe transcript-based token counts and use real input/output cost ratios#13
coldhighsun merged 4 commits into
mainfrom
fix/dedupe-hourly-and-cost-ratio

Conversation

@coldhighsun

Copy link
Copy Markdown
Owner

Summary

  • Dedupe assistant-message token usage across SessionParser, StatsCacheBuilder, HourlyActivityBuilder, and RecentActivityBuilder when a transcript carries multiple JSONL lines for the same assistant response (same message.id+requestId, or uuid as fallback). This fixes watch hours/recent-activity totals not matching the today/period summary tables.
  • UsageAnalyzer.GetDailySummary now estimates daily cost using each model's real observed input/output/cache-read/cache-creation ratio (from StatsCache.ModelUsage) instead of a fixed 25/25/50/0 guess, since the daily figures only carry a combined token total.
  • export command's model distribution now includes the input/output/cache token breakdown (JSON and CSV), not just the combined total.

Test plan

  • dotnet build AIUsageMonitor.slnx
  • dotnet test AIUsageMonitor.slnx (40/40 passing, including new dedup and cost-ratio tests)

…computing token stats

Claude Code transcripts can contain multiple JSONL lines carrying the
same assistant response (retried/streamed writes sharing message.id
and requestId), which was causing SessionParser and StatsCacheBuilder
to double-count their token usage.
…builders

HourlyActivityBuilder (watch hours view) and RecentActivityBuilder
(watch mode's rolling window) summed assistant-message token usage
straight from raw transcripts with no dedup, unlike StatsCacheBuilder
and SessionParser which already guard against duplicate transcript
lines for the same assistant response. This made watch mode's hourly
totals diverge from the today/period summary tables.
…atio

Claude Code's stats-cache.json only stores a combined token total per
model per day, so daily/period cost estimates were splitting that
total using a fixed 25/25/50/0 input/output/cache guess regardless of
the model's actual usage pattern. Use the model's observed cumulative
input/output/cache-read/cache-creation ratio from ModelUsage instead,
falling back to the old approximation only when that model has no
recorded usage.
…ribution

ExportModelDistribution previously only exposed the combined total
per model, so JSON/CSV exports couldn't distinguish input vs output
vs cache token usage.
@coldhighsun
coldhighsun merged commit f7fdde4 into main Aug 11, 2026
1 check passed
@coldhighsun
coldhighsun deleted the fix/dedupe-hourly-and-cost-ratio branch August 11, 2026 05:36
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.

1 participant