openclaw - 💡(How to fix) Fix [Feature] Local context token tracking when model API doesn't return usage data [1 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#54996Fetched 2026-04-08 01:33:50
View on GitHub
Comments
0
Participants
1
Timeline
0
Reactions
0
Author
Participants

Root Cause

  • totalTokensFresh stays False for all sessions
  • /status always shows Context: 0/205k (0%) — completely unusable
  • Auto-compaction never triggers because OpenClaw does not know the current context size
  • Users have no visibility into context consumption
RAW_BUFFERClick to expand / collapse

Problem

Some model providers (e.g. MiniMax) do not return usage data in the format OpenClaw expects. As a result:

  • totalTokensFresh stays False for all sessions
  • /status always shows Context: 0/205k (0%) — completely unusable
  • Auto-compaction never triggers because OpenClaw does not know the current context size
  • Users have no visibility into context consumption

Current behavior

  • /status reads usage from the model API response
  • When API returns {"input": 0, "output": 0, "totalTokens": 0}, OpenClaw gives up and shows 0%
  • contextTokens in session store contains the model's max context window (e.g. 204800), not the actual usage

Proposed solution

When totalTokensFresh is False (API usage unavailable), OpenClaw should:

  1. Parse the session JSONL file locally
  2. Estimate token count from stored messages (chars / 4 or similar heuristic)
  3. Display in /status with an [estimated] indicator, e.g. Context: ~194k/205k (95%) [estimated]
  4. Use this estimate for auto-compaction threshold calculation

Evidence

  • Session file was 600KB+ with 247+ messages
  • Manually parsed: ~102k tokens (~50% of 204.8k context window)
  • But /status still showed Context: 0/205k (0%)

Environment

  • OpenClaw 2026.3.24
  • Model: minimax/MiniMax-M2.7
  • The model's API does not return standard usage data
  • OS: macOS (Darwin 25.4.0, arm64)

extent analysis

Fix Plan

To address the issue, we need to modify OpenClaw to estimate token count from stored messages when the model API does not return usage data. Here are the steps:

  • Modify the /status endpoint to check if totalTokensFresh is False. If so, parse the session JSONL file locally to estimate the token count.
  • Implement a heuristic to estimate token count from stored messages, such as counting characters and dividing by 4.
  • Display the estimated token count in the /status endpoint with an [estimated] indicator.
  • Use the estimated token count for auto-compaction threshold calculation.

Example Code

import json

def estimate_token_count(session_file):
    """Estimate token count from stored messages in session file"""
    token_count = 0
    with open(session_file, 'r') as f:
        for line in f:
            message = json.loads(line)
            token_count += len(message['text']) // 4  # heuristic: 1 token = 4 characters
    return token_count

def get_status(session_file, max_context_window):
    """Get status with estimated token count if API usage is unavailable"""
    try:
        # Try to get usage data from model API
        usage_data = get_usage_data_from_api()
        total_tokens = usage_data['totalTokens']
    except Exception:
        # If API usage is unavailable, estimate token count from session file
        total_tokens = estimate_token_count(session_file)
        estimated = True
    else:
        estimated = False

    context_size = total_tokens / max_context_window * 100
    status = f"Context: {total_tokens}/{max_context_window} ({context_size:.0f}%)"
    if estimated:
        status += " [estimated]"
    return status

# Example usage
session_file = "path/to/session.jsonl"
max_context_window = 204800
status = get_status(session_file, max_context_window)
print(status)

Verification

To verify that the fix worked, check the /status endpoint to see if it displays the estimated token count with an [estimated] indicator. Also, verify that auto-compaction triggers correctly based on the estimated token count.

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