[BUG] token calculation mismatch causes premature auto-compaction
Preflight Checklist
- [x] I have searched existing issues and this hasn't been reported yet
- [x] This is a single bug report (please file separate reports for different bugs)
- [x] I am using the latest version of Claude Code
What's Wrong?
The /context command displays a total token count that doesn't match the sum of individual category breakdowns.
Example from my session:
claude-opus-4-5 · 162k/200k tokens (81%)
Estimated usage by category:
⛁ System prompt: 2.6k tokens (1.3%)
⛁ Custom agents: 280 tokens (0.1%)
⛁ Memory files: 271 tokens (0.1%)
⛁ Skills: 1.4k tokens (0.7%)
⛁ Messages: 49.3k tokens (24.6%)
⛶ Free space: 113k (56.6%)
⛝ Autocompact buffer: 33k tokens (16.5%)
The math doesn't add up:
| Metric | Value |
|--------|-------|
| Header shows | 162k/200k (81%) used |
| Sum of categories | 2.6k + 0.28k + 0.27k + 1.4k + 49.3k ≈ 53.9k |
| Free space shown | 113k (56.6%) |
- If 162k tokens are used, free space should be ~38k, not 113k
- If free space is 113k, actual usage should be ~87k (including buffer), not 162k
- The 81% usage indicator appears significantly inflated
Impact: Auto-compaction triggers prematurely based on the inflated percentage, causing conversations to be compacted
when there's still substantial free space available. This disrupts workflow and loses important context unnecessarily.
What Should Happen?
- The total token count in the header should accurately reflect the sum of all category breakdowns
- The percentage calculation should be consistent with both the used tokens and free space
- Auto-compaction should only trigger when context is genuinely approaching capacity, not based on miscalculated metrics
Error Messages/Logs
Steps to Reproduce
- Start a Claude Code session with multiple MCP tools, custom agents, memory files, and skills configured
- Have a conversation with moderate message history
- Run
/contextcommand - Compare the total tokens shown in the header with the sum of individual categories
- Observe the discrepancy between reported usage and actual breakdown
Claude Model
None
Is this a regression?
Yes, this worked in a previous version
Last Working Version
v2.1.20
Claude Code Version
v2.1.31
Platform
Other
Operating System
macOS
Terminal/Shell
Terminal.app (macOS)
Additional Information
_No response_
This issue has 6 comments on GitHub. Read the full discussion on GitHub ↗