Skip to content

feat(tui): compact turn token usage with expandable steps - #41922

Merged
kitlangton merged 2 commits into
v2from
compact-turn-tokens
Aug 12, 2026
Merged

kitlangton merged 2 commits into
v2from
compact-turn-tokens

Conversation

@kitlangton

@kitlangton kitlangton commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

What

The debug.turn_tokens diagnostic renders a per-turn "Tokens" table in the session transcript. With tool-heavy turns it prints one row per step and easily exceeds 30 rows per turn, even in non-verbose mode. This PR collapses that table into a single compact summary line by default and makes it click-to-expand.

Before / After

Before: enabling debug.turn_tokens rendered the full table for every completed turn — a header row plus one row per LLM step (Step / New / Cached / Total), unconditionally. A 30-step agentic turn produced 30+ transcript rows of diagnostics. The "Turn token usage (verbose)" checkbox in the DevTools Tools panel was always visible, even when the parent "Turn token usage" toggle was off, where toggling it had no visible effect.

After: each turn renders one line:

+ Tokens: 4 steps · 5,944 new · 4,500 cached · 5,520 total
  • step count, summed new tokens across steps, and the final step's cached/total figures
  • clicking the line expands the original per-step table in place (+ becomes -); clicking again collapses it
  • likely cache busts surface on the compact line as a warning-colored suffix (· ! 2 likely cache busts), so the signal that matters is never hidden behind the fold
  • the verbose checkbox only appears in the DevTools Tools panel once "Turn token usage" is enabled

Nothing renders when the setting is off, exactly as before.

How

  • packages/tui/src/routes/session/index.tsx — TurnTokenUsage gains expanded/hover signals. The header row follows the existing transcript idioms: + /- toggle prefix (as in the collapsed reasoning header), hover highlight, and a renderer.getSelection() guard so text selection doesn't toggle (as in InlineTool). The per-step table and cache-bust detail rows are unchanged, just wrapped in <Show when={expanded()}>. A summary memo derives the compact figures from the existing steps memo.
  • packages/tui/src/component/devtools-bar.tsx — the "Turn token usage (verbose)" Action is wrapped in <Show when={Boolean(turnTokens())}>, making it a visible refinement of the parent toggle rather than a peer.

Scope

  • No changes to how token usage is collected or to the turn-usage row reduction in rows.ts.
  • Expanded/collapsed state is per-render component state; it intentionally resets on transcript rebuild (replay, resize), like other transcript expand states.
  • Verbose mode still only affects the expanded table (tool-call detail rows); the compact line is identical in both modes.

Testing

  • bun typecheck in packages/tui — clean.
  • End-to-end via opencode-drive against this branch (--dev worktree): scripted a 4-step turn (3 tool calls + final text) with simulated OpenAI usage chunks, enabled the setting from the DevTools Tools panel, and verified compact line → click to expand (full Step/New/Cached/Total table) → click to collapse, plus the verbose checkbox appearing only after enabling the parent toggle.

Demo

Recorded from the end-to-end run above (simulated model; real TUI):

turn-tokens-demo-v2.mp4

@kitlangton
kitlangton marked this pull request as ready for review August 12, 2026 02:22
@kitlangton
kitlangton enabled auto-merge (squash) August 12, 2026 02:22
@kitlangton
kitlangton merged commit d3eecf7 into v2 Aug 12, 2026
10 checks passed
@kitlangton
kitlangton deleted the compact-turn-tokens branch August 12, 2026 02:39
jinhuang712 pushed a commit to jinhuang712/opencode that referenced this pull request Sep 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant