Skip to content

Fallback token estimation for auto-compaction when API doesn't return usage stats #48365

Description

@SallyLX

Problem

When using models that don't return token usage statistics (e.g., GLM-5 via Alibaba Cloud Bailian API), OpenClaw's auto-compaction threshold check fails to work.

Current Behavior

  1. GLM-5 API doesn't return token usage information
  2. OpenClaw sets contextTokens to the maximum value (contextWindow) instead of estimating actual usage
  3. totalTokensFresh is set to false
  4. The threshold check contextTokens > contextWindow - reserveTokens cannot be performed correctly
  5. Auto-compaction doesn't trigger until a 400 error occurs (overflow recovery)

Observed Output

| glm-5 | unknown/203k (?%) |
{
  "contextTokens": 202752,
  "totalTokensFresh": false
}

Proposed Solution

Implement a fallback token estimation mechanism when API doesn't return usage stats:

Estimation Methods

  1. Character-based estimation: chars / 4 ≈ tokens
  2. Message count estimation: message_count * average_tokens_per_message

Expected Behavior

API returns token stats?
    ↓ Yes                    ↓ No
Use API data            Use local estimation
    ↓                        ↓
    └────────┬───────────────┘
             ↓
    Check threshold
             ↓
    Trigger auto-compaction

Benefits

  • Auto-compaction works for all models, not just those with token stats API
  • Better user experience (no 400 errors before compaction)
  • More reliable token management

Environment

  • OpenClaw: 2026.3.13
  • Model: GLM-5 via Alibaba Cloud Bailian API
  • Context Window: 203k tokens

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions