Skip to content

[Feature] Local context token tracking when model API doesn't return usage data #54996

@w000001

Description

@w000001

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Normal backlog priority with limited blast radius.clawsweeper:fix-shape-clearClawSweeper found a clear likely implementation shape for this issue.clawsweeper:needs-maintainer-reviewClawSweeper marked this issue as needing maintainer review before automation.clawsweeper:needs-product-decisionClawSweeper marked this issue as needing a product or behavior decision.clawsweeper:no-new-fix-prClawSweeper does not recommend queueing a new automated fix PR for this issue.clawsweeper:source-reproClawSweeper found a high-confidence source-level issue reproduction.impact:session-stateSession, memory, transcript, context, or agent state can drift or corrupt.issue-rating: 🦞 diamond lobsterVery strong issue quality with high-confidence source-level or clear reproduction.

    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