fix: deduplicate token metrics by message.id to prevent multi-counting#462
Open
KorenKrita wants to merge 1 commit into
Open
fix: deduplicate token metrics by message.id to prevent multi-counting#462KorenKrita wants to merge 1 commit into
KorenKrita wants to merge 1 commit into
Conversation
Claude Code writes one JSONL transcript entry per content block (thinking, text, tool_use) within a single API response. Each entry carries the same message.id and a full copy of the usage snapshot with a non-null stop_reason. Both getTokenMetrics and collectSpeedMetricsFromLines were summing every assistant row without deduplication, causing token counts and speed metrics to be inflated by 1.4-2.5x depending on the number of content blocks per turn. Fix: dedupe by message.id in both paths. For speed metrics, later rows for the same id only extend the interval end timestamp. Rows without a message.id fall back to per-row counting for backwards compatibility.
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.
Problem
Claude Code writes one JSONL transcript entry per content block within a single API response. A typical assistant turn with thinking + text + tool_use produces 2–4 JSONL rows, each carrying:
message.idusagesnapshot (identicalinput_tokens,output_tokensacross all rows)stop_reason(e.g."tool_use"or"end_turn")Both
getTokenMetricsandcollectSpeedMetricsFromLinessum every assistant row that has ausagefield without checking whether multiple rows belong to the same API response. This causes token counts and speed metrics to be inflated by 1.4–2.5× depending on the average number of content blocks per turn.The existing
stop_reason-based dedup ingetTokenMetricsdoes not help because all rows from the same response carry the same non-nullstop_reason.Measured impact (real transcripts)
requestCountoutputTokensoutTpsVerified across 20 models with 50k+ assistant rows — all affected (1.36×–2.46× depending on model's tendency to produce multi-block responses).
Fix
Deduplicate by
message.idin both code paths:collectSpeedMetricsFromLines(speed metrics): maintain aMap<messageId, SpeedRequest>. First row per id seeds the request with usage and interval. Subsequent rows for the same id only extendinterval.endMsto the turn's final timestamp. Rows without amessage.idfall back to per-row counting.getTokenMetrics(token totals): single-pass filter that applies both the existingstop_reasoncheck and aSet<messageId>dedup, keeping only the first row per id.TranscriptLinetype: add optionalid?: stringtomessage.Testing
requestCountmatches the actual number of unique turns (confirmed viamessage.idgrouping)