Skip to content

Skills for Claude Code CLI such as full stack dev Cloudflare, React, Tailwind v4, and AI integrations.

fix(contextbricks): v2.1.0 - Accurate context usage after compaction

Jez11196f58ee91
diff --git a/tools/statusline-npm/README.md b/tools/statusline-npm/README.md
index 20c3bb32..572ac3a4 100644
--- a/tools/statusline-npm/README.md
+++ b/tools/statusline-npm/README.md
@@ -12,6 +12,7 @@ A custom status line for Claude Code that displays accurate token usage with a b
## ✨ Features
- 🎯 **Native context_window support** (Claude Code 2.0.65+) - accurate token data
+- 📦 **Compaction detection** (v2.1+) - shows real context usage after automatic compaction
- 🔄 **Backwards compatible** - falls back to transcript parsing for older versions
- 🧱 **Brick visualization** showing context usage at a glance
- 🔧 **Git integration**: repo:branch [commit] message | github-repo *↑↓
@@ -73,9 +74,15 @@ For Max subscribers (no API cost):
ctx [■■■■■■■■■■■■■■■■■□□□□□□□□□□□□□□□□□□□□□□□] 43% (86k/200k) | 113k free | 12m45s
```
+After context compaction (📦 indicates compaction occurred):
+```
+ctx [■■■■■■■■■■■■■■■■■■■■■■■■■■■■□□□□□□□□□□□□] 📦 68% (136k/200k) | 64k free | 2h 38m
+```
+
**Color Legend:**
- 🟦 Cyan = Used context
- ⬜ Dim hollow = Free space
+- 📦 Purple = Context has been compacted (shows actual current usage)
## 🔧 Requirements
@@ -132,6 +139,12 @@ npx contextbricks@latest init
## 📋 Changelog
+### v2.1.0 (2025-12-13)
+- **Compaction detection** - Shows accurate context usage after automatic compaction
+- **📦 indicator** - Purple box emoji when context has been compacted
+- **Hybrid calculation** - Uses cumulative tokens when normal, transcript parsing when compacted
+- **Fixes >100% display** - No more "246% (492k/200k)" after compaction
+
### v2.0.0 (2025-12-11)
- **Native context_window support** - Uses Claude Code 2.0.65+ native data
- **Added session duration** - Shows time spent in session (e.g., `12m45s`)
diff --git a/tools/statusline-npm/package.json b/tools/statusline-npm/package.json
index 945071cc..0659ecb3 100644
--- a/tools/statusline-npm/package.json
+++ b/tools/statusline-npm/package.json
@@ -1,6 +1,6 @@
{
"name": "contextbricks",
- "version": "2.0.0",
+ "version": "2.1.0",
"description": "Real-time context tracking with brick visualization for Claude Code CLI",
"keywords": [
"claude",
diff --git a/tools/statusline-npm/scripts/statusline.sh b/tools/statusline-npm/scripts/statusline.sh
index ddf716ea..6960958b 100755
--- a/tools/statusline-npm/scripts/statusline.sh
+++ b/tools/statusline-npm/scripts/statusline.sh
@@ -94,20 +94,52 @@ fi
# Build Line 2: Context Brick Visualization
# Try native context_window first (Claude Code 2.0.65+)
-input_tokens=$(echo "$input" | jq -r '.context_window.total_input_tokens // null')
-
-if [[ "$input_tokens" != "null" && -n "$input_tokens" && "$input_tokens" != "0" ]]; then
- # Use native context_window data (2.0.65+)
- output_tokens=$(echo "$input" | jq -r '.context_window.total_output_tokens // 0')
- total_tokens=$(echo "$input" | jq -r '.context_window.context_window_size // 200000')
- used_tokens=$((input_tokens + output_tokens))
+cumulative_input=$(echo "$input" | jq -r '.context_window.total_input_tokens // null')
+cumulative_output=$(echo "$input" | jq -r '.context_window.total_output_tokens // 0')
+total_tokens=$(echo "$input" | jq -r '.context_window.context_window_size // 200000')
+
+compacted=false
+
+if [[ "$cumulative_input" != "null" && -n "$cumulative_input" && "$cumulative_input" != "0" ]]; then
+ cumulative_total=$((cumulative_input + cumulative_output))
+
+ # Detect compaction: cumulative exceeds context limit
+ if [[ $cumulative_total -gt $total_tokens ]]; then
+ compacted=true
+
+ # Get ACTUAL current context from last message's input_tokens
+ transcript_path=$(echo "$input" | jq -r '.transcript_path // ""')
+
+ if [[ -f "$transcript_path" && -s "$transcript_path" ]]; then
+ # Get the last assistant message's input_tokens (actual context sent)
+ last_input=$(grep '"type":"assistant"' "$transcript_path" 2>/dev/null | \
+ tail -1 | \
+ jq -r '.message.usage.input_tokens // 0' 2>/dev/null)
+
+ if [[ -n "$last_input" && "$last_input" =~ ^[0-9]+$ && "$last_input" -gt 0 ]]; then
+ # Add cache tokens for complete picture
+ cache_read=$(grep '"type":"assistant"' "$transcript_path" 2>/dev/null | \
+ tail -1 | \
+ jq -r '.message.usage.cache_read_input_tokens // 0' 2>/dev/null)
+ [[ ! "$cache_read" =~ ^[0-9]+$ ]] && cache_read=0
+
+ used_tokens=$((last_input + cache_read))
+ else
+ # Fallback: cap at 90% if we can't determine actual usage
+ used_tokens=$(( (total_tokens * 90) / 100 ))
+ fi
+ else
+ used_tokens=$(( (total_tokens * 90) / 100 ))
+ fi
+ else
+ # Normal state: use cumulative (it's accurate when < limit)
+ used_tokens=$cumulative_total
+ fi
else
- # Fallback: Parse transcript for older Claude Code versions
+ # Fallback for older Claude Code versions (no native data)
transcript_path=$(echo "$input" | jq -r '.transcript_path // ""')
- total_tokens=200000 # Default context limit
if [[ -f "$transcript_path" && -s "$transcript_path" ]]; then
- # Parse JSONL transcript - get the LAST assistant message's token usage
last_usage=$(grep '"type":"assistant"' "$transcript_path" 2>/dev/null | \
tail -1 | \
jq -r '.message.usage // null' 2>/dev/null)
@@ -123,10 +155,7 @@ else
used_tokens=0
fi
- # Ensure we have a valid number
- if [[ ! "$used_tokens" =~ ^[0-9]+$ ]]; then
- used_tokens=0
- fi
+ [[ ! "$used_tokens" =~ ^[0-9]+$ ]] && used_tokens=0
else
used_tokens=0
fi
@@ -171,7 +200,14 @@ for ((i=0; i<free_bricks; i++)); do
brick_line+="\033[2;37m□\033[0m"
done
-brick_line+="] \033[1m${usage_pct}%\033[0m (${used_k}k/${total_k}k)"
+brick_line+="]"
+
+# Add compaction indicator if applicable
+if [[ "$compacted" == "true" ]]; then
+ brick_line+=" \033[1;35m📦\033[0m"
+fi
+
+brick_line+=" \033[1m${usage_pct}%\033[0m (${used_k}k/${total_k}k)"
# Add free space
brick_line+=" | \033[1;32m${free_k}k free\033[0m"
diff --git a/tools/statusline/README.md b/tools/statusline/README.md
index 21ed6915..cc5b98ec 100644
--- a/tools/statusline/README.md
+++ b/tools/statusline/README.md
@@ -8,6 +8,7 @@ A custom status line for Claude Code CLI that displays real-time context trackin
### Real-Time Context Tracking
- **Native context_window support** (Claude Code 2.0.65+) - accurate token data direct from Claude Code
+- **Compaction detection** (v2.1+) - shows real context usage after automatic compaction with 📦 indicator
- **Fallback for older versions** - parses transcript files if native data unavailable
- **Brick visualization** showing context usage at a glance
- **Session duration** - track how long you've been working
@@ -91,9 +92,15 @@ For Max subscribers (no API cost):
ctx [■■■■■■■■■■■■■■■■■□□□□□□□□□□□□□□□□□□□□□□□] 43% (86k/200k) | 113k free | 12m45s
```
+After context compaction (📦 indicates compaction occurred):
+```
+ctx [■■■■■■■■■■■■■■■■■■■■■■■■■■■■□□□□□□□□□□□□] 📦 68% (136k/200k) | 64k free | 2h 38m
+```
+
**Color Legend:**
- 🟦 Cyan = Used context
- ⬜ Dim hollow = Free space
+- 📦 Purple = Context has been compacted (shows actual current usage, not cumulative)
## 🔧 Requirements
@@ -125,9 +132,21 @@ ctx [■■■■■■■■■■■■■■■■■□□□□□□□□
The status line:
1. Checks for native `context_window` data (2.0.65+)
-2. Falls back to transcript parsing for older versions
-3. Calculates percentage against context limit
-4. Generates brick visualization proportional to usage
+2. Detects compaction (when cumulative tokens > context limit)
+3. If compacted: parses transcript for actual current context size
+4. Falls back to transcript parsing for older versions
+5. Generates brick visualization proportional to usage
+
+### Compaction Detection (v2.1+)
+
+Claude Code's `context_window.total_input_tokens` and `total_output_tokens` are **cumulative session totals** that don't reset after compaction. When these exceed the context limit, we know compaction occurred.
+
+To show accurate post-compaction usage, the script reads the **last assistant message's `input_tokens`** from the transcript - this represents the actual tokens sent to Claude in the most recent turn (compacted summary + new content).
+
+When compaction is detected:
+- 📦 indicator appears (purple)
+- Bar shows real current context usage
+- Percentage reflects actual capacity used
### Fallback for Older Versions
@@ -231,6 +250,12 @@ rm ~/.claude/statusline.sh
## 📋 Changelog
+### v2.1.0 (2025-12-13)
+- **Compaction detection** - Shows accurate context usage after automatic compaction
+- **📦 indicator** - Purple box emoji when context has been compacted
+- **Hybrid calculation** - Uses cumulative tokens when normal, transcript parsing when compacted
+- **Fixes >100% display** - No more "246% (492k/200k)" after compaction
+
### v2.0.0 (2025-12-11)
- **Native context_window support** - Uses Claude Code 2.0.65+ native data
- **Added session duration** - Shows time spent in session (e.g., `12m45s`)
diff --git a/tools/statusline/statusline.sh b/tools/statusline/statusline.sh
index ddf716ea..6960958b 100755
--- a/tools/statusline/statusline.sh
+++ b/tools/statusline/statusline.sh
@@ -94,20 +94,52 @@ fi
# Build Line 2: Context Brick Visualization
# Try native context_window first (Claude Code 2.0.65+)
-input_tokens=$(echo "$input" | jq -r '.context_window.total_input_tokens // null')
-
-if [[ "$input_tokens" != "null" && -n "$input_tokens" && "$input_tokens" != "0" ]]; then
- # Use native context_window data (2.0.65+)
- output_tokens=$(echo "$input" | jq -r '.context_window.total_output_tokens // 0')
- total_tokens=$(echo "$input" | jq -r '.context_window.context_window_size // 200000')
- used_tokens=$((input_tokens + output_tokens))
+cumulative_input=$(echo "$input" | jq -r '.context_window.total_input_tokens // null')
+cumulative_output=$(echo "$input" | jq -r '.context_window.total_output_tokens // 0')
+total_tokens=$(echo "$input" | jq -r '.context_window.context_window_size // 200000')
+
+compacted=false
+
+if [[ "$cumulative_input" != "null" && -n "$cumulative_input" && "$cumulative_input" != "0" ]]; then
+ cumulative_total=$((cumulative_input + cumulative_output))
+
+ # Detect compaction: cumulative exceeds context limit
+ if [[ $cumulative_total -gt $total_tokens ]]; then
+ compacted=true
+
+ # Get ACTUAL current context from last message's input_tokens
+ transcript_path=$(echo "$input" | jq -r '.transcript_path // ""')
+
+ if [[ -f "$transcript_path" && -s "$transcript_path" ]]; then
+ # Get the last assistant message's input_tokens (actual context sent)
+ last_input=$(grep '"type":"assistant"' "$transcript_path" 2>/dev/null | \
+ tail -1 | \
+ jq -r '.message.usage.input_tokens // 0' 2>/dev/null)
+
+ if [[ -n "$last_input" && "$last_input" =~ ^[0-9]+$ && "$last_input" -gt 0 ]]; then
+ # Add cache tokens for complete picture
+ cache_read=$(grep '"type":"assistant"' "$transcript_path" 2>/dev/null | \
+ tail -1 | \
+ jq -r '.message.usage.cache_read_input_tokens // 0' 2>/dev/null)
+ [[ ! "$cache_read" =~ ^[0-9]+$ ]] && cache_read=0
+
+ used_tokens=$((last_input + cache_read))
+ else
+ # Fallback: cap at 90% if we can't determine actual usage
+ used_tokens=$(( (total_tokens * 90) / 100 ))
+ fi
+ else
+ used_tokens=$(( (total_tokens * 90) / 100 ))
+ fi
+ else
+ # Normal state: use cumulative (it's accurate when < limit)
+ used_tokens=$cumulative_total
+ fi
else
- # Fallback: Parse transcript for older Claude Code versions
+ # Fallback for older Claude Code versions (no native data)
transcript_path=$(echo "$input" | jq -r '.transcript_path // ""')
- total_tokens=200000 # Default context limit
if [[ -f "$transcript_path" && -s "$transcript_path" ]]; then
- # Parse JSONL transcript - get the LAST assistant message's token usage
last_usage=$(grep '"type":"assistant"' "$transcript_path" 2>/dev/null | \
tail -1 | \
jq -r '.message.usage // null' 2>/dev/null)
@@ -123,10 +155,7 @@ else
used_tokens=0
fi
- # Ensure we have a valid number
- if [[ ! "$used_tokens" =~ ^[0-9]+$ ]]; then
- used_tokens=0
- fi
+ [[ ! "$used_tokens" =~ ^[0-9]+$ ]] && used_tokens=0
else
used_tokens=0
fi
@@ -171,7 +200,14 @@ for ((i=0; i<free_bricks; i++)); do
brick_line+="\033[2;37m□\033[0m"
done
-brick_line+="] \033[1m${usage_pct}%\033[0m (${used_k}k/${total_k}k)"
+brick_line+="]"
+
+# Add compaction indicator if applicable
+if [[ "$compacted" == "true" ]]; then
+ brick_line+=" \033[1;35m📦\033[0m"
+fi
+
+brick_line+=" \033[1m${usage_pct}%\033[0m (${used_k}k/${total_k}k)"
# Add free space
brick_line+=" | \033[1;32m${free_k}k free\033[0m"