Skip to content

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

Open

Description

@seanGSISG

Preflight Checklist

  • I have searched existing issues and this hasn't been reported yet
  • This is a single bug report (please file separate reports for different bugs)
  • 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

Image Image

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

Activity

  1. github-actions commented on Jan 14, 2026

    @github-actions

    Found 3 possible duplicate issues:

    1. Context display shows 15% free but reports 'Context limit reached' #18178
    2. [BUG] Context low warning shows 7% when actually 36% free (auto-compact disabled) #15005
    3. [Bug] Context limit reached with 32k tokens free space and auto-compact disabled #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

  2. smconner commented on Jan 18, 2026

    @smconner

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

  3. qte77 commented on Feb 15, 2026

    @qte77

    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:

    # 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.
  4. Sebi2020 commented on May 23, 2026

    @Sebi2020

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

  5. adam-stone-zocdoc commented on Jun 4, 2026

    @adam-stone-zocdoc

    I am seeing the same thing today.

    Image
  6. junaidtitan commented on Jun 5, 2026

    @junaidtitan

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:corearea:tuibugSomething isn't workinghas reproHas detailed reproduction stepsplatform:linuxIssue specifically occurs on Linux

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions