Skip to content

feat(widgets): add Tokens Last Turn widget for per-message token usage - #1

Merged
axisrow merged 4 commits into
mainfrom
ao/ccstatusline-3/tokens-last-turn
Sep 25, 2026
Merged

axisrow merged 4 commits into
mainfrom
ao/ccstatusline-3/tokens-last-turn

Conversation

@axisrow

@axisrow axisrow commented Sep 25, 2026

Copy link
Copy Markdown
Owner

Summary

Implements sirmalloc#44 — a token count widget for the last message only. New tokens-last-turn widget ("Tokens Last Turn") renders the total token usage (input + output + cache) of the most recent assistant API call as Turn: <n>.

How it works

  • Data is collected by the shared single-pass transcript scan (getTranscriptAnalysis) — no second transcript walk. New opt-in includeLastTurnTokens scan option; the lastTurnTokens field on TokenMetrics is only populated when a tokens-last-turn widget is present in the configured lines.
  • Deduplication by 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 one message.id — and, in the dominant shape, all carrying the final non-null stop_reason. Per-call usage is grouped by id and only the highest output_tokens is 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).
  • Cumulative token widgets are untouched; the existing stop_reason-based filter and its behavior are unchanged.

Tests

Known risks / follow-ups

Fixes sirmalloc#44

🤖 Generated with Claude Code

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 axisrow left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/utils/jsonl-metrics.ts Resolved
…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 axisrow left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving. The follow-up commit (fe295c0) resolves both findings from the previous review:

  1. 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.
  2. 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.
  3. 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.

axisrow and others added 2 commits September 25, 2026 12:26
Co-Authored-By: Claude Code <noreply@anthropic.com>
Co-Authored-By: Claude Code <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a token count widget for last message only

1 participant