Repository navigation
feat(widgets): add Tokens Last Turn widget for per-message token usage - #1
Conversation
Implements sirmalloc#44: a widget showing token usage for the most recent assistant message only. The usage is collected in the shared single-pass transcript scan (getTranscriptAnalysis) behind an includeLastTurnTokens opt-in and is deduplicated by message.id: Claude Code writes one JSONL entry per content block of a single API call, all sharing one message.id, so entries are grouped per id and only the highest output_tokens is kept instead of summing every entry (sirmalloc#549). Streaming partials resolve to the finalized value, and the in-flight call still counts while it streams. Co-Authored-By: Claude Code <noreply@anthropic.com>
axisrow
left a comment
There was a problem hiding this comment.
Requesting changes (one inline finding).
The sirmalloc#549 deduplication itself is correct and well tested: per-content-block entries of one message id collapse to the max output_tokens, streaming partials resolve to the finalized value, id-less entries each form their own group, and the field is opt-in so cumulative widgets are untouched. The widget mirrors TokensTotal conventions (raw value, zero hide state, number format, preview) and is wired through the single widget manifest, so the TUI picks it up automatically.
Substantive issue (inline): last-turn tracking also consumes sidechain subagent and API-error usage entries, unlike every other consumer of the same scan - while a Task subagent runs, Turn: silently reports the subagent call as the user turn.
Minor: docs/USAGE.md catalogs the other token widgets (Tokens Input / Output / Cached / Total bullet) but does not mention Tokens Last Turn; please add a bullet.
Per review policy tests and lint were not re-run in this review; the PR reports lint clean and 2369 tests passing.
…idget trackLastTurnTokens now runs only for main-chain usage entries, matching the recency rule already used for context length. While a Task subagent runs, its assistant entries are the newest usage rows in the transcript, so without the gate Turn: reported the subagent call as the user's turn. Also documents the widget in docs/USAGE.md. Co-Authored-By: Claude Code <noreply@anthropic.com>
axisrow
left a comment
There was a problem hiding this comment.
Approving. The follow-up commit (fe295c0) resolves both findings from the previous review:
- Last-turn tracking is now gated on entry.isMainChain, so sidechain (subagent) and API-error usage rows no longer displace the user turn - exactly the suggested fix, with a comment tying it to the recency rule used for context length.
- A new test covers interleaved sidechain usage and a synthetic API-error row (stop_reason null) and asserts the widget keeps reporting the main-chain call.
- docs/USAGE.md now documents Tokens Last Turn, including the dedup-per-message.id rationale and the main-chain-only scope.
The grouping logic is unchanged for main-chain rows, so the previously reviewed dedup behavior (per-content-block collapse, streaming partials, id-less fallback, opt-in flag) still stands. No further findings - ready to merge.
Per review policy tests and lint were not re-run in this review; the PR reports lint clean and the suite passing.
Co-Authored-By: Claude Code <noreply@anthropic.com>
Co-Authored-By: Claude Code <noreply@anthropic.com>
Summary
Implements sirmalloc#44 — a token count widget for the last message only. New
tokens-last-turnwidget ("Tokens Last Turn") renders the total token usage (input + output + cache) of the most recent assistant API call asTurn: <n>.How it works
getTranscriptAnalysis) — no second transcript walk. New opt-inincludeLastTurnTokensscan option; thelastTurnTokensfield onTokenMetricsis only populated when atokens-last-turnwidget is present in the configured lines.message.id(avoids Token widgets over-report ~1.84x: one JSONL entry per content block is counted per API call sirmalloc/ccstatusline#549): Claude Code writes one JSONL entry per content block of a single API call, all sharing onemessage.id— and, in the dominant shape, all carrying the final non-nullstop_reason. Per-call usage is grouped by id and only the highestoutput_tokensis kept instead of summing every entry, so the widget does not inherit the ~1.84x over-report. Streaming partials (stop_reason: null) resolve to the finalized value, and the in-flight call stays visible while streaming. Entries without an id each start a new group (legacy-format fallback).stop_reason-based filter and its behavior are unchanged.Tests
bun run lint— clean (tsc + ESLint, one run).bun test— 2369 pass / 0 fail, including new cases:src/widgets/__tests__/TokensLastTurn.test.ts— render, raw value, zero-hide, number format, preview, missing-data → null.src/utils/__tests__/jsonl-metrics.test.ts— 4 dedup cases: per-content-block entries of one id (the Token widgets over-report ~1.84x: one JSONL entry per content block is counted per API call sirmalloc/ccstatusline#549 shape), streaming partials of one id, id-less entries, opt-in flag off → field omitted.message.id, all with non-nullstop_reason):Turn: 16.5k(exact 16,545 = 2 in + 287 out + 3978 read + 12278 creation, counted once) next to cumulativeTotal: 49.3kon the same data.Known risks / follow-ups
tokens-input/output/cached/total) is deliberately not addressed here — this change only guarantees the new per-turn widget does not inherit it. Fixing the cumulative path is a separate change.Fixes sirmalloc#44
🤖 Generated with Claude Code