✨ feat(agents): token badge shows ↑context ↓output (bounded, not a 22M cumulative total) - #44
Merged
Merged
Conversation
The old badge summed input across every turn, so a long session read as "22M tok" —
technically the total processed, but alarming and not what Claude Code shows. Reshape
TokenUsage to two meaningful numbers:
↑ context = the LATEST turn's input (input + cache read + cache create) = how full the
context window is right now (bounded, ~≤ the model's window)
↓ output = cumulative output generated (a monotonic "how much this agent produced")
addLine now overwrites context to each assistant line's input and accumulates output, so
after a read context reflects the last turn. Drops the now-unused cumulative sum + addUsage.
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.
Follow-up to #43 (merged). Live testing showed a long session's badge reading
22M tok— that was the cumulative sum of input across every turn: technically real, but alarming and not what Claude Code shows. This reshapes the badge to the two numbers you'd actually want, matching Claude's ↑↓.Change
TokenUsagebecomes two meaningful values:input + cache_read + cache_create) = how full the context window is right now. Bounded (~≤ the model window), not cumulative.addLinenow overwrites context to each assistant line's input and accumulates output, so after a readcontextreflects the last turn. Badge renders e.g.↑148k ↓1.2k(tooltip:↑ 148k context · ↓ 7.4M generated). Dropped the now-dead cumulative-sum path +addUsage.Perf (measured, since it was asked)
The transcript read stays off the terminal hot path — main-process hook channel,
fs.promiseson the libuv threadpool, incremental by byte-offset, and parsed in bounded slices thatawait setImmediatebetween them. Benchmarked on a real 90.9 MB / 29,647-line transcript:~4.6 ms is under a third of a 16 ms frame, and only on the first read of a resumed session (a
Stop, seconds-apart). Steady-state incremental reads (a turn's new KB) are sub-millisecond. Reduced the slice size 1 MiB → 256 KiB for the comfortable margin.Tests
make check+typecheckgreen — 427 tests. Updated:addLine/TranscriptTokensassert the new{context, output}(incl. context-tracks-latest, output-accumulates, chunk-boundary + multibyte offset correctness);tokenBreakdown/badge assert↑…k ↓…k.