Skip to content

fix(ui): include cacheRead in contextPercent calculation#70592

Closed
akinshaywai wants to merge 1 commit into
openclaw:mainfrom
akinshaywai:fix/context-percent-includes-cache-read
Closed

fix(ui): include cacheRead in contextPercent calculation#70592
akinshaywai wants to merge 1 commit into
openclaw:mainfrom
akinshaywai:fix/context-percent-includes-cache-read

Conversation

@akinshaywai

Copy link
Copy Markdown
Contributor

Closes #70491

Summary

The % ctx status pill under assistant messages showed 0% in sessions that rely on prompt caching — even when the actual context window consumption was 44%+.

Root cause: contextPercent was computed from inputTokens alone. With prompt caching, inputTokens is just the small per-turn delta (e.g. 6 tokens), while cacheRead holds the bulk of the tokens consuming the context budget (e.g. 440k tokens). The existing code accumulated cacheRead correctly but then ignored it when computing the percentage.

Fix: Sum input + cacheRead as totalContextTokens before computing the percentage, matching the same accounting used by the /status slash command.

Files changed

ui/src/ui/chat/grouped-render.ts

  • extractGroupMeta: derive contextPercent from input + cacheRead instead of input alone

Test plan

  • Open a cache-heavy session (long session with a large model like claude-opus-4-7)
  • Confirm the % ctx status pill now shows a non-zero percentage matching /status output
  • Confirm fresh sessions (no cache) still show correct percentage
  • Confirm % ctx stays capped at 100% even if tokens exceed window

@greptile-apps

greptile-apps Bot commented Apr 23, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes the % ctx status pill showing 0% for cache-heavy sessions by including cacheRead tokens in the contextPercent calculation. The logic is correct for the common case (cache hits), but cacheWrite tokens (cache_creation_input_tokens) are also input-side tokens that fully count against the context window and should be included in the same sum. Without them, the first turn of any cache-heavy session — when the large prompt is written to the cache for the first time and cacheRead = 0 — still produces a near-zero percentage.

Confidence Score: 4/5

Safe to merge with a one-line follow-up: include cacheWrite in totalContextTokens.

The fix is a clear improvement over the previous behavior and is correct for cache-read turns. The single remaining P1 gap (cacheWrite omission) causes the same original bug to persist on cache-write turns, warranting a score of 4 rather than 5.

ui/src/ui/chat/grouped-render.ts — specifically the totalContextTokens formula on line 289.

Prompt To Fix All With AI
This is a comment left during a code review.
Path: ui/src/ui/chat/grouped-render.ts
Line: 289

Comment:
**`cacheWrite` also consumes the context window but is excluded**

`cache_creation_input_tokens` (`cacheWrite`) represents tokens being written to the cache for the first time — they are fully present in the input context for that turn. Omitting them means that on the *first* turn of a cache-heavy session (when the large prompt is initially sent and `cacheWrite` is large while `cacheRead` is 0), the pill will still read near-0% rather than the correct ≥44% value.

The consistent formula in the rest of the codebase (e.g. `session-cost-usage.ts`) accumulates `input + cacheRead + cacheWrite` as the total prompt-side token count.

```suggestion
  const totalContextTokens = input + cacheRead + cacheWrite;
```

How can I resolve this? If you propose a fix, please make it concise.

Reviews (1): Last reviewed commit: "fix(ui): include cacheRead in contextPer..." | Re-trigger Greptile

@@ -286,8 +286,11 @@ function extractGroupMeta(group: MessageGroup, contextWindow: number | null): Gr
return null;
}

const totalContextTokens = input + cacheRead;

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.

P1 cacheWrite also consumes the context window but is excluded

cache_creation_input_tokens (cacheWrite) represents tokens being written to the cache for the first time — they are fully present in the input context for that turn. Omitting them means that on the first turn of a cache-heavy session (when the large prompt is initially sent and cacheWrite is large while cacheRead is 0), the pill will still read near-0% rather than the correct ≥44% value.

The consistent formula in the rest of the codebase (e.g. session-cost-usage.ts) accumulates input + cacheRead + cacheWrite as the total prompt-side token count.

Suggested change
const totalContextTokens = input + cacheRead;
const totalContextTokens = input + cacheRead + cacheWrite;
Prompt To Fix With AI
This is a comment left during a code review.
Path: ui/src/ui/chat/grouped-render.ts
Line: 289

Comment:
**`cacheWrite` also consumes the context window but is excluded**

`cache_creation_input_tokens` (`cacheWrite`) represents tokens being written to the cache for the first time — they are fully present in the input context for that turn. Omitting them means that on the *first* turn of a cache-heavy session (when the large prompt is initially sent and `cacheWrite` is large while `cacheRead` is 0), the pill will still read near-0% rather than the correct ≥44% value.

The consistent formula in the rest of the codebase (e.g. `session-cost-usage.ts`) accumulates `input + cacheRead + cacheWrite` as the total prompt-side token count.

```suggestion
  const totalContextTokens = input + cacheRead + cacheWrite;
```

How can I resolve this? If you propose a fix, please make it concise.

Closes openclaw#70491. The status bar showed 0% ctx in cache-heavy sessions
because contextPercent was computed from inputTokens alone, ignoring
cacheRead. With prompt caching, inputTokens is just the small delta
per turn while cacheRead holds the bulk of the context budget consumed.
@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

@steipete steipete added duplicate This issue or pull request already exists close:duplicate Closed as duplicate dedupe:child Duplicate issue/PR child in dedupe cluster labels Apr 23, 2026
@steipete

Copy link
Copy Markdown
Contributor

Thanks for the fix. This is now covered by the landed #70532 / commit 6415e35.

#70532 fixes the same Control UI footer context-percent bug in ui/src/ui/chat/grouped-render.ts, using input + cacheRead + cacheWrite and adding regression coverage for both cached prompt tokens and output-token exclusion. This PR is the same bug/fix direction, with a narrower input + cacheRead numerator, so closing it as a duplicate.

@steipete steipete closed this Apr 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

app: web-ui App: web-ui close:duplicate Closed as duplicate dedupe:child Duplicate issue/PR child in dedupe cluster duplicate This issue or pull request already exists size: XS

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