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
- GLM-5 API doesn't return token usage information
- OpenClaw sets
contextTokens to the maximum value (contextWindow) instead of estimating actual usage
totalTokensFresh is set to false
- The threshold check
contextTokens > contextWindow - reserveTokens cannot be performed correctly
- 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
- Character-based estimation:
chars / 4 ≈ tokens
- 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
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
contextTokensto the maximum value (contextWindow) instead of estimating actual usagetotalTokensFreshis set tofalsecontextTokens > contextWindow - reserveTokenscannot be performed correctlyObserved Output
{ "contextTokens": 202752, "totalTokensFresh": false }Proposed Solution
Implement a fallback token estimation mechanism when API doesn't return usage stats:
Estimation Methods
chars / 4 ≈ tokensmessage_count * average_tokens_per_messageExpected Behavior
Benefits
Environment