openclaw - 💡(How to fix) Fix [Bug]: Web UI msg-meta contextPercent shows 100% after long sessions due to _j() accumulating cacheRead across messages [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openclaw/openclaw#76059Fetched 2026-05-03 04:42:47
View on GitHub
Comments
1
Participants
2
Timeline
5
Reactions
2
Author
Timeline (top)
closed ×1commented ×1mentioned ×1subscribed ×1

Root Cause

In control-ui/assets/index-ckUmEo1l.js, the _j() function accumulates usage from all assistant messages:

function _j(e,t){
  let n=0,r=0,i=0,a=0,o=0,s=null,c=!1;
  for(let{message:t}of e.messages){
    let e=t;
    if(e.role!==`assistant`)continue;
    let l=e.usage;
    l&&(c=!0,
      n+=l.input??...,           // ← accumulates across ALL messages
      i+=l.cacheRead??...,       // ← BUG: cacheRead is per-API-call cache hit size
      a+=l.cacheWrite??...);
  }
  let l=n+i+a,
    u=t&&l>0?Math.min(Math.round(l/t*100),100):null;
  return {..., contextPercent:u};
}

The issue is:

  1. cacheRead is accumulated across all API calls: Each API response reports cacheRead as how many tokens were read from the cache for that specific call. After 821 calls, this sum reaches ~69M tokens.
  2. The formula l = input + cacheWrite + cacheRead / contextTokens produces 7120% which gets capped to 100% by Math.min(..., 100).
  3. kw() function (context bar at the top of the chat pane) correctly uses the session-level totalTokens and shows ~128K/1M = 13%.

Code Example

function _j(e,t){
  let n=0,r=0,i=0,a=0,o=0,s=null,c=!1;
  for(let{message:t}of e.messages){
    let e=t;
    if(e.role!==`assistant`)continue;
    let l=e.usage;
    l&&(c=!0,
      n+=l.input??...,           // ← accumulates across ALL messages
      i+=l.cacheRead??...,       // ← BUG: cacheRead is per-API-call cache hit size
      a+=l.cacheWrite??...);
  }
  let l=n+i+a,
    u=t&&l>0?Math.min(Math.round(l/t*100),100):null;
  return {..., contextPercent:u};
}
RAW_BUFFERClick to expand / collapse

Bug Description

After a long session (821 assistant messages, ~20 hours of conversation), the Web UI message metadata bar shows 100% ctx with a red/danger indicator. This makes it look like the 1M context window is full, but the actual context usage is only ~12.8%.

Root Cause

In control-ui/assets/index-ckUmEo1l.js, the _j() function accumulates usage from all assistant messages:

function _j(e,t){
  let n=0,r=0,i=0,a=0,o=0,s=null,c=!1;
  for(let{message:t}of e.messages){
    let e=t;
    if(e.role!==`assistant`)continue;
    let l=e.usage;
    l&&(c=!0,
      n+=l.input??...,           // ← accumulates across ALL messages
      i+=l.cacheRead??...,       // ← BUG: cacheRead is per-API-call cache hit size
      a+=l.cacheWrite??...);
  }
  let l=n+i+a,
    u=t&&l>0?Math.min(Math.round(l/t*100),100):null;
  return {..., contextPercent:u};
}

The issue is:

  1. cacheRead is accumulated across all API calls: Each API response reports cacheRead as how many tokens were read from the cache for that specific call. After 821 calls, this sum reaches ~69M tokens.
  2. The formula l = input + cacheWrite + cacheRead / contextTokens produces 7120% which gets capped to 100% by Math.min(..., 100).
  3. kw() function (context bar at the top of the chat pane) correctly uses the session-level totalTokens and shows ~128K/1M = 13%.

Impact

Users see a misleading red "100% ctx" badge on every assistant message in a long session, suspecting the context window is full when it isnt.

Reproduce

  1. Use a model with reasoning (e.g. deepseek-v4-pro) in a long conversation
  2. Accumulate 200+ assistant messages with API usage data
  3. Observe _j() returning contextPercent: 100 from summary stats
  4. Check kw() context bar — shows correct lower value

Expected Behavior

_j() should either:

  • Use the latest (current) API response usage instead of accumulating across all messages, OR
  • Use the same session-level totalTokens approach as kw(), OR
  • Remove cacheRead from the contextPercent denominator since it is already included in the per-call context sizing

Environment

  • OpenClaw version: 2026.4.24
  • Model: deepseek-v4-pro
  • Session: 821 assistant messages, 67M cumulative API tokens

extent analysis

TL;DR

The issue can be fixed by modifying the _j() function to correctly calculate the context percentage, either by using the latest API response usage or the session-level totalTokens approach.

Guidance

  • Review the _j() function and consider using the latest API response usage instead of accumulating across all messages to calculate the context percentage.
  • Alternatively, update the _j() function to use the same session-level totalTokens approach as the kw() function to ensure consistency in context percentage calculations.
  • Verify the fix by checking the context percentage displayed in the Web UI message metadata bar and comparing it with the value shown in the kw() context bar.
  • Test the fix with a long conversation and multiple API calls to ensure the context percentage is accurately calculated.

Example

function _j(e, t) {
  // ...
  let latestUsage = e.messages[e.messages.length - 1].usage;
  let contextPercent = latestUsage ? Math.min(Math.round(latestUsage.input / t * 100), 100) : null;
  return { ..., contextPercent };
}

Note: This example assumes using the latest API response usage to calculate the context percentage.

Notes

The provided fix assumes that the issue is solely due to the incorrect accumulation of cacheRead values. However, the actual implementation may require additional modifications to ensure accurate context percentage calculations.

Recommendation

Apply a workaround by modifying the _j() function to use the latest API response usage or the session-level totalTokens approach, as this will provide a more accurate context percentage calculation.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

openclaw - 💡(How to fix) Fix [Bug]: Web UI msg-meta contextPercent shows 100% after long sessions due to _j() accumulating cacheRead across messages [1 comments, 2 participants]