Skip to content

fix(webchat): use totalTokens for context utilization display#51536

Closed
VipinSinghChauhan wants to merge 6 commits intoopenclaw:mainfrom
VipinSinghChauhan:fix/webchat-context-calculation-premature-100
Closed

fix(webchat): use totalTokens for context utilization display#51536
VipinSinghChauhan wants to merge 6 commits intoopenclaw:mainfrom
VipinSinghChauhan:fix/webchat-context-calculation-premature-100

Conversation

@VipinSinghChauhan
Copy link
Copy Markdown

@VipinSinghChauhan VipinSinghChauhan commented Mar 21, 2026

Fixes #51507

The context warning banner and /status command were reading session.inputTokens to compute context fill %, but inputTokens accumulates input tokens across ALL API sub-calls in a run (tool-use loops, retries, compaction). With models like MiniMax-M2.5 that chain many sub-calls, this number far exceeds the actual context size, causing premature 100% warnings and a blocked chat input.

The correct field is session.totalTokens, which the backend already derives from lastCallUsage (the final API call only) — the true prompt-size snapshot. This is the same value the TUI uses, which explains why TUI showed accurate context while WebChat did not.

Changes:

  • ui/views/chat.ts: renderContextNotice uses totalTokens (not inputTokens)
  • ui/chat/slash-command-executor.ts: /status Context % uses totalTokens
  • ui/views/chat.browser.test.ts: update fixture to totalTokens so the existing context notice test continues to pass

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 required for the fix
  • 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:

Fixes openclaw#51507

The context warning banner and /status command were reading
session.inputTokens to compute context fill %, but inputTokens
accumulates input tokens across ALL API sub-calls in a run
(tool-use loops, retries, compaction). With models like MiniMax-M2.5
that chain many sub-calls, this number far exceeds the actual context
size, causing premature 100% warnings and a blocked chat input.

The correct field is session.totalTokens, which the backend already
derives from lastCallUsage (the final API call only) — the true
prompt-size snapshot. This is the same value the TUI uses, which
explains why TUI showed accurate context while WebChat did not.

Changes:
- ui/views/chat.ts: renderContextNotice uses totalTokens (not inputTokens)
- ui/chat/slash-command-executor.ts: /status Context % uses totalTokens
- ui/views/chat.browser.test.ts: update fixture to totalTokens so the
  existing context notice test continues to pass
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Mar 21, 2026

Greptile Summary

This PR fixes a context-utilization display bug in WebChat where session.inputTokens (cumulative across all API sub-calls in a run) was used instead of session.totalTokens (the prompt-size snapshot from the final API call). The result was premature 100% context warnings and a blocked chat input with multi-call models like MiniMax-M2.5.

Changes:

  • ui/src/ui/views/chat.tsrenderContextNotice now reads session.totalTokens instead of session.inputTokens.
  • ui/src/ui/chat/slash-command-executor.ts/status context % calculation switched from inputTokens to totalTokens, matching the TUI behaviour.
  • ui/src/ui/views/chat.browser.test.ts — fixture updated from inputTokens to totalTokens so the existing context-notice test continues to pass.

Issue: slash-command-executor.node.test.ts was not updated. Its fixture already has inputTokens: 1200, totalTokens: 1500, and contextTokens: 4000. The old code yielded 1200 / 4000 = 30% (what the test asserts); the new code yields Math.round(1500 / 4000 * 100) = 38%, so that test will fail.

Confidence Score: 3/5

  • The semantic fix is correct, but the paired node test was not updated and will fail CI.
  • The logic change across chat.ts and slash-command-executor.ts is correct and well-reasoned. The browser test fixture was updated. However, slash-command-executor.node.test.ts still asserts 30% context while the new code produces 38% — a test that was passing before will now fail in CI, blocking the merge until fixed.
  • ui/src/ui/chat/slash-command-executor.node.test.ts — expected context percentage string must be updated from 30% to 38%.
Prompt To Fix All With AI
This is a comment left during a code review.
Path: ui/src/ui/chat/slash-command-executor.ts
Line: 310-311

Comment:
**Existing node test will break with new percentage**

`slash-command-executor.node.test.ts` (line ~325) was not updated and now produces the wrong expected value. The fixture already has `inputTokens: 1200`, `totalTokens: 1500`, and `contextTokens: 4000`. The old code computed context % from `inputTokens`: `1200 / 4000 = 30%`, which matches what the test asserts. With this change, `contextUsed = session.totalTokens = 1500`, so the new result is `Math.round(1500 / 4000 * 100) = Math.round(37.5) = 38%` — but the test still expects `Context: **30%** of 4k`.

The fix is to update the expected string in `slash-command-executor.node.test.ts`:

```
"**Session Usage**\nInput: **1.2k** tokens\nOutput: **300** tokens\nTotal: **1.5k** tokens\nContext: **38%** of 4k\nModel: `gpt-4.1-mini`"
```

(or align the fixture's `totalTokens` to a value that yields the same 30% to minimize test delta).

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

Last reviewed commit: "fix(webchat): use to..."

Comment thread ui/src/ui/chat/slash-command-executor.ts
@steipete
Copy link
Copy Markdown
Contributor

Closing this as implemented after Codex review.

Current main already implements this fix on the affected Control UI surfaces and includes matching tests, so PR #51536 is superseded by code now on main. The later timeline cross-reference to PR #71462 matches the behavior now present in the repo.

What I checked:

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

Review notes: reviewed against 42514156e03c; fix evidence: commit 42514156e03c.

@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.

[Bug]: [WebChat/Control-UI] Context calculation is inaccurate - shows 100% before actual limit reached, while TUI works correctly

2 participants