Skip to content

feat(tui): v2: show per-step and per-turn cost in the token usage view - #53156

Closed
bew wants to merge 1 commit into
anomalyco:devfrom
bew:turn-token-cost
Closed

bew wants to merge 1 commit into
anomalyco:devfrom
bew:turn-token-cost

Conversation

@bew

@bew bew commented Oct 4, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #53121

Type of change

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

What does this PR do?

Hello!

I was staring at the debug.turn_tokens usage view and thought it would be nice to see the price next to the tokens.
The view already splits a turn into steps by token counts, but there was no cost tracking in it. 👀

So this PR adds a Cost column to the expanded table, plus a turn total on the collapsed summary line:

  • Each step row shows its incremental spend as +$0.0004, and a step with no price data shows —.
  • The turn total shows up as · $0.0012 cost, only when at least one step actually reports a cost.
  • The Cost header is centered over the column; the values sit left-aligned, since nothing follows them.
  • I sum it from the visible step rows, so the summary and the table always agree.

The formatting localized in a small Locale.formatCost helper:

  • From a cent up it keeps two decimals.
  • Below a cent it keeps one significant digit and truncates instead of rounding, so a step never reads as costing more than it did.
    (these costs are insignificant anyway..)

The data was already sitting on each assistant message (cost?: Money.USD), so nothing outside the TUI changed. 🙂

How did you verify your code works?

Tested manually with bun dev, switched to an old test session.

From packages/tui:

  • bun test test/util/locale.test.ts — 4 pass, 19 expect() calls
  • bun typecheck — clean (tsgo -b, no errors)
  • oxlint on the changed files — 0 warnings, 0 errors

The formatCost test covers undefined, zero, normal amounts, and sub-cent truncation ($0.005, $0.0004, $0.00003); the padCenter test covers centering, including a width narrower than the text.
The TurnTokenUsage column layout and cost aggregation themselves have no unit test.

Screenshots / recordings

BEFORE (no Cost column):
image

AFTER examples:
image
image

Checklist

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

Surface money spent per step and per turn in the session token
usage view, alongside the existing token counts.

- Render each step's incremental cost in the expandable table and the
  turn total in the collapsed summary line.
- Add `Locale.formatCost` for USD amounts: two decimals at a cent or
  more, one truncated significant digit below, with tests.
@github-actions

github-actions Bot commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

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

@bew bew changed the title feat(tui): show per-step and per-turn cost in the token usage view feat(tui): v2: show per-step and per-turn cost in the token usage view Oct 4, 2026
@thdxr
thdxr changed the base branch from v2 to dev October 10, 2026 19:31
@bew

bew commented Nov 1, 2026

Copy link
Copy Markdown
Author

Closing for now, it's not that important and I want to open another PR (I'm at limit of open PR now)

@bew bew closed this Nov 1, 2026
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]: v2: show per-step and per-turn cost in the token usage view

1 participant