Skip to content

fix: include cached tokens in context window % calculation#48827

Closed
yydonk wants to merge 1 commit intoopenclaw:mainfrom
yydonk:fix/context-percent-include-cached-tokens
Closed

fix: include cached tokens in context window % calculation#48827
yydonk wants to merge 1 commit intoopenclaw:mainfrom
yydonk:fix/context-percent-include-cached-tokens

Conversation

@yydonk
Copy link
Copy Markdown

@yydonk yydonk commented Mar 17, 2026

When a session has a heavily-cached prompt, usage.input only contains the uncached new tokens (e.g. 2k out of a 200k window), causing contextPercent to show 0% even when the context is 76% full.

The correct total input token count is:
input (uncached) + cacheRead + cacheWrite

Use that sum when computing the context % so the indicator reflects actual context window utilisation.

Fixes #48640

Summary

Describe the problem and fix in 2–5 bullets:

  • Problem:
  • Why it matters:
  • What changed:
  • What did NOT change (scope boundary):

Change Type (select all)

  • Bug fix
  • Feature
  • Refactor
  • Docs
  • Security hardening
  • Chore/infra

Scope (select all touched areas)

  • Gateway / orchestration
  • Skills / tool execution
  • Auth / tokens
  • Memory / storage
  • Integrations
  • API / contracts
  • UI / DX
  • CI/CD / infra

Linked Issue/PR

  • Closes #
  • Related #

User-visible / Behavior Changes

List user-visible changes (including defaults/config).
If none, write None.

Security Impact (required)

  • New permissions/capabilities? (Yes/No)
  • Secrets/tokens handling changed? (Yes/No)
  • New/changed network calls? (Yes/No)
  • Command/tool execution surface changed? (Yes/No)
  • Data access scope changed? (Yes/No)
  • If any Yes, explain risk + mitigation:

Repro + Verification

Environment

  • OS:
  • Runtime/container:
  • Model/provider:
  • Integration/channel (if any):
  • Relevant config (redacted):

Steps

Expected

Actual

Evidence

Attach at least one:

  • Failing test/log before + passing after
  • Trace/log snippets
  • Screenshot/recording
  • Perf numbers (if relevant)

Human Verification (required)

What you personally verified (not just CI), and how:

  • Verified scenarios:
  • Edge cases checked:
  • What you did not verify:

Review Conversations

  • I replied to or resolved every bot review conversation I addressed in this PR.
  • I left unresolved only the conversations that still need reviewer or maintainer judgment.

If a bot review conversation is addressed by this PR, resolve that conversation yourself. Do not leave bot review conversation cleanup for maintainers.

Compatibility / Migration

  • Backward compatible? (Yes/No)
  • Config/env changes? (Yes/No)
  • Migration needed? (Yes/No)
  • If yes, exact upgrade steps:

Failure Recovery (if this breaks)

  • How to disable/revert this change quickly:
  • Files/config to restore:
  • Known bad symptoms reviewers should watch for:

Risks and Mitigations

List only real risks for this PR. Add/remove entries as needed. If none, write None.

  • Risk:
    • Mitigation:

When a session has a heavily-cached prompt, usage.input only contains
the uncached new tokens (e.g. 2k out of a 200k window), causing
contextPercent to show 0% even when the context is 76% full.

The correct total input token count is:
  input (uncached) + cacheRead + cacheWrite

Use that sum when computing the context % so the indicator reflects
actual context window utilisation.

Fixes openclaw#48640
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Mar 17, 2026

Greptile Summary

This PR fixes a display bug where the context-window percentage indicator showed ~0% during heavily-cached sessions. The Anthropic API only puts uncached tokens in input; cached tokens come back in cacheRead/cacheWrite. By computing totalInput = input + cacheRead + cacheWrite and using that for the percentage, the indicator now reflects real context utilisation.

  • Root cause fixed: contextPercent was computed from input alone, which can be a small fraction of the total when prompt caching is active.
  • Minimal scope: only the contextPercent calculation changes; the raw input, cacheRead, and cacheWrite values returned in GroupMeta are unchanged, preserving the existing token-breakdown display.
  • Edge cases handled: token counters are already initialised to 0 and guarded with ?? 0 on accumulation, so totalInput is always a valid number; the totalInput > 0 guard prevents a division-by-zero when no usage is available.
  • No concerns found: the fix is correct, well-commented, and non-breaking.

Confidence Score: 5/5

  • This PR is safe to merge — it is a one-expression bug fix with no side-effects on data flow, rendering, or API contracts.
  • The change is small, well-reasoned, and correct. Token counters are already zero-initialised and null-guarded, so no new NaN/undefined risk is introduced. The fix only affects contextPercent and does not mutate any other part of the returned GroupMeta, keeping the rest of the UI unchanged.
  • No files require special attention.

Last reviewed commit: 8541ed1

@steipete
Copy link
Copy Markdown
Contributor

Closing this as implemented after Codex review.

Current main already includes the requested behavior. ui/src/ui/chat/grouped-render.ts:427 computes prompt/context usage as input + cacheRead + cacheWrite before deriving contextPercent, matching this PR’s change, and ui/src/ui/chat/grouped-render.test.ts:263 covers the cached-token case with an assertion for 44% ctx.

What I checked:

So I’m closing this as already implemented rather than keeping a duplicate issue open.

Review notes: reviewed against 01bc49c88c0c; fix evidence: commit 8fade9df27a9.

@steipete steipete closed this Apr 25, 2026
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.

Dashboard v2 Chat: context % always shows 0%, cache hit always 100%

2 participants