Skip to content

Instantly share code, notes, and snippets.

@Richard-Weiss
Last active February 24, 2026 17:12
Show Gist options
  • Select an option

  • Save Richard-Weiss/ffadccca6238b3c9c9be5ebf23fa4487 to your computer and use it in GitHub Desktop.

Select an option

Save Richard-Weiss/ffadccca6238b3c9c9be5ebf23fa4487 to your computer and use it in GitHub Desktop.
CC context statusline
#!/bin/bash
# Claude Code status line - shows context window usage percentage
# Last update: Claude Code 2.1.34
input=$(cat)
MODEL=$(echo "$input" | jq -r '.model.display_name')
MODEL_ID=$(echo "$input" | jq -r '.model.id')
CONTEXT_SIZE=$(echo "$input" | jq -r '.context_window.context_window_size')
USAGE=$(echo "$input" | jq '.context_window.current_usage')
# Max output tokens for effective window calculation
# CC internally caps this to JE1=20000 when computing the autocompact threshold
# The actual model max doesn't matter here — only the capped value does
MAX_OUTPUT_CAP=20000
case "$MODEL_ID" in
*opus-4-6*) MODEL_MAX=128000 ;;
*opus-4-5*|*sonnet-4*|*haiku-4*) MODEL_MAX=64000 ;;
*opus-4*) MODEL_MAX=32000 ;;
*3-5*) MODEL_MAX=8192 ;;
*claude-3-opus*) MODEL_MAX=4096 ;;
*claude-3-sonnet*) MODEL_MAX=8192 ;;
*claude-3-haiku*) MODEL_MAX=4096 ;;
*) MODEL_MAX=32000 ;;
esac
# min(modelMax, cap)
[ "$MODEL_MAX" -lt "$MAX_OUTPUT_CAP" ] && MAX_OUTPUT=$MODEL_MAX || MAX_OUTPUT=$MAX_OUTPUT_CAP
# Calculate autocompact buffer
# EHA = contextSize - maxOutputTokens (available context)
EHA=$((CONTEXT_SIZE - MAX_OUTPUT))
if [ -n "$CLAUDE_AUTOCOMPACT_PCT_OVERRIDE" ]; then
# threshold = min(EHA * pct/100, EHA - 13000)
PCT_THRESHOLD=$((EHA * CLAUDE_AUTOCOMPACT_PCT_OVERRIDE / 100))
DEFAULT_THRESHOLD=$((EHA - 13000))
if [ "$PCT_THRESHOLD" -lt "$DEFAULT_THRESHOLD" ]; then
THRESHOLD=$PCT_THRESHOLD
else
THRESHOLD=$DEFAULT_THRESHOLD
fi
else
THRESHOLD=$((EHA - 13000))
fi
# Buffer = contextSize - threshold (only if autocompact enabled)
# Check env vars and config file
AUTOCOMPACT_ENABLED=1
# Check DISABLE_COMPACT env var
if [ -n "$DISABLE_COMPACT" ] && [ "$DISABLE_COMPACT" != "0" ] && [ "$DISABLE_COMPACT" != "false" ]; then
AUTOCOMPACT_ENABLED=0
fi
# Check DISABLE_AUTO_COMPACT env var
if [ -n "$DISABLE_AUTO_COMPACT" ] && [ "$DISABLE_AUTO_COMPACT" != "0" ] && [ "$DISABLE_AUTO_COMPACT" != "false" ]; then
AUTOCOMPACT_ENABLED=0
fi
# Check config file (defaults to true if not set)
CONFIG_FILE="$HOME/.claude.json"
if [ -f "$CONFIG_FILE" ]; then
CONFIG_VAL=$(jq -r 'if has("autoCompactEnabled") then .autoCompactEnabled else true end' "$CONFIG_FILE" 2>/dev/null)
if [ "$CONFIG_VAL" = "false" ]; then
AUTOCOMPACT_ENABLED=0
fi
fi
# Calculate buffer based on compact settings
# - Full autocompact enabled: use full threshold buffer
# - Autocompact disabled but compact enabled: use 3k blocking buffer
# - All compact disabled: no buffer
COMPACT_DISABLED=0
if [ -n "$DISABLE_COMPACT" ] && [ "$DISABLE_COMPACT" != "0" ] && [ "$DISABLE_COMPACT" != "false" ]; then
COMPACT_DISABLED=1
fi
if [ "$AUTOCOMPACT_ENABLED" = "1" ]; then
BUFFER=$((CONTEXT_SIZE - THRESHOLD))
elif [ "$COMPACT_DISABLED" = "0" ]; then
BUFFER=3000
else
BUFFER=0
fi
if [ "$USAGE" != "null" ]; then
# Calculate current context from current_usage fields
CURRENT_TOKENS=$(echo "$USAGE" | jq '.input_tokens + .cache_creation_input_tokens + .cache_read_input_tokens')
# Show usage as % of effective space
# With autocompact: denominator = threshold (hits 100% when compact triggers)
# Without autocompact: denominator = effective window (EHA)
if [ "$AUTOCOMPACT_ENABLED" = "1" ]; then
EFFECTIVE=$THRESHOLD
else
EFFECTIVE=$EHA
fi
PERCENT_USED=$((CURRENT_TOKENS * 100 / EFFECTIVE))
[ "$PERCENT_USED" -gt 100 ] && PERCENT_USED=100
echo "[$MODEL] Context: ${PERCENT_USED}%"
else
echo "[$MODEL] Context: n/a"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment