Skip to content

[Bug]: contextTokens field shows model maximum instead of actual usage causing false 100% context readings #6012

@crayfellow

Description

@crayfellow

Problem Description

The contextTokens field in sessions.json is incorrectly set to the model's maximum context limit (200,000 for Claude Sonnet 4) instead of tracking actual token usage. This causes false 100% context readings on fresh sessions.

Impact

  • False 100% context readings - Every session shows maximum context usage regardless of actual content
  • Unnecessary safety system triggers - Context monitoring scripts incorrectly trigger high-usage alerts
  • Model routing issues - Systems route to local models unnecessarily due to false high usage readings
  • User experience degradation - Constant false alarms about context limits

Evidence

Current Behavior

// From sessions.json
{
  "agent:main:main": {
    "contextTokens": 200000,  // ← Always shows maximum limit
    "model": "claude-sonnet-4-20250514"
    // ...
  }
}

Session File Analysis

  • Fresh session file: Only 31 lines of JSONL content
  • Estimated actual usage: ~4.4% of context limit
  • Reported usage: 100% (false positive)

Verification Command

# Show the false 100% reading
python3 scripts/context_healer.py --json
# Returns: "context_percent": 100.0

Expected Behavior

The contextTokens field should track actual token consumption for the session, not the model's maximum capacity.

Temporary Workaround

Created scripts/context_workaround.py that manually calculates actual usage by:

  1. Reading the session JSONL file
  2. Estimating tokens from message content, tool calls, and results
  3. Calculating percentage against model limits

Workaround results: Shows accurate 4.4% usage vs false 100%

System Details

  • OpenClaw Version: Latest (as of 2026-02-01)
  • OS: macOS 14.6.0 (arm64)
  • Model: claude-sonnet-4-20250514
  • Context Limit: 200,000 tokens

Reproduction Steps

  1. Start fresh OpenClaw session (/new or /reset)
  2. Check context usage: python3 scripts/context_healer.py --json
  3. Observe: "context_percent": 100.0 despite minimal actual usage
  4. Verify: Session JSONL file has minimal content (e.g., 31 lines)

Proposed Fix

Update the session tracking logic to:

  • Calculate actual token usage from session content
  • Store real usage count in contextTokens field
  • Preserve maxContextTokens or similar field for model limits

Files to Investigate

  • Session management code that writes to sessions.json
  • Token counting/tracking logic
  • Context monitoring systems

Note: This bug affects all context monitoring and safety systems that rely on the contextTokens field. Priority fix recommended.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    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