fix(ui): include cached tokens in context usage#70532
Conversation
Greptile SummaryFixes a display bug where the assistant message footer showed Confidence Score: 5/5Safe to merge — the fix is minimal, well-tested, and the only open note is a P2 clarification about whether All findings are P2 or lower. The core fix (adding cache tokens) is correct and validated by a new regression test. No logic errors, security issues, or breaking changes. No files require special attention. Prompt To Fix All With AIThis is a comment left during a code review.
Path: ui/src/ui/chat/grouped-render.ts
Line: 405-409
Comment:
**Output tokens included in context-window percentage**
`contextTokens` now sums `input + output + cacheRead + cacheWrite`. Anthropic's context window is the combined input+output limit, so this is technically correct. However, for providers where the advertised `contextWindow` value is input-only capacity (some OpenAI endpoints expose `max_input_tokens` separately), including `output` will slightly overstate the percentage. The original bug (missing cache tokens) is clearly fixed; this is just worth confirming that `contextWindow` here always reflects the combined limit, not just the input side.
How can I resolve this? If you propose a fix, please make it concise.Reviews (1): Last reviewed commit: "fix: include cached tokens in control ui..." | Re-trigger Greptile |
| const contextTokens = input + output + cacheRead + cacheWrite; | ||
| const contextPercent = | ||
| contextWindow && input > 0 ? Math.min(Math.round((input / contextWindow) * 100), 100) : null; | ||
| contextWindow && contextTokens > 0 | ||
| ? Math.min(Math.round((contextTokens / contextWindow) * 100), 100) | ||
| : null; |
There was a problem hiding this comment.
Output tokens included in context-window percentage
contextTokens now sums input + output + cacheRead + cacheWrite. Anthropic's context window is the combined input+output limit, so this is technically correct. However, for providers where the advertised contextWindow value is input-only capacity (some OpenAI endpoints expose max_input_tokens separately), including output will slightly overstate the percentage. The original bug (missing cache tokens) is clearly fixed; this is just worth confirming that contextWindow here always reflects the combined limit, not just the input side.
Prompt To Fix With AI
This is a comment left during a code review.
Path: ui/src/ui/chat/grouped-render.ts
Line: 405-409
Comment:
**Output tokens included in context-window percentage**
`contextTokens` now sums `input + output + cacheRead + cacheWrite`. Anthropic's context window is the combined input+output limit, so this is technically correct. However, for providers where the advertised `contextWindow` value is input-only capacity (some OpenAI endpoints expose `max_input_tokens` separately), including `output` will slightly overstate the percentage. The original bug (missing cache tokens) is clearly fixed; this is just worth confirming that `contextWindow` here always reflects the combined limit, not just the input side.
How can I resolve this? If you propose a fix, please make it concise.There was a problem hiding this comment.
Updated in d16926c802 after checking the repo's accounting semantics. SessionEntry.totalTokens is intentionally a prompt/context snapshot in src/agents/usage.ts, so the footer now uses input + cacheRead + cacheWrite and leaves output out of % ctx. The cached-session regression still renders 44% ctx, and a new completion-heavy regression confirms output does not inflate the percentage.
|
Review follow-up correction: the latest patch excludes output tokens from the footer context percentage. After checking src/agents/usage.ts, the repo treats session context occupancy as prompt-side usage: input + cacheRead + cacheWrite. The footer now follows that same accounting, and d16926c802 adds a regression where a long completion does not inflate % ctx. This still fixes #70491 because cached prompt tokens are included, while completion-heavy responses no longer trigger premature warning/danger styling. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2ffc6037fe
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| @@ -402,8 +402,11 @@ function extractGroupMeta(group: MessageGroup, contextWindow: number | null): Gr | |||
| return null; | |||
| } | |||
|
|
|||
| const contextTokens = input + output + cacheRead + cacheWrite; | |||
There was a problem hiding this comment.
Use prompt tokens for context-percentage math
Including output in % ctx inflates the context-window usage shown in chat footers for replies with long completions, even when prompt usage is low. In this codebase, context occupancy is tracked as prompt tokens (input + cacheRead + cacheWrite) and explicitly excludes completion tokens (src/agents/usage.ts), so this change makes footer % ctx diverge from /status and can trigger warning/danger styling prematurely for completion-heavy responses.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Updated in d16926c802 to align with prompt/context accounting: the footer now computes % ctx from input + cacheRead + cacheWrite and excludes output. I also added a regression test where a 9k-token completion over a 10k context still renders 10% ctx, so long completions do not trigger warning/danger styling prematurely.
|
@steipete when you have a chance, could you take a look at this one? It fixes #70491 with a small Control UI footer change: % ctx now includes cached prompt tokens (input + cacheRead + cacheWrite) but excludes output tokens, matching the repo's prompt-side context accounting. CI is green and the automated review nit has been addressed with a regression test. |
steipete
left a comment
There was a problem hiding this comment.
No blocking findings. The footer now matches the repo's prompt-side context accounting () and keeps output tokens out of the context percentage. The added jsdom tests cover both the cached-token regression from #70491 and the long-output non-regression. Worth landing.
|
Clarifying my approval text: the intended accounting phrase was |
51cd8d2 to
0f58fcf
Compare
|
Landed via squash merge onto main.
Thanks @chen-zhang-cs-code! |
|
Related work from PRtags group Title: Control UI context percent ignores cached prompt tokens
|
Fixes openclaw#70491. Includes cached prompt tokens in the Control UI context percent and keeps output tokens out of the percentage. Thanks @chen-zhang-cs-code.
Fixes openclaw#70491. Includes cached prompt tokens in the Control UI context percent and keeps output tokens out of the percentage. Thanks @chen-zhang-cs-code.
Fixes openclaw#70491. Includes cached prompt tokens in the Control UI context percent and keeps output tokens out of the percentage. Thanks @chen-zhang-cs-code.
Fixes openclaw#70491. Includes cached prompt tokens in the Control UI context percent and keeps output tokens out of the percentage. Thanks @chen-zhang-cs-code.
Fixes openclaw#70491. Includes cached prompt tokens in the Control UI context percent and keeps output tokens out of the percentage. Thanks @chen-zhang-cs-code.
Fixes openclaw#70491. Includes cached prompt tokens in the Control UI context percent and keeps output tokens out of the percentage. Thanks @chen-zhang-cs-code.
Summary
Fixes #70491.
The Control UI assistant message footer already collects
input,output,cacheRead, andcacheWrite, but the% ctxcalculation only usedinput. For cached providers,inputis often just the per-turn delta after a cache hit, so long Claude/OpenAI sessions could show0% ctxwhile/statusreported substantial context use.This PR keeps the change scoped to the footer metadata calculation:
contextPercentfrom prompt/context tokens:input + cacheRead + cacheWrite.% ctxso long completions do not inflate prompt occupancy.AI-assisted: yes, Codex.
Screenshots
No screenshot attached. This is a computed footer value change; the regression test renders the footer and asserts the visible
44% ctxtext for the cached-token case.Test plan
pnpm exec vitest run ui/src/ui/chat/grouped-render.test.ts --config test/vitest/vitest.unit.config.tspnpm exec oxfmt --check CHANGELOG.md ui/src/ui/chat/grouped-render.ts ui/src/ui/chat/grouped-render.test.tspnpm exec oxlint ui/src/ui/chat/grouped-render.ts ui/src/ui/chat/grouped-render.test.tspnpm check:changedgit diff --check