Skip to content

feat(tui): add persistent token usage display in session footer#19105

Closed
andrewdunndev wants to merge 1 commit into
anomalyco:devfrom
andrewdunndev:feat/footer-token-display
Closed

feat(tui): add persistent token usage display in session footer#19105
andrewdunndev wants to merge 1 commit into
anomalyco:devfrom
andrewdunndev:feat/footer-token-display

Conversation

@andrewdunndev
Copy link
Copy Markdown

Issue for this PR

Closes #17449
Relates to #6096, #10354, #13003

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Adds cumulative token usage and live tokens-per-second to the TUI session footer, displayed alongside existing LSP/MCP status items.

Footer format:

~/project                          142 tok/s · 4.5M in · 19K out · • 2 LSP · ⊙ 1 MCP

Three data points:

  • tok/s -- live output tokens-per-second during streaming (bright text), persists for 3 seconds after streaming ends (muted text), then hides
  • in -- cumulative input tokens for the session with compact K/M suffix
  • out -- cumulative output tokens for the session with compact K/M suffix

Token stats are computed from AssistantMessage.tokens in the sync store via createMemo. TPS is calculated in a createEffect (no side effects in memos) that tracks output token deltas between reactive updates during the busy session status.

Files changed:

  • packages/opencode/src/cli/cmd/tui/routes/session/footer.tsx -- token stats memo, TPS tracking effect, footer display
  • packages/opencode/src/cli/cmd/tui/util/tokens.ts -- formatTokenCount() (compact K/M suffixes with boundary rollover) and formatTps() (clean threshold at 10)
  • packages/opencode/src/cli/cmd/tui/util/tokens.test.ts -- 8 tests, 30 assertions covering edge cases (K→M rollover at 999,500, TPS threshold at 9.95)

How did you verify your code works?

  • bun test src/cli/cmd/tui/util/tokens.test.ts -- 8 pass, 0 fail, 30 assertions
  • bun run --filter opencode typecheck -- exits 0 (no new type errors)
  • Cross-model code review (GPT-5 Codex) identified a critical issue in initial implementation: TPS was computed in a createMemo with side effects causing a reactive loop. Refactored to createEffect with explicit dependency tracking before submission.
  • Reviewed SessionStatus discriminated union type (idle | busy | retry) against SDK v2 types to ensure correct status detection

Screenshots / recordings

TUI change. The footer gains a token stats section to the left of the existing LSP/MCP items. During streaming, shows live tok/s rate. Always shows cumulative in/out totals once the first assistant message completes. Hidden when no messages have been exchanged yet.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Add cumulative input/output token counts and live tokens-per-second
to the TUI session footer. Displays alongside existing LSP/MCP
status items.

Format: '142 tok/s · 4.5M in · 19K out · • 2 LSP · ⊙ 1 MCP'

- tok/s shown during streaming (bright) and 3s after (muted)
- Cumulative in/out tokens with compact K/M suffixes
- Token stats computed from AssistantMessage.tokens in the sync store
- TPS calculated via createEffect (no side effects in memos)
- Formatting utilities in util/tokens.ts with 8 tests covering
  edge cases (K/M rollover, TPS threshold at 10)

Closes anomalyco#17449
Relates to anomalyco#6096, anomalyco#10354, anomalyco#13003
@github-actions
Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

Potential Duplicate/Related PRs Found:

  1. feat(tui): show tokens per second in assistant message footer

  2. feat(tui): add tokens per second to response footer

  3. feat(tui): add native status line template system

Note: The current PR (#19105) builds on or supersedes similar earlier attempts to add token metrics to the footer. Check if PRs #14493 and #12721 are closed/merged or if they address overlapping functionality.

@andrewdunndev
Copy link
Copy Markdown
Author

Closing -- I should have searched open PRs more thoroughly before submitting. #14493 (by @thdxr) and #12721 both address the same feature. Apologies for the noise.

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.

[FEATURE]: Real-time token display in TUI footer/header

1 participant