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
- Start a session
- Send multiple messages
- Check
/status or control UI display
- Context permanently shows
0/{configured_value} (0%)
- 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
- Cannot determine context usage - always shows 0%, thinking it's safe
- Cannot auto-compact - system doesn't know context is full
- Context saturation occurs multiple times without warning
- Large model has no response or very slow response
- Forced to manually reset sessions
- 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! 🐾
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. ThecontextTokensfield is hardcoded to the configured limit value and never updated with real token counts.Problem
Context: 0/{configured_value} (0%)contextTokensfield in session metadata is static, not dynamicReproduction Steps
/statusor control UI display0/{configured_value} (0%)Expected Behavior
Actual Behavior
contextTokensis hardcoded to configured value (e.g., 50000, 70000, etc.)0/{contextTokens} (0%)Affected Version
Environment Info
Root Cause Analysis
The
contextTokensfield insessions.jsonis set once when session is created:But the actual token count from llama.cpp is never aggregated into this field.
Proposed Fixes
Fix 1: Dynamic contextTokens Calculation
Fix 2: Real-time Token Usage Update
Fix 3: UI Display Real Usage Rate
Fix 4: Context Usage Alerts
Impact
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_statuscommand outputsessions.jsonmetadata fileLabels
bug,session,context,monitoring,token-countingPlease investigate and fix this critical monitoring bug! 🐾