[BUG] Context percentage mismatch between /context, statusline API, and internal limit trigger

Status Open
Reported on v2.1.6
Maintainer reply None cached
Activity 6 comments · opened Jan 14, 2026

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

  1. Set autocompact to disabled in /config
  2. Start a long conversation (or continue an existing one) with Claude Code
  3. Work until the "Context limit reached" message appears
  4. Run /context command to see actual usage breakdown
  5. 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

View original on GitHub ↗

6 Comments

github-actions[bot] · 7 months ago

Found 3 possible duplicate issues:

  1. https://github.com/anthropics/claude-code/issues/18178
  2. https://github.com/anthropics/claude-code/issues/15005
  3. https://github.com/anthropics/claude-code/issues/18159

This issue will be automatically closed as a duplicate in 3 days.

  • If your issue is a duplicate, please close it and 👍 the existing issue instead
  • To prevent auto-closure, add a comment or 👎 this comment

🤖 Generated with Claude Code

smconner · 7 months ago

🔗 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.

qte77 · 6 months ago

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:

  • /context reports: remaining_percentage (includes autocompact buffer)
  • Autocompact buffer: 16.5% (33k tokens as of 2026-02)
  • True usable space: remaining_percentage - 16.5%

Your reported numbers:

  • /context: 31.7% free
  • Status bar: 2% remaining
  • Difference: ~29.7% (close to our observed 16.5% buffer + some variance)

Why this happens:

  1. /context shows total remaining space (including the reserved autocompact buffer)
  2. Built-in status bar shows actual usable space before compaction triggers
  3. Statusline JSON context_window.remaining_percentage includes 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:

  • Docs claim default is 95% (5% buffer)
  • Observed behavior shows 16.5% buffer
  • Buffer % = 100 - CLAUDE_AUTOCOMPACT_PCT_OVERRIDE

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:

  • #15184 (locked): Requested statusline buffer exposure
  • #18264: autoCompact: false ignored, compaction triggers early

This would make all UI elements show consistent, accurate context usage.

Sebi2020 · 3 months ago

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).

adam-stone-zocdoc · 2 months ago

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" />

junaidtitan · 2 months ago

The mismatch between /context reporting 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 --diagnose reads your session JSONL directly and reports the ground-truth token count — independent of the status line or /context display. 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 /context but 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 /context is 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.