fix: dedupe transcript-based token counts and use real input/output cost ratios - #13
Merged
Merged
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
SessionParser,StatsCacheBuilder,HourlyActivityBuilder, andRecentActivityBuilderwhen a transcript carries multiple JSONL lines for the same assistant response (samemessage.id+requestId, oruuidas fallback). This fixeswatch hours/recent-activity totals not matching thetoday/period summary tables.UsageAnalyzer.GetDailySummarynow estimates daily cost using each model's real observed input/output/cache-read/cache-creation ratio (fromStatsCache.ModelUsage) instead of a fixed 25/25/50/0 guess, since the daily figures only carry a combined token total.exportcommand's model distribution now includes the input/output/cache token breakdown (JSON and CSV), not just the combined total.Test plan
dotnet build AIUsageMonitor.slnxdotnet test AIUsageMonitor.slnx(40/40 passing, including new dedup and cost-ratio tests)