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:
- Reading the session JSONL file
- Estimating tokens from message content, tool calls, and results
- 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
- Start fresh OpenClaw session (
/new or /reset)
- Check context usage:
python3 scripts/context_healer.py --json
- Observe:
"context_percent": 100.0 despite minimal actual usage
- 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.
Problem Description
The
contextTokensfield insessions.jsonis 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
Evidence
Current Behavior
Session File Analysis
Verification Command
Expected Behavior
The
contextTokensfield should track actual token consumption for the session, not the model's maximum capacity.Temporary Workaround
Created
scripts/context_workaround.pythat manually calculates actual usage by:Workaround results: Shows accurate 4.4% usage vs false 100%
System Details
Reproduction Steps
/newor/reset)python3 scripts/context_healer.py --json"context_percent": 100.0despite minimal actual usageProposed Fix
Update the session tracking logic to:
contextTokensfieldmaxContextTokensor similar field for model limitsFiles to Investigate
sessions.jsonNote: This bug affects all context monitoring and safety systems that rely on the
contextTokensfield. Priority fix recommended.