openclaw - 💡(How to fix) Fix [Bug] Context monitor shows 0% forever - contextTokens hardcoded, not dynamic [3 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#52219Fetched 2026-04-08 01:14:04
View on GitHub
Comments
3
Participants
2
Timeline
6
Reactions
0
Author
Participants
Timeline (top)
commented ×3closed ×1cross-referenced ×1locked ×1

Error Message

if (actualTokens > 0.8 * contextWindow) warn();

Root Cause

The contextTokens field in sessions.json is set once when session is created:

"agent:main:main": {
  "sessionId": "d8350628-882d-4acf-a31b-6bd0f7d82132",
  "contextTokens": 50000,  // ← BUG: static value, should be dynamic
  "updatedAt": 1774179679613,
  ...
}

But the actual token count from llama.cpp is never aggregated into this field.

Code Example

"agent:main:main": {
  "sessionId": "d8350628-882d-4acf-a31b-6bd0f7d82132",
  "contextTokens": 50000,  // ← BUG: static value, should be dynamic
  "updatedAt": 1774179679613,
  ...
}

---

// Current (incorrect)
const contextTokens = 50000; // hardcoded

// Should be dynamic calculation
function calculateActualTokens(): number {
  // Read recent messages from JSONL session file
  // Sum token counts per message from API responses
  // Return actual total
}

---

// Current (incorrect)
display = 0 / 200000;

// Should be
actualTokens = calculateActualTokens();
display = actualTokens / contextWindow;

---

if (actualTokens > 0.8 * contextWindow) warn();
if (actualTokens > 0.9 * contextWindow) severeWarning();
if (actualTokens > 0.95 * contextWindow) autoCompact();

// UI prompt
⚠️ Context usage: 85% (170k/200k)
Suggestion: Send /compact to clean history
RAW_BUFFERClick to expand / collapse

Bug Report: Context Monitor Shows 0% Forever - contextTokens Hardcoded

🐛 Bug Description

All sessions' Context monitor permanently displays Context: 0/200k (0%), but actual token usage is not reflected. The contextTokens field is hardcoded to the configured limit value and never updated with real token counts.

Problem

  • All sessions show Context: 0/{configured_value} (0%)
  • Actual token usage from llama.cpp is not being aggregated
  • The contextTokens field in session metadata is static, not dynamic
  • Cannot determine actual context usage from UI
  • Auto-compaction cannot trigger based on real usage

Reproduction Steps

  1. Start a session
  2. Send multiple messages
  3. Check /status or control UI display
  4. Context permanently shows 0/{configured_value} (0%)
  5. Actual token usage is not reflected

Expected Behavior

  • Display actual token usage in real-time
  • Show percentage of contextWindow used
  • Trigger warnings at 80%, 90%, 95% thresholds
  • Auto-compact when saturation is detected

Actual Behavior

  • contextTokens is hardcoded to configured value (e.g., 50000, 70000, etc.)
  • UI always displays 0/{contextTokens} (0%)
  • Token usage from llama.cpp API is not being counted

Affected Version

  • OpenClaw: 2026.3.13
  • Impact: All session types (webchat, Telegram, Discord, WhatsApp, etc.)

Environment Info

  • Node.js: v24.14.0
  • Platform: Windows_NT 10.0.26200 (x64)
  • OpenClaw: 2026.3.13 (61d171a)
  • Model: 5600x-local/Qwen3.5-35B-A3B
  • llama.cpp contextWindow: 81920 (configured)

Root Cause Analysis

The contextTokens field in sessions.json is set once when session is created:

"agent:main:main": {
  "sessionId": "d8350628-882d-4acf-a31b-6bd0f7d82132",
  "contextTokens": 50000,  // ← BUG: static value, should be dynamic
  "updatedAt": 1774179679613,
  ...
}

But the actual token count from llama.cpp is never aggregated into this field.

Proposed Fixes

Fix 1: Dynamic contextTokens Calculation

// Current (incorrect)
const contextTokens = 50000; // hardcoded

// Should be dynamic calculation
function calculateActualTokens(): number {
  // Read recent messages from JSONL session file
  // Sum token counts per message from API responses
  // Return actual total
}

Fix 2: Real-time Token Usage Update

  • Increment on each message load
  • Reset to 0 after compact
  • Decrement on each message deletion

Fix 3: UI Display Real Usage Rate

// Current (incorrect)
display = 0 / 200000;

// Should be
actualTokens = calculateActualTokens();
display = actualTokens / contextWindow;

Fix 4: Context Usage Alerts

if (actualTokens > 0.8 * contextWindow) warn();
if (actualTokens > 0.9 * contextWindow) severeWarning();
if (actualTokens > 0.95 * contextWindow) autoCompact();

// UI prompt
⚠️ Context usage: 85% (170k/200k)
Suggestion: Send /compact to clean history

Impact

  1. Cannot determine context usage - always shows 0%, thinking it's safe
  2. Cannot auto-compact - system doesn't know context is full
  3. Context saturation occurs multiple times without warning
  4. Large model has no response or very slow response
  5. Forced to manually reset sessions
  6. Wasted time and energy repeating work

Suggested Priority

High: Fix token counting mechanism to display real usage Medium: Add context usage warnings and auto-compaction triggers Low: Improve UI to show actual token statistics

Related Files

  • session_status command output
  • sessions.json metadata file
  • llama.cpp API responses with token usage

Labels

bug, session, context, monitoring, token-counting


Please investigate and fix this critical monitoring bug! 🐾

extent analysis

Fix Plan

To fix the context monitor issue, we will implement the following steps:

  • Update the contextTokens field to be dynamic
  • Calculate the actual token count from llama.cpp API responses
  • Display the actual token usage in the UI
  • Add context usage alerts and auto-compaction triggers

Code Changes

We will make the following code changes:

// Calculate actual token count
function calculateActualTokens(sessionId: string): number {
  const sessionFile = `sessions/${sessionId}.jsonl`;
  const tokenCounts = [];
  const fs = require('fs');
  const readline = require('readline');

  const fileStream = fs.createReadStream(sessionFile);
  const rl = readline.createInterface({
    input: fileStream,
    crlfDelay:

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] Context monitor shows 0% forever - contextTokens hardcoded, not dynamic [3 comments, 2 participants]