Skip to content

fix(ui): include cached tokens in context usage#70532

Merged
steipete merged 2 commits into
openclaw:mainfrom
chen-zhang-cs-code:codex/issue-70491-control-ui-context-cache
Apr 23, 2026
Merged

fix(ui): include cached tokens in context usage#70532
steipete merged 2 commits into
openclaw:mainfrom
chen-zhang-cs-code:codex/issue-70491-control-ui-context-cache

Conversation

@chen-zhang-cs-code

@chen-zhang-cs-code chen-zhang-cs-code commented Apr 23, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #70491.

The Control UI assistant message footer already collects input, output, cacheRead, and cacheWrite, but the % ctx calculation only used input. For cached providers, input is often just the per-turn delta after a cache hit, so long Claude/OpenAI sessions could show 0% ctx while /status reported substantial context use.

This PR keeps the change scoped to the footer metadata calculation:

  • Compute contextPercent from prompt/context tokens: input + cacheRead + cacheWrite.
  • Keep completion/output tokens visible in the footer, but exclude them from % ctx so long completions do not inflate prompt occupancy.
  • Add jsdom regression coverage for both cached prompt tokens and completion-heavy responses.
  • Add a changelog entry for the user-visible fix.

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% ctx text 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.ts
  • pnpm exec oxfmt --check CHANGELOG.md ui/src/ui/chat/grouped-render.ts ui/src/ui/chat/grouped-render.test.ts
  • pnpm exec oxlint ui/src/ui/chat/grouped-render.ts ui/src/ui/chat/grouped-render.test.ts
  • pnpm check:changed
  • git diff --check

@greptile-apps

greptile-apps Bot commented Apr 23, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

Fixes a display bug where the assistant message footer showed 0% ctx for cached sessions by including cacheRead, cacheWrite, and output tokens in the contextPercent calculation, alongside a jsdom regression test that validates the 44% ctx output for a representative cached-token payload.

Confidence Score: 5/5

Safe to merge — the fix is minimal, well-tested, and the only open note is a P2 clarification about whether contextWindow always represents the combined input+output limit.

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 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.

Reviews (1): Last reviewed commit: "fix: include cached tokens in control ui..." | Re-trigger Greptile

Comment thread ui/src/ui/chat/grouped-render.ts Outdated
Comment on lines +405 to +409
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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@chen-zhang-cs-code

chen-zhang-cs-code commented Apr 23, 2026

Copy link
Copy Markdown
Contributor Author

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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread ui/src/ui/chat/grouped-render.ts Outdated
@@ -402,8 +402,11 @@ function extractGroupMeta(group: MessageGroup, contextWindow: number | null): Gr
return null;
}

const contextTokens = input + output + cacheRead + cacheWrite;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

@chen-zhang-cs-code chen-zhang-cs-code Apr 23, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@chen-zhang-cs-code

Copy link
Copy Markdown
Contributor Author

@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 steipete left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@steipete

Copy link
Copy Markdown
Contributor

Clarifying my approval text: the intended accounting phrase was input + cacheRead + cacheWrite. The approval stands; no blocking findings.

@steipete
steipete force-pushed the codex/issue-70491-control-ui-context-cache branch from 51cd8d2 to 0f58fcf Compare April 23, 2026 17:08
@steipete
steipete merged commit 6415e35 into openclaw:main Apr 23, 2026
67 checks passed
@steipete

Copy link
Copy Markdown
Contributor

Landed via squash merge onto main.

  • Gate: pnpm exec vitest run ui/src/ui/chat/grouped-render.test.ts --config test/vitest/vitest.unit.config.ts; pnpm check:changed; fresh GitHub CI green after rebase
  • Live check: targeted live cache lane exercised real OpenAI/Anthropic cache reads/writes, but Anthropic intermittently returned empty text in existing live-cache probes; treated as unrelated to this UI-only diff
  • Source SHA: 0f58fcf
  • Land commit: 6415e35

Thanks @chen-zhang-cs-code!

@prtags

prtags Bot commented Apr 23, 2026

Copy link
Copy Markdown

Related work from PRtags group liked-piglet-oe6w

Title: Control UI context percent ignores cached prompt tokens

Number Title
#70530 fix(ui): include cache tokens in contextPercent calculation
#70532* fix(ui): include cached tokens in context usage
#70592 fix(ui): include cacheRead in contextPercent calculation

* This PR

ogt-redknie pushed a commit to ogt-redknie/OPENX that referenced this pull request May 2, 2026
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.
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 9, 2026
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.
globalcaos pushed a commit to globalcaos/tinkerclaw that referenced this pull request May 13, 2026
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.
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 24, 2026
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.
jameslcowan pushed a commit to jameslcowan/openclaw that referenced this pull request Jun 2, 2026
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.
sablehead pushed a commit to sablehead/openclaw that referenced this pull request Jun 10, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Control UI status bar shows '0% ctx' when actual session context is 44% — contextPercent ignores cacheRead

2 participants