Skip to content

[Bug] Context monitor shows 0% forever - contextTokens hardcoded, not dynamic #52219

@gzsiang

Description

@gzsiang

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! 🐾

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions