[BUG] Context percentage mismatch between /context, statusline API, and internal limit trigger
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?
Context percentage displays are inconsistent across different UI elements. The /context command and statusline JSON API show 67% used (31.7% free), but the built-in context warning on the right side of the status bar shows "2% remaining" and the system triggers "Context limit reached" even though there's ~63k tokens of free space available.
What Should Happen?
All context usage displays should show consistent percentages.
If /context reports 67% used with 63k tokens free (31.7%), the status bar warning should also show ~33% remaining (not 2%), and Context limit reached should not trigger until context is actually near capacity.
Error Messages/Logs
`Context limit reached · /compact or /clear to continue`
This message appeared while /context showed:
`claude-opus-4-5-20251101 · 134k/200k tokens (67%)` | `Free space: 63k (31.7%)`
Steps to Reproduce
- Set autocompact to disabled in /config
- Start a long conversation (or continue an existing one) with Claude Code
- Work until the "Context limit reached" message appears
- Run /context command to see actual usage breakdown
- Compare the "Free space" percentage from /context with the "X% remaining" shown in the right side of the status bar
Claude Model
Opus
Is this a regression?
Yes, this worked in a previous version
Last Working Version
2.1.6
Claude Code Version
2.1.7
Platform
Anthropic API
Operating System
Ubuntu/Debian Linux
Terminal/Shell
WSL2 with Wez-Term
Additional Information
<img width="709" height="314" alt="Image" src="https://github.com/user-attachments/assets/21a37405-7e9c-463d-b6ec-1f94e454da10" />
<img width="1288" height="145" alt="Image" src="https://github.com/user-attachments/assets/e1fca9dc-b729-456d-8f07-3bda4597b13b" />
My statusline-command.sh
#!/bin/bash
# Read JSON input from stdin
input=$(cat)
# Parse JSON fields
MODEL=$(echo "$input" | jq -r '.model.display_name')
USED=$(echo "$input" | jq -r '.context_window.used_percentage // 0')
CWD=$(echo "$input" | jq -r '.workspace.current_dir')
# Get git info
BRANCH=$(git -C "$CWD" branch --show-current 2>/dev/null || echo "")
if [ -n "$BRANCH" ]; then
git -C "$CWD" diff --quiet 2>/dev/null || DIRTY="*"
GIT="${BRANCH}${DIRTY}"
else
GIT=""
fi
# Build progress bar (10 characters wide)
BAR_WIDTH=10
FILLED=$(printf "%.0f" $(echo "scale=2; $USED * $BAR_WIDTH / 100" | bc 2>/dev/null || echo "0"))
[ -z "$FILLED" ] && FILLED=0
[ "$FILLED" -lt 0 ] 2>/dev/null && FILLED=0
[ "$FILLED" -gt "$BAR_WIDTH" ] 2>/dev/null && FILLED=$BAR_WIDTH
EMPTY=$((BAR_WIDTH - FILLED))
# Build the bar string with parallelograms
BAR=""
for ((i=0; i<FILLED; i++)); do BAR+="▰"; done
for ((i=0; i<EMPTY; i++)); do BAR+="▱"; done
# Output: Percentage | Model | Git | Progress Bar
PERCENT="${USED}%"
if [ -n "$GIT" ]; then
printf "%s | %s | %s %s" "$PERCENT" "$MODEL" "$GIT" "$BAR"
else
printf "%s | %s %s" "$PERCENT" "$MODEL" "$BAR"
fi
6 Comments
Found 3 possible duplicate issues:
This issue will be automatically closed as a duplicate in 3 days.
🤖 Generated with Claude Code
🔗 Related: Orphaned background agents
This mismatch appears to have a secondary consequence: background agents become orphaned when context limit triggers early.
See #18981 for details — when "Context limit reached" fires based on one metric, the cleanup logic may be checking a different metric that still shows headroom. Result: agent never gets terminated, runs at 100%+ CPU indefinitely.
The fix for this issue (#18241) would likely also fix #18981 as a side effect.
Context percentage mismatch between /context, statusline API, and internal limit trigger
Root cause identified:
The mismatch is caused by the autocompact buffer not being accounted for consistently across UI elements.
The Math:
/contextreports:remaining_percentage(includes autocompact buffer)remaining_percentage - 16.5%Your reported numbers:
/context: 31.7% freeWhy this happens:
/contextshows total remaining space (including the reserved autocompact buffer)context_window.remaining_percentageincludes the buffer (misleading)The 16.5% buffer is NOT exposed in statusline JSON, forcing custom statusline scripts to reverse-engineer it:
```bash
# Workaround for accurate statusline display
AUTOCOMPACT_BUFFER_PCT=16.5 # Observed default (2026-02)
true_free_pct=$(echo "$remaining_pct - $AUTOCOMPACT_BUFFER_PCT" | bc -l)
Environment variable: CLAUDE_AUTOCOMPACT_PCT_OVERRIDE (1-100) can adjust the trigger threshold:
Solution: Claude Code should expose autocompact buffer data in both /context output and statusline JSON:
{
"context_window": {
"remaining_percentage": 31.7,
"autocompact_buffer_percentage": 16.5,
"true_free_percentage": 15.2
}
}
Related issues:
This would make all UI elements show consistent, accurate context usage.
Error also occurs if auto compact is disabled. /context shows 88%, but no new messages can be send. (latest claude code version as of today, macOS).
I am seeing the same thing today.
<img width="1570" height="562" alt="Image" src="https://github.com/user-attachments/assets/81251c76-c4b5-4b1c-96ab-f38059e16d8e" />
The mismatch between
/contextreporting 67% and the status bar showing 2% remaining (with "Context limit reached" firing while 63k tokens are supposedly free) is something we ran into while building cozempic, and it pointed to a real measurement gap: CC assembles the "context used" number differently in different places.cozempic current --diagnosereads your session JSONL directly and reports the ground-truth token count — independent of the status line or/contextdisplay. You can see the real number side-by-side with what CC shows you.What's most interesting for this issue specifically: if the diagnose output matches the 67% from
/contextbut not the 2% from the status bar, that confirms the status bar is computing against a different ceiling. If diagnose shows something closer to 98%, it suggests the JSONL has grown more than/contextis surfacing.Would genuinely like to know which way it goes for your session — github.com/Ruya-AI/cozempic, pip/pipx install, auto-runs. Happy to dig into the numbers with you.