Skip to content

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

feat(contextbricks): v2.0.0 - Native context_window support

Jeza3bbfc56a822
diff --git a/tools/statusline-npm/README.md b/tools/statusline-npm/README.md
index f822ae2b..20c3bb32 100644
--- a/tools/statusline-npm/README.md
+++ b/tools/statusline-npm/README.md
@@ -11,11 +11,13 @@ A custom status line for Claude Code that displays accurate token usage with a b
## ✨ Features
-- 🎯 **Real-time context tracking** from Claude Code transcript files
-- 🧱 **Brick visualization** showing token breakdown by category
+- 🎯 **Native context_window support** (Claude Code 2.0.65+) - accurate token data
+- 🔄 **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 *↑↓
+- ⏱️ **Session duration**: track how long you've been working
+- 💰 **Session cost**: API users see spending (hidden for Max subscribers)
- 📊 **Session metrics**: model name, lines changed, free space
-- 🎨 **Model-aware**: Automatic context limits (200k for all models in Claude Code)
- ⚡ **Zero config**: Auto-detects everything, just install and go
## 🚀 Quick Start
@@ -61,17 +63,18 @@ cd claude-skills/tools/statusline
[Sonnet 4.5] claude-skills:main [5f2ce67] Remove auth-js skill | jezweb/claude-skills *↑2 | +145/-23
```
-**Line 2: Context Bricks + Breakdown**
+**Line 2: Context Bricks + Duration + Cost**
```
-ctx [■■■■■■■■■■■■■■■■■□□□□□□□□□□□□□□□□□□□□□□□] 43% (86k/200k tokens) | sys:4k tools:16k mcp:2k mem:10k msg:54k | 113k free
+ctx [■■■■■■■■■■■■■■■■■□□□□□□□□□□□□□□□□□□□□□□□] 43% (86k/200k) | 113k free | 12m45s | $0.87
+```
+
+For Max subscribers (no API cost):
+```
+ctx [■■■■■■■■■■■■■■■■■□□□□□□□□□□□□□□□□□□□□□□□] 43% (86k/200k) | 113k free | 12m45s
```
**Color Legend:**
-- 🔲 Dim white = System prompt
-- 🟨 Yellow = Tools
-- 🟪 Magenta = MCP servers
-- 🟦 Blue = Memory/files
-- 🟦 Cyan = Messages
+- 🟦 Cyan = Used context
- ⬜ Dim hollow = Free space
## 🔧 Requirements
@@ -97,15 +100,25 @@ contextbricks --version # Show version
## 🎯 How It Works
-ContextBricks reads Claude Code's session transcript files (JSONL format) to get **real token usage** - the same data that the `/context` command shows.
+### Claude Code 2.0.65+ (Native)
+
+ContextBricks v2.0 uses the **native `context_window` data** provided by Claude Code:
+
+```json
+{
+ "context_window": {
+ "total_input_tokens": 15234,
+ "total_output_tokens": 4521,
+ "context_window_size": 200000
+ }
+}
+```
+
+This gives you accurate, real-time token counts without any parsing overhead.
-Each assistant message includes cumulative token counts:
-- `input_tokens` - New input
-- `cache_read_input_tokens` - Cached tokens reused
-- `cache_creation_input_tokens` - New cache entries
-- `output_tokens` - Claude's response
+### Older Versions (Fallback)
-The brick visualization shows proportional usage across categories, and git info is queried from your workspace.
+For Claude Code < 2.0.65, ContextBricks falls back to parsing session transcript files (JSONL format) to get token usage.
## 🔄 Updates
@@ -117,19 +130,32 @@ npm update -g contextbricks
npx contextbricks@latest init
```
+## 📋 Changelog
+
+### 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`)
+- **Added session cost** - Shows API cost (only if > $0, hidden for Max subscribers)
+- **Simplified visualization** - Single cyan colour for used context
+- **Removed breakdown estimates** - No more estimated sys/tools/mcp/mem/msg
+- **Backwards compatible** - Falls back to transcript parsing for older versions
+
+### v1.0.x
+- Initial release with transcript parsing and multi-colour breakdown
+
## 🐛 Troubleshooting
### Status line shows 0% (0k/200k tokens)
-**Cause**: jq not installed or transcript file not found
+**Cause**: jq not installed or no context data available (new session)
**Fix**:
```bash
# Check jq
which jq || sudo apt install jq
-# Test manually
-contextbricks install
+# Test with native data
+echo '{"context_window":{"total_input_tokens":50000,"total_output_tokens":5000,"context_window_size":200000},"model":{"display_name":"Sonnet 4.5"},"workspace":{"current_dir":"'"$PWD"'"},"cost":{"total_duration_ms":300000}}' | ~/.claude/statusline.sh
```
### Git info not showing
diff --git a/tools/statusline-npm/assets/context-bricks.png b/tools/statusline-npm/assets/context-bricks.png
index 92ab5d67..a5908715 100644
Binary files a/tools/statusline-npm/assets/context-bricks.png and b/tools/statusline-npm/assets/context-bricks.png differ
diff --git a/tools/statusline-npm/package.json b/tools/statusline-npm/package.json
index bca485bc..945071cc 100644
--- a/tools/statusline-npm/package.json
+++ b/tools/statusline-npm/package.json
@@ -1,6 +1,6 @@
{
"name": "contextbricks",
- "version": "1.0.4",
+ "version": "2.0.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 9534a2c3..ddf716ea 100755
--- a/tools/statusline-npm/scripts/statusline.sh
+++ b/tools/statusline-npm/scripts/statusline.sh
@@ -1,8 +1,9 @@
#!/bin/bash
# Claude Code Custom Status Line with Context Brick Visualization
+# v2.0 - Uses native context_window data from Claude Code 2.0.65+
# Shows: Model | Repo:Branch [commit] message | GitHub | git status | lines changed
-# Context usage as horizontal brick visualization (like /context)
+# Context usage as horizontal brick visualization
# Read JSON from stdin
input=$(cat)
@@ -92,131 +93,77 @@ if [[ "$lines_added" -gt 0 || "$lines_removed" -gt 0 ]]; then
fi
# Build Line 2: Context Brick Visualization
-# Parse REAL context data from transcript file
-
-# Extract transcript path from JSON
-transcript_path=$(echo "$input" | jq -r '.transcript_path // ""')
-
-# Get model ID for context limit detection
-model_id=$(echo "$input" | jq -r '.model.id // ""')
-
-# Determine context limit based on model
-# NOTE: While Opus 4/4.5 models have 1M context at API level, Claude Code CLI
-# enforces a 200k limit for all models. Using 200k for accurate display.
-case "$model_id" in
- *"opus-4"*) total_tokens=200000 ;; # Claude Code limits to 200k
- *"sonnet-4"*|*"sonnet-3-7"*) total_tokens=200000 ;;
- *"haiku"*) total_tokens=200000 ;;
- *) total_tokens=200000 ;; # Default
-esac
-
-# Parse transcript for token usage (if available)
-if [[ -f "$transcript_path" && -s "$transcript_path" ]]; then
- # Parse JSONL transcript - get the LAST assistant message's token usage
- # (Each message contains cumulative context, so we only need the most recent)
- last_usage=$(grep '"type":"assistant"' "$transcript_path" 2>/dev/null | \
- tail -1 | \
- jq -r '.message.usage // null' 2>/dev/null)
-
- if [[ -n "$last_usage" && "$last_usage" != "null" ]]; then
- # Sum all token types from the last message
- used_tokens=$(echo "$last_usage" | jq '
- (.input_tokens // 0) +
- (.cache_read_input_tokens // 0) +
- (.cache_creation_input_tokens // 0) +
- (.output_tokens // 0)
- ')
+# 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))
+else
+ # Fallback: Parse transcript for older Claude Code versions
+ 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)
+
+ if [[ -n "$last_usage" && "$last_usage" != "null" ]]; then
+ used_tokens=$(echo "$last_usage" | jq '
+ (.input_tokens // 0) +
+ (.cache_read_input_tokens // 0) +
+ (.cache_creation_input_tokens // 0) +
+ (.output_tokens // 0)
+ ')
+ else
+ used_tokens=0
+ fi
+
+ # Ensure we have a valid number
+ if [[ ! "$used_tokens" =~ ^[0-9]+$ ]]; then
+ used_tokens=0
+ fi
else
used_tokens=0
fi
-
- # Ensure we have a valid number
- if [[ ! "$used_tokens" =~ ^[0-9]+$ ]]; then
- used_tokens=0
- fi
-else
- # No transcript available (new session)
- used_tokens=0
-fi
-
-# Estimate breakdown (rough approximation based on typical usage)
-# In future: could parse tool definitions, CLAUDE.md, etc. for accuracy
-system_tokens=$((total_tokens * 2 / 100)) # ~2% system prompt
-tools_tokens=$((total_tokens * 8 / 100)) # ~8% tools
-mcp_tokens=$((total_tokens * 1 / 100)) # ~1% MCP
-memory_tokens=$((total_tokens * 5 / 100)) # ~5% memory/files
-message_tokens=$((used_tokens - system_tokens - tools_tokens - mcp_tokens - memory_tokens))
-if [[ $message_tokens -lt 0 ]]; then
- message_tokens=0
fi
+# Calculate metrics
free_tokens=$((total_tokens - used_tokens))
-
-usage_pct=$(( (used_tokens * 100) / total_tokens ))
+if [[ $total_tokens -gt 0 ]]; then
+ usage_pct=$(( (used_tokens * 100) / total_tokens ))
+else
+ usage_pct=0
+fi
# Convert to 'k' format for display
used_k=$(( used_tokens / 1000 ))
total_k=$(( total_tokens / 1000 ))
free_k=$(( free_tokens / 1000 ))
-system_k=$(( system_tokens / 1000 ))
-tools_k=$(( tools_tokens / 1000 ))
-mcp_k=$(( mcp_tokens / 1000 ))
-memory_k=$(( memory_tokens / 1000 ))
-message_k=$(( message_tokens / 1000 ))
-# Generate brick visualization (40 bricks total for more granularity)
+# Get session duration (convert ms to HHh MMm format)
+duration_ms=$(echo "$input" | jq -r '.cost.total_duration_ms // 0')
+duration_hours=$((duration_ms / 3600000))
+duration_min=$(((duration_ms % 3600000) / 60000))
+
+# Get session cost (only show if > 0, for API users)
+cost_usd=$(echo "$input" | jq -r '.cost.total_cost_usd // 0')
+
+# Generate brick visualization (40 bricks total)
total_bricks=40
used_bricks=$(( (used_tokens * total_bricks) / total_tokens ))
free_bricks=$((total_bricks - used_bricks))
-# Color codes for different types (matching /context)
-# System: dim (gray), Tools: yellow, MCP: magenta, Memory: blue, Messages: cyan, Free: dim white
+# Build brick line with single colour (cyan for used, dim white for free)
brick_line="ctx ["
-# Calculate bricks per category (proportional to actual usage)
-system_bricks=$(( (system_tokens * total_bricks) / total_tokens ))
-tools_bricks=$(( (tools_tokens * total_bricks) / total_tokens ))
-mcp_bricks=$(( (mcp_tokens * total_bricks) / total_tokens ))
-memory_bricks=$(( (memory_tokens * total_bricks) / total_tokens ))
-message_bricks=$(( (message_tokens * total_bricks) / total_tokens ))
-
-# Draw bricks by category
-brick_count=0
-
-# System bricks (dim white)
-for ((i=0; i<system_bricks && brick_count<used_bricks; i++)); do
- brick_line+="\033[2;37m■\033[0m"
- ((brick_count++))
-done
-
-# Tools bricks (yellow)
-for ((i=0; i<tools_bricks && brick_count<used_bricks; i++)); do
- brick_line+="\033[0;33m■\033[0m"
- ((brick_count++))
-done
-
-# MCP bricks (magenta/pink)
-for ((i=0; i<mcp_bricks && brick_count<used_bricks; i++)); do
- brick_line+="\033[0;35m■\033[0m"
- ((brick_count++))
-done
-
-# Memory bricks (blue)
-for ((i=0; i<memory_bricks && brick_count<used_bricks; i++)); do
- brick_line+="\033[0;34m■\033[0m"
- ((brick_count++))
-done
-
-# Message bricks (cyan)
-for ((i=0; i<message_bricks && brick_count<used_bricks; i++)); do
+# Used bricks (cyan)
+for ((i=0; i<used_bricks; i++)); do
brick_line+="\033[0;36m■\033[0m"
- ((brick_count++))
-done
-
-# Fill any remaining used bricks (in case of rounding)
-while ((brick_count < used_bricks)); do
- brick_line+="\033[0;36m■\033[0m"
- ((brick_count++))
done
# Free bricks (dim/gray hollow squares)
@@ -224,16 +171,27 @@ 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 tokens)"
+brick_line+="] \033[1m${usage_pct}%\033[0m (${used_k}k/${total_k}k)"
-# Add breakdown (simplified)
-brick_line+=" | \033[2;37msys:${system_k}k\033[0m"
-brick_line+=" \033[0;33mtools:${tools_k}k\033[0m"
-brick_line+=" \033[0;35mmcp:${mcp_k}k\033[0m"
-brick_line+=" \033[0;34mmem:${memory_k}k\033[0m"
-brick_line+=" \033[0;36mmsg:${message_k}k\033[0m"
+# Add free space
brick_line+=" | \033[1;32m${free_k}k free\033[0m"
+# Add duration (HHh MMm format)
+brick_line+=" | ${duration_hours}h ${duration_min}m"
+
+# Add cost only if non-zero, rounded to 2 decimal places
+if command -v bc &> /dev/null; then
+ if (( $(echo "$cost_usd > 0" | bc -l 2>/dev/null || echo "0") )); then
+ cost_formatted=$(printf "%.2f" "$cost_usd" 2>/dev/null || echo "0.00")
+ brick_line+=" | \033[0;33m\$${cost_formatted}\033[0m"
+ fi
+else
+ # Fallback without bc: simple string comparison
+ if [[ "$cost_usd" != "0" && "$cost_usd" != "0.0" && "$cost_usd" != "0.00" && -n "$cost_usd" ]]; then
+ brick_line+=" | \033[0;33m\$${cost_usd}\033[0m"
+ fi
+fi
+
# Output both lines
echo -e "$line1"
echo -e "$brick_line"
diff --git a/tools/statusline/README.md b/tools/statusline/README.md
index 626a7d2f..21ed6915 100644
--- a/tools/statusline/README.md
+++ b/tools/statusline/README.md
@@ -7,10 +7,11 @@ A custom status line for Claude Code CLI that displays real-time context trackin
## ✨ Features
### Real-Time Context Tracking
-- **Accurate token usage** from Claude Code's transcript files
-- **Brick visualization** showing context breakdown by category
-- **Model-aware limits** (200k for all models in Claude Code)
-- **Token breakdown**: System, Tools, MCP, Memory, Messages
+- **Native context_window support** (Claude Code 2.0.65+) - accurate token data direct from Claude Code
+- **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
+- **Session cost** - API users see spending (hidden for Max subscribers)
### Git Integration
- Current **repo name and branch**
@@ -25,6 +26,8 @@ A custom status line for Claude Code CLI that displays real-time context trackin
- **Model name** (Sonnet 4.5, Opus 4, Haiku, etc.)
- **Lines changed** this session (+added/-removed)
- **Free space** remaining in context
+- **Duration** - time spent in session
+- **Cost** - API usage cost (only shown if > $0)
## 📦 Installation
@@ -78,17 +81,18 @@ The installer will:
[Sonnet 4.5] claude-skills:main [5f2ce67] Remove auth-js skill | jezweb/claude-skills *↑2 | +145/-23
```
-### Line 2: Context Bricks + Breakdown
+### Line 2: Context Bricks + Duration + Cost
```
-ctx [■■■■■■■■■■■■■■■■■□□□□□□□□□□□□□□□□□□□□□□□] 43% (86k/200k tokens) | sys:4k tools:16k mcp:2k mem:10k msg:54k | 113k free
+ctx [■■■■■■■■■■■■■■■■■□□□□□□□□□□□□□□□□□□□□□□□] 43% (86k/200k) | 113k free | 12m45s | $0.87
+```
+
+For Max subscribers (no API cost):
+```
+ctx [■■■■■■■■■■■■■■■■■□□□□□□□□□□□□□□□□□□□□□□□] 43% (86k/200k) | 113k free | 12m45s
```
**Color Legend:**
-- 🔲 Dim white = System prompt
-- 🟨 Yellow = Tools
-- 🟪 Magenta = MCP servers
-- 🟦 Blue = Memory/files
-- 🟦 Cyan = Messages
+- 🟦 Cyan = Used context
- ⬜ Dim hollow = Free space
## 🔧 Requirements
@@ -106,34 +110,33 @@ ctx [■■■■■■■■■■■■■■■■■□□□□□□□□
## 📖 How It Works
-### Context Tracking
-
-Claude Code stores session transcripts in JSONL format at:
-```
-~/.claude/projects/{project-dir}/{session-id}.jsonl
-```
+### Context Tracking (v2.0+)
-Each assistant message includes cumulative token usage:
+**Claude Code 2.0.65+** provides native context data via JSON stdin:
```json
{
- "type": "assistant",
- "message": {
- "usage": {
- "input_tokens": 3,
- "cache_read_input_tokens": 93279,
- "cache_creation_input_tokens": 2822,
- "output_tokens": 467
- }
+ "context_window": {
+ "total_input_tokens": 15234,
+ "total_output_tokens": 4521,
+ "context_window_size": 200000
}
}
```
The status line:
-1. Receives `transcript_path` from Claude Code via JSON stdin
-2. Parses the **last assistant message** (cumulative counts)
-3. Sums all token types
-4. Calculates percentage against model's context limit
-5. Generates brick visualization proportional to usage
+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
+
+### Fallback for Older Versions
+
+For Claude Code < 2.0.65, the script parses session transcripts:
+```
+~/.claude/projects/{project-dir}/{session-id}.jsonl
+```
+
+Each assistant message includes cumulative token usage which is parsed as a fallback.
### Git Information
@@ -149,18 +152,15 @@ Queries git commands in the current workspace:
### Status line shows 0% (0k/200k tokens)
-**Cause**: Transcript file not found or parsing failed
+**Cause**: No context data available (new session or Claude Code < 2.0.65 without transcript)
**Debug**:
```bash
# Check if jq is installed
which jq
-# Test the script manually
-echo '{"transcript_path":"~/.claude/projects/test.jsonl","model":{"id":"claude-sonnet-4-5"}}' | ~/.claude/statusline.sh
-
-# Check Claude Code logs
-tail -f ~/.claude/logs/claude-code.log
+# Test with native context_window data (2.0.65+)
+echo '{"context_window":{"total_input_tokens":50000,"total_output_tokens":5000,"context_window_size":200000},"model":{"display_name":"Sonnet 4.5"},"workspace":{"current_dir":"'"$PWD"'"},"cost":{"total_duration_ms":300000}}' | ~/.claude/statusline.sh
```
### Brick visualization looks wrong
@@ -185,35 +185,18 @@ git rev-parse --git-dir
sudo apt install git
```
-### Script runs slowly
-
-**Cause**: Large transcript files (>10MB)
-
-**Optimization**: The script uses `grep | tail -1` which is fast for most cases. For very large files, consider:
-- Clearing old sessions: `rm ~/.claude/projects/*/old-session-*.jsonl`
-- Using `tail -1000` to limit parsing
-
## 🎛️ Customization
### Change Brick Count
-Edit `statusline.sh` line 166:
+Edit `statusline.sh` line ~156:
```bash
total_bricks=40 # Increase for more granular visualization
```
-### Adjust Token Breakdown Estimates
-
-Edit lines 140-147:
-```bash
-system_tokens=$((total_tokens * 2 / 100)) # Adjust percentages
-tools_tokens=$((total_tokens * 8 / 100))
-# ...
-```
-
### Hide Git Status
-Comment out lines 84-87:
+Comment out lines ~85-88:
```bash
# if [[ -n "$git_status" ]]; then
# line1+=" \033[1;31m$git_status\033[0m"
@@ -222,7 +205,7 @@ Comment out lines 84-87:
### Compact Mode (Single Line)
-Comment out line 197:
+Comment out the second echo:
```bash
echo -e "$line1"
# echo -e "$brick_line" # Disable context line
@@ -246,6 +229,19 @@ rm ~/.claude/statusline.sh
# Edit ~/.claude/settings.json and remove the "statusLine" section
```
+## 📋 Changelog
+
+### 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`)
+- **Added session cost** - Shows API cost (only if > $0, hidden for Max subscribers)
+- **Simplified visualization** - Single cyan colour for used context
+- **Removed breakdown estimates** - No more estimated sys/tools/mcp/mem/msg
+- **Backwards compatible** - Falls back to transcript parsing for older versions
+
+### v1.0.0
+- Initial release with transcript parsing and multi-colour breakdown
+
## 🤝 Contributing
Found a bug or have a feature request?
diff --git a/tools/statusline/assets/context-bricks.png b/tools/statusline/assets/context-bricks.png
index 92ab5d67..a5908715 100644
Binary files a/tools/statusline/assets/context-bricks.png and b/tools/statusline/assets/context-bricks.png differ
diff --git a/tools/statusline/statusline.sh b/tools/statusline/statusline.sh
index 9534a2c3..ddf716ea 100755
--- a/tools/statusline/statusline.sh
+++ b/tools/statusline/statusline.sh
@@ -1,8 +1,9 @@
#!/bin/bash
# Claude Code Custom Status Line with Context Brick Visualization
+# v2.0 - Uses native context_window data from Claude Code 2.0.65+
# Shows: Model | Repo:Branch [commit] message | GitHub | git status | lines changed
-# Context usage as horizontal brick visualization (like /context)
+# Context usage as horizontal brick visualization
# Read JSON from stdin
input=$(cat)
@@ -92,131 +93,77 @@ if [[ "$lines_added" -gt 0 || "$lines_removed" -gt 0 ]]; then
fi
# Build Line 2: Context Brick Visualization
-# Parse REAL context data from transcript file
-
-# Extract transcript path from JSON
-transcript_path=$(echo "$input" | jq -r '.transcript_path // ""')
-
-# Get model ID for context limit detection
-model_id=$(echo "$input" | jq -r '.model.id // ""')
-
-# Determine context limit based on model
-# NOTE: While Opus 4/4.5 models have 1M context at API level, Claude Code CLI
-# enforces a 200k limit for all models. Using 200k for accurate display.
-case "$model_id" in
- *"opus-4"*) total_tokens=200000 ;; # Claude Code limits to 200k
- *"sonnet-4"*|*"sonnet-3-7"*) total_tokens=200000 ;;
- *"haiku"*) total_tokens=200000 ;;
- *) total_tokens=200000 ;; # Default
-esac
-
-# Parse transcript for token usage (if available)
-if [[ -f "$transcript_path" && -s "$transcript_path" ]]; then
- # Parse JSONL transcript - get the LAST assistant message's token usage
- # (Each message contains cumulative context, so we only need the most recent)
- last_usage=$(grep '"type":"assistant"' "$transcript_path" 2>/dev/null | \
- tail -1 | \
- jq -r '.message.usage // null' 2>/dev/null)
-
- if [[ -n "$last_usage" && "$last_usage" != "null" ]]; then
- # Sum all token types from the last message
- used_tokens=$(echo "$last_usage" | jq '
- (.input_tokens // 0) +
- (.cache_read_input_tokens // 0) +
- (.cache_creation_input_tokens // 0) +
- (.output_tokens // 0)
- ')
+# 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))
+else
+ # Fallback: Parse transcript for older Claude Code versions
+ 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)
+
+ if [[ -n "$last_usage" && "$last_usage" != "null" ]]; then
+ used_tokens=$(echo "$last_usage" | jq '
+ (.input_tokens // 0) +
+ (.cache_read_input_tokens // 0) +
+ (.cache_creation_input_tokens // 0) +
+ (.output_tokens // 0)
+ ')
+ else
+ used_tokens=0
+ fi
+
+ # Ensure we have a valid number
+ if [[ ! "$used_tokens" =~ ^[0-9]+$ ]]; then
+ used_tokens=0
+ fi
else
used_tokens=0
fi
-
- # Ensure we have a valid number
- if [[ ! "$used_tokens" =~ ^[0-9]+$ ]]; then
- used_tokens=0
- fi
-else
- # No transcript available (new session)
- used_tokens=0
-fi
-
-# Estimate breakdown (rough approximation based on typical usage)
-# In future: could parse tool definitions, CLAUDE.md, etc. for accuracy
-system_tokens=$((total_tokens * 2 / 100)) # ~2% system prompt
-tools_tokens=$((total_tokens * 8 / 100)) # ~8% tools
-mcp_tokens=$((total_tokens * 1 / 100)) # ~1% MCP
-memory_tokens=$((total_tokens * 5 / 100)) # ~5% memory/files
-message_tokens=$((used_tokens - system_tokens - tools_tokens - mcp_tokens - memory_tokens))
-if [[ $message_tokens -lt 0 ]]; then
- message_tokens=0
fi
+# Calculate metrics
free_tokens=$((total_tokens - used_tokens))
-
-usage_pct=$(( (used_tokens * 100) / total_tokens ))
+if [[ $total_tokens -gt 0 ]]; then
+ usage_pct=$(( (used_tokens * 100) / total_tokens ))
+else
+ usage_pct=0
+fi
# Convert to 'k' format for display
used_k=$(( used_tokens / 1000 ))
total_k=$(( total_tokens / 1000 ))
free_k=$(( free_tokens / 1000 ))
-system_k=$(( system_tokens / 1000 ))
-tools_k=$(( tools_tokens / 1000 ))
-mcp_k=$(( mcp_tokens / 1000 ))
-memory_k=$(( memory_tokens / 1000 ))
-message_k=$(( message_tokens / 1000 ))
-# Generate brick visualization (40 bricks total for more granularity)
+# Get session duration (convert ms to HHh MMm format)
+duration_ms=$(echo "$input" | jq -r '.cost.total_duration_ms // 0')
+duration_hours=$((duration_ms / 3600000))
+duration_min=$(((duration_ms % 3600000) / 60000))
+
+# Get session cost (only show if > 0, for API users)
+cost_usd=$(echo "$input" | jq -r '.cost.total_cost_usd // 0')
+
+# Generate brick visualization (40 bricks total)
total_bricks=40
used_bricks=$(( (used_tokens * total_bricks) / total_tokens ))
free_bricks=$((total_bricks - used_bricks))
-# Color codes for different types (matching /context)
-# System: dim (gray), Tools: yellow, MCP: magenta, Memory: blue, Messages: cyan, Free: dim white
+# Build brick line with single colour (cyan for used, dim white for free)
brick_line="ctx ["
-# Calculate bricks per category (proportional to actual usage)
-system_bricks=$(( (system_tokens * total_bricks) / total_tokens ))
-tools_bricks=$(( (tools_tokens * total_bricks) / total_tokens ))
-mcp_bricks=$(( (mcp_tokens * total_bricks) / total_tokens ))
-memory_bricks=$(( (memory_tokens * total_bricks) / total_tokens ))
-message_bricks=$(( (message_tokens * total_bricks) / total_tokens ))
-
-# Draw bricks by category
-brick_count=0
-
-# System bricks (dim white)
-for ((i=0; i<system_bricks && brick_count<used_bricks; i++)); do
- brick_line+="\033[2;37m■\033[0m"
- ((brick_count++))
-done
-
-# Tools bricks (yellow)
-for ((i=0; i<tools_bricks && brick_count<used_bricks; i++)); do
- brick_line+="\033[0;33m■\033[0m"
- ((brick_count++))
-done
-
-# MCP bricks (magenta/pink)
-for ((i=0; i<mcp_bricks && brick_count<used_bricks; i++)); do
- brick_line+="\033[0;35m■\033[0m"
- ((brick_count++))
-done
-
-# Memory bricks (blue)
-for ((i=0; i<memory_bricks && brick_count<used_bricks; i++)); do
- brick_line+="\033[0;34m■\033[0m"
- ((brick_count++))
-done
-
-# Message bricks (cyan)
-for ((i=0; i<message_bricks && brick_count<used_bricks; i++)); do
+# Used bricks (cyan)
+for ((i=0; i<used_bricks; i++)); do
brick_line+="\033[0;36m■\033[0m"
- ((brick_count++))
-done
-
-# Fill any remaining used bricks (in case of rounding)
-while ((brick_count < used_bricks)); do
- brick_line+="\033[0;36m■\033[0m"
- ((brick_count++))
done
# Free bricks (dim/gray hollow squares)
@@ -224,16 +171,27 @@ 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 tokens)"
+brick_line+="] \033[1m${usage_pct}%\033[0m (${used_k}k/${total_k}k)"
-# Add breakdown (simplified)
-brick_line+=" | \033[2;37msys:${system_k}k\033[0m"
-brick_line+=" \033[0;33mtools:${tools_k}k\033[0m"
-brick_line+=" \033[0;35mmcp:${mcp_k}k\033[0m"
-brick_line+=" \033[0;34mmem:${memory_k}k\033[0m"
-brick_line+=" \033[0;36mmsg:${message_k}k\033[0m"
+# Add free space
brick_line+=" | \033[1;32m${free_k}k free\033[0m"
+# Add duration (HHh MMm format)
+brick_line+=" | ${duration_hours}h ${duration_min}m"
+
+# Add cost only if non-zero, rounded to 2 decimal places
+if command -v bc &> /dev/null; then
+ if (( $(echo "$cost_usd > 0" | bc -l 2>/dev/null || echo "0") )); then
+ cost_formatted=$(printf "%.2f" "$cost_usd" 2>/dev/null || echo "0.00")
+ brick_line+=" | \033[0;33m\$${cost_formatted}\033[0m"
+ fi
+else
+ # Fallback without bc: simple string comparison
+ if [[ "$cost_usd" != "0" && "$cost_usd" != "0.0" && "$cost_usd" != "0.00" && -n "$cost_usd" ]]; then
+ brick_line+=" | \033[0;33m\$${cost_usd}\033[0m"
+ fi
+fi
+
# Output both lines
echo -e "$line1"
echo -e "$brick_line"