fix(ui): include cacheRead in contextPercent calculation#70592
fix(ui): include cacheRead in contextPercent calculation#70592akinshaywai wants to merge 1 commit into
Conversation
Greptile SummaryThis PR fixes the Confidence Score: 4/5Safe 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 AIThis 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; | |||
There was a problem hiding this 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.
| 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.
09c26f9 to
25c63f1
Compare
|
Related work from PRtags group Title: Control UI context percent ignores cached prompt tokens
|
|
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. |
Closes #70491
Summary
The
% ctxstatus pill under assistant messages showed0%in sessions that rely on prompt caching — even when the actual context window consumption was 44%+.Root cause:
contextPercentwas computed frominputTokensalone. With prompt caching,inputTokensis just the small per-turn delta (e.g. 6 tokens), whilecacheReadholds the bulk of the tokens consuming the context budget (e.g. 440k tokens). The existing code accumulatedcacheReadcorrectly but then ignored it when computing the percentage.Fix: Sum
input + cacheReadastotalContextTokensbefore computing the percentage, matching the same accounting used by the/statusslash command.Files changed
ui/src/ui/chat/grouped-render.tsextractGroupMeta: derivecontextPercentfrominput + cacheReadinstead ofinputaloneTest plan
% ctxstatus pill now shows a non-zero percentage matching/statusoutput% ctxstays capped at 100% even if tokens exceed window