Token accounting: dedupe fallback by message.id + fresh-vs-cached headline#11
Open
wan-huiyan wants to merge 2 commits into
Open
Token accounting: dedupe fallback by message.id + fresh-vs-cached headline#11wan-huiyan wants to merge 2 commits into
wan-huiyan wants to merge 2 commits into
Conversation
…absent (#5) `deduplicateAssistant` collapses streaming assistant chunks (which share a requestId and each report the same usage) so tokens aren't counted ~2-3x. But assistant rows that OMIT requestId — older Claude Code versions or partial transcripts — hit the `!msg.requestId` guard and passed straight through un-grouped, re-introducing the exact inflation this function exists to prevent. Those chunks still share `message.id`, so group by `requestId ?? message.id`. Rows with neither key still pass through unchanged; behaviour for requestId- bearing transcripts is identical. - types.ts: add optional `message.id` - parser.ts: key dedup on `requestId ?? message.id`; extract a `flush()` helper - parser.test.ts: +2 cases (merge no-requestId chunks sharing message.id; do NOT merge no-requestId chunks with different message.ids) - CHANGELOG: Unreleased entry Defensive: current Claude Code transcripts carry requestId on every assistant row (verified against a real 150-response session — dedup already correct there), so this closes a latent edge rather than a live miscount. Full suite 89 passing; tsc clean. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The "NN in" headline sums input + cache_read + cache_creation, which on a long multi-turn session is ~97% cheap cache_read — so a user sees an alarming "37M in / $80" without realizing almost none of it is freshly-billed input. Add an `Input X new · Y cached` line that splits freshly-billed input (input + cache_creation, at $15/M + $18.75/M) from cache reads ($1.50/M), so the cost line is interpretable. Single-session and live views; +2 tests. 89 pass.
This was referenced May 29, 2026
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.
Two related token-reporting changes:
message.id—deduplicateAssistantgroups streaming chunks byrequestId, but transcripts that omitrequestId(older Claude Code / partial logs) bypassed it and re-introduced the ~2-3× token inflation. Chunks still sharemessage.id, so it now keys onrequestId ?? message.id.cache_read; a newInput X new · Y cachedline separates freshly-billed input from cache reads so the cost is interpretable. Headline total unchanged.91 tests pass,
tscclean. Supersedes #9 (consolidated with the dedup fix here).🤖 Generated with Claude Code