Usage and balance reporting: token counts disagree across surfaces, tool rounds record zero usage, and budget exhaustion is inferred not observed
Description
Token, cost and budget figures are reported inconsistently across the app's surfaces, and several are wrong or unlabelled. The same phrase — "input tokens" — means two different quantities in two places, differing by multiples on any turn that calls a tool. Separately, rounds that issue a tool call record zero usage, so per-round spend is unrecoverable; and "out of credits" is sometimes inferred rather than observed, so a user can be told to top up when nothing is wrong with their balance.
Balance surfaces already exist (WalletBalancesPanel.tsx, WalletPanel.tsx, panels/billing/) alongside the cost dashboard (UsagePanel.tsx with costs / log / tokens / background tabs). The ask is to make the numbers correct and consistent first, then bring balance and spend together into one trustworthy view.
Build: v0.64.4 (main @ f1a3ca782 and later)
Reproducible: Yes — every multi-round turn
Steps to Reproduce
- Open a thread and send a message that forces at least one tool call, e.g.
fetch my latest email - Let the turn complete — it will run several model calls
- Compare three numbers for that single turn:
- the
input_tokensin theturn prompt summarylog line fromturn_run_finalize - the context-usage ring in the composer
- the provider's own
prompt_tokensfor each call
- the
- Open the thread's transcript under
workspace/session_raw/and inspectusageon each assistant row
Expected Behaviour
- One definition of "context used" per turn, with the same value on every surface, or clearly distinct labels where the quantities genuinely differ (per-call prompt size vs. turn-total spend)
- Every model call records its own input, output, cached and cost figures, so per-round spend is attributable
- The context ring reflects the turn's peak prompt size, since that is what determines whether the window will overflow
- "Out of credits" is shown only when the provider actually reported a budget failure
- Usage and balance panels degrade gracefully when the backend is briefly unavailable, showing the last known figures with a staleness marker
Actual Behaviour
1 — "input tokens" means two different things.
turn_run_finalize logs the sum across every model call in the turn. The composer's context ring uses last_input_tokens, which threads/ops/usage.rs:113 sets from the newest durable record: spend.last_input_tokens = usage.usage.input. Verified against intercepted requests — a two-call turn logged input_tokens=34097 while the wire showed 15,869 and 18,228, summing to exactly 34,097. A seven-call turn logged 94,062 while no single prompt exceeded ~20k. Neither surface states which quantity it is showing.
2 — tool-call rounds record no usage at all.
In a current transcript, 5 of 25 assistant rows have usage.input == 0 — every round that issued a tool call. All spend is attributed to the final round. Per-round cost cannot be recovered, and the ring shows the last round rather than the largest.
3 — budget exhaustion is inferred.
BudgetCorrelation::UpgradeEmptyToBudget reclassifies an ordinary empty provider response as out-of-credits when a budget signal was recorded on the same thread recently (#3386). The user then sees "You're out of credits, so I can't run the managed (cloud) model right now… top up your credits or pick a plan", which is wrong if the empty response had another cause.
4 — usage lookups have no fallback.
openhuman.team_get_usage failed twice today after ~16s each (transient downstream failure … GET /teams/me/usage), against 197 successes. There is no cached last-known value, so the panel has nothing to show while the backend is unreachable.
Impact
- Users cannot trust any token or cost number in the app, because two surfaces disagree and neither is labelled
- The mislabelled budget error can push a user to pay for credits they do not need
- Per-round attribution is impossible, so a turn that burns tokens on a tool loop looks identical to a cheap one — exactly the case someone checking usage is trying to find
- The context ring under-reports the turn's peak, so a turn can overflow the window while the indicator looked comfortable
- This is not hypothetical: the summed-vs-last discrepancy caused a root-cause analysis to be published with the wrong conclusion this session, and it had to be retracted
Acceptance Criteria
- A single documented definition of per-turn "context used" and per-turn "total spend"; every surface uses one or the other and labels which
-
turn_run_finalize's summed figure is renamed so it cannot be read as a prompt size - Each model call records its own input / output / cached / cost; no assistant row with a tool call persists
usage.input == 0 - The composer context ring reports the turn's peak prompt size, not the last round's
- Per-round spend is visible in the usage log for a multi-round turn
- "Out of credits" copy appears only on an observed provider budget failure; the inferred path either shows a distinct message or is removed
-
team_get_usagefailure shows the last known figures with a staleness marker instead of an empty panel - Balance and spend are reachable from one place, with remaining credit shown next to consumption
- Verified on a tool-heavy turn by reconciling the log, the ring and the provider's own
prompt_tokensto the same definitions
Source: tinyhumansai/openhuman