Skip to content

fix(ui): include cache tokens in ctx% badge and cache-hit-rate#68557

Closed
Ante042 wants to merge 2 commits intoopenclaw:mainfrom
Ante042:fix/ui-ctx-badge-and-cache-hit-rate
Closed

fix(ui): include cache tokens in ctx% badge and cache-hit-rate#68557
Ante042 wants to merge 2 commits intoopenclaw:mainfrom
Ante042:fix/ui-ctx-badge-and-cache-hit-rate

Conversation

@Ante042
Copy link
Copy Markdown

@Ante042 Ante042 commented Apr 18, 2026

Summary

  • Problem: The Control UI's ctx% badge and cache-hit-rate metric both ignore prompt cache tokens, so any session using Anthropic prompt caching (where most of the prompt lives in `cacheRead`/`cacheWrite`) renders a ~0% context badge and an inflated cache-hit-rate.
  • Why it matters: Operators rely on the badge to know when a session is approaching its context window; today it stays near 0% even when the cached prompt is at 300k/1M. The cache-hit-rate metric is a KPI in the usage overview.
  • What changed: Include cache-read + cache-write in the prompt-size sum for the ctx% badge, and in the cache-hit-rate denominator. Update the matching tooltip string.
  • What did NOT change: No API/contract changes, no data shape changes. Pure presentational fix.

Change Type (select all)

  • Bug fix

Scope (select all touched areas)

  • UI / DX

Linked Issue/PR

  • Closes #
  • Related #
  • This PR fixes a bug or regression

Root Cause (if applicable)

Both call sites were written when Anthropic prompt caching wasn't a first-class concept and only the `input` counter mattered. Once caching landed, the bulk of the prompt tokens shifted into `cacheRead`/`cacheWrite` but the two math expressions weren't updated:

  • `grouped-render.ts:288` computed `input / contextWindow` → near-zero ratio when caching is warm.
  • `usage-render-overview.ts:499` computed `cacheRead / (input + cacheRead)` → excludes cache writes from the base, inflating the ratio.

Evidence

  • Observed wrong behavior: on Opus 4.7 1M-context sessions, ctx% badge stays at 0% while actual prompt caching is at 200-400k tokens per turn. After the fix the badge correctly reflects utilization.
  • Ran locally against a production OpenClaw deployment; badge and cache-hit-rate now match what you'd compute by hand from the raw Anthropic usage payload.

Human Verification (required)

  • Verified scenarios:
    • Warm cached prompt on Opus 4.7 1M → badge now shows a meaningful percent instead of sticking at 0%.
    • Cache-hit-rate recomputed on a full-day usage window — lower (more honest) value after the fix.
  • Edge cases checked:
    • Cold session (no cache) — behavior unchanged.
    • `contextWindow` null — still returns null (no division).
    • `cacheBase = 0` — still returns the empty-value fallback.
  • What I did not verify: i18n strings in non-English locales. Only `en.ts` is touched; other locales still reference the old formula in their own copies of the tooltip and may want the same treatment in a follow-up.

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.

Compatibility / Migration

  • Backward compatible? Yes
  • Config/env changes? No
  • Migration needed? No

Risks and Mitigations

  • Risk: Existing dashboards/alerts calibrated against the old cache-hit-rate will show lower values after this lands.
    • Mitigation: Lower values are closer to truth; worth the one-time recalibration. Tooltip text in this PR documents the new formula.

The context percentage badge in grouped render and the cache-hit-rate
metric in the usage overview were both ignoring cache tokens, so any
session using Anthropic prompt caching (where most of the prompt lives
in cacheRead/cacheWrite rather than input) rendered a misleading ~0%
context usage and an inflated cache-hit-rate.

Fixes:

* ui/src/ui/chat/grouped-render.ts: sum input + cacheRead + cacheWrite
  when computing contextPercent, so the badge reflects the real share
  of the context window occupied by the prompt.
* ui/src/ui/views/usage-render-overview.ts: include cacheWrite in the
  cache-hit-rate denominator. The current formula double-counts cache
  writes toward hit rate by omitting them from the base.
* ui/src/i18n/locales/en.ts: update the cacheHint tooltip to match
  the corrected formula.
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Apr 18, 2026

Greptile Summary

This PR fixes two related math bugs: the ctx% context-window badge was dividing only input tokens by contextWindow (leaving it near 0% when prompt caching is warm), and the cache-hit-rate denominator was omitting cacheWrite tokens (inflating the ratio). Both expressions now use the full prompt-token sum (input + cacheRead + cacheWrite), which is the correct base in both cases. The English tooltip is updated to match.

  • The one outstanding gap is that pnpm ui:i18n:sync was not run — 12 non-English locale files retain the old formula in their cacheHint tooltip string. Per ui/CLAUDE.md, regenerating and committing the locale bundles is a required pipeline step when en.ts is edited.

Confidence Score: 4/5

Safe to merge after running pnpm ui:i18n:sync and committing the regenerated locale files.

All logic changes are correct. The only gap is the missing i18n sync step mandated by ui/CLAUDE.md — non-English locale tooltips will show the old formula until regenerated. This is a process step that should be completed before landing per project rules.

ui/src/i18n/locales/en.ts — missing pnpm ui:i18n:sync to regenerate the 12 non-English locale bundles.

Prompt To Fix All With AI
This is a comment left during a code review.
Path: ui/src/i18n/locales/en.ts
Line: 514

Comment:
**Missing `pnpm ui:i18n:sync` run**

`ui/CLAUDE.md` specifies that after editing `en.ts`, the pipeline step `pnpm ui:i18n:sync` must be run and the regenerated locale bundles committed. The 12 non-English locale files (`de.ts`, `es.ts`, `fr.ts`, etc.) still contain the old `cacheHint` formula. The tooltip mismatch is cosmetic, but the process rule is explicit — regenerate and commit before landing.

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

Reviews (1): Last reviewed commit: "fix(ui): include cache tokens in ctx% ba..." | Re-trigger Greptile

Comment thread ui/src/i18n/locales/en.ts
avgSession: "avg session",
cacheHitRate: "Cache Hit Rate",
cacheHint: "Cache hit rate = cache read / (input + cache read). Higher is better.",
cacheHint: "Cache hit rate = cache read / (input + cache read + cache write). Higher is better.",
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 Missing pnpm ui:i18n:sync run

ui/CLAUDE.md specifies that after editing en.ts, the pipeline step pnpm ui:i18n:sync must be run and the regenerated locale bundles committed. The 12 non-English locale files (de.ts, es.ts, fr.ts, etc.) still contain the old cacheHint formula. The tooltip mismatch is cosmetic, but the process rule is explicit — regenerate and commit before landing.

Prompt To Fix With AI
This is a comment left during a code review.
Path: ui/src/i18n/locales/en.ts
Line: 514

Comment:
**Missing `pnpm ui:i18n:sync` run**

`ui/CLAUDE.md` specifies that after editing `en.ts`, the pipeline step `pnpm ui:i18n:sync` must be run and the regenerated locale bundles committed. The 12 non-English locale files (`de.ts`, `es.ts`, `fr.ts`, etc.) still contain the old `cacheHint` formula. The tooltip mismatch is cosmetic, but the process rule is explicit — regenerate and commit before landing.

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

Copy link
Copy Markdown

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

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: 3c88628bcf

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread ui/src/i18n/locales/en.ts
avgSession: "avg session",
cacheHitRate: "Cache Hit Rate",
cacheHint: "Cache hit rate = cache read / (input + cache read). Higher is better.",
cacheHint: "Cache hit rate = cache read / (input + cache read + cache write). Higher is better.",
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Regenerate locale bundles after changing cache-hit hint

This update changes only ui/src/i18n/locales/en.ts, but the cache-hit-rate math was also changed in UI logic, so non-English users will now see a stale tooltip that still describes the old denominator while the displayed metric uses input + cacheRead + cacheWrite. ui/AGENTS.md explicitly requires running pnpm ui:i18n:sync and committing regenerated locale bundles after English string changes, so skipping that step leaves localized help text inconsistent with runtime behavior.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown

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

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: c35787a200

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread ui/src/i18n/locales/en.ts
avgSession: "avg session",
cacheHitRate: "Cache Hit Rate",
cacheHint: "Cache hit rate = cache read / (input + cache read). Higher is better.",
cacheHint: "Cache hit rate = cache read / (input + cache read + cache write). Higher is better.",
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Sync localized cache-hit hint to the new denominator

Updating the English usage.overview.cacheHint formula without updating translated locale bundles leaves non-English users with a misleading tooltip while the metric now uses a different denominator. Fresh evidence in this commit is that .i18n/*.meta.json source hashes were bumped, but locale entries such as ui/src/i18n/locales/de.ts and ui/src/i18n/locales/zh-CN.ts still describe cache read / (input + cache read), so the UI explanation is now inconsistent for those locales.

Useful? React with 👍 / 👎.

@clawsweeper
Copy link
Copy Markdown
Contributor

clawsweeper Bot commented Apr 26, 2026

Closing this as duplicate or superseded after Codex automated review.

PR #68557 is superseded by merged PR #71462. Current main contains the requested Control UI runtime fixes and regression coverage for both cache-token-aware ctx% and cache-hit-rate math. The latest release tag predates the Usage overview denominator fix, so there is no shipped release to cite yet.

Best possible solution:

Close #68557 as superseded by merged PR #71462 and keep the current main implementation/tests. Track the remaining non-English cacheHitRate tooltip drift separately if maintainers want localized tooltip parity.

What I checked:

So I’m closing this here and keeping the remaining discussion on the canonical linked item.

Codex Review notes: model gpt-5.5, reasoning high; reviewed against d54d2d6b9b8a; fix evidence: commit 4282c591775b.

@clawsweeper clawsweeper Bot closed this Apr 26, 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.

1 participant