📊

ツール利用毎のコンテキストの消費量をステータスラインで表示する

に公開

Claude Code用のカスタムステータスラインを作りました。

/ProjectName/app (feature/branch) +120|-45 ◑ 12%-3% Bash(2),Read
表示 内容
/ProjectName/app プロジェクトルートからの相対パス
(feature/branch) Gitブランチ
+120|-45 未コミットの追加/削除行数
◑ コンテキスト残量(◔:45%+ / ◑:20-44% / ◕:20%未満)
12% auto-compactまでの残り
-3% 前回ツール呼び出しからの変化
Bash(2),Read 使用ツールと回数

インストール

jq が必要です。

~/.claude/settings.json に追加:

{
  "statusLine": {
    "type": "command",
    "command": "bash ~/.claude/statusline-command.sh"
  }
}

Claude Codeを再起動すれば反映されます。

シェルスクリプト

statusline-command.sh
#!/bin/bash

input=$(cat)
session_id=$(echo "$input" | jq -r '.session_id')
state_file="/tmp/claude-context-${session_id}.state"

# Get project directory from input JSON
project_dir=$(echo "$input" | jq -r '.workspace.project_dir // empty')
current_dir=$(pwd)

# Calculate relative path from project root
git_root=$(git rev-parse --show-toplevel 2>/dev/null)
if [ -n "$git_root" ]; then
    project_name=$(basename "$git_root")
    if [ "$current_dir" = "$git_root" ]; then
        rel_path="$project_name"
    else
        rel_path="${project_name}/${current_dir#$git_root/}"
    fi
elif [ -n "$project_dir" ]; then
    # Not a git repo, but we have project_dir from Claude Code
    project_name=$(basename "$project_dir")
    if [ "$current_dir" = "$project_dir" ]; then
        rel_path="$project_name"
    else
        rel_path="${project_name}/${current_dir#$project_dir/}"
    fi
else
    rel_path=$(basename "$(pwd)")
fi

# Git branch and diff stats
git_info=""
diff_info=""
if [ -n "$git_root" ]; then
    branch=$(git -c core.useBuiltinFSMonitor=false branch --show-current 2>/dev/null)
    if [ -n "$branch" ]; then
        git_info=$(printf ' \033[33m(%s)\033[0m' "$branch")
    fi

    stats=$(git -c core.useBuiltinFSMonitor=false diff --numstat 2>/dev/null | awk '{a+=$1;d+=$2} END{if(a+d>0) printf "%d %d",a,d}')
    if [ -n "$stats" ]; then
        read -r add del <<< "$stats"
        diff_info=$(printf ' \033[32m+%s\033[0m|\033[31m-%s\033[0m' "$add" "$del")
    fi
fi

# Context window usage and change tracking
context_info=""
usage=$(echo "$input" | jq -r '.context_window.current_usage // empty')

if [ -n "$usage" ]; then
    input_tokens=$(echo "$input" | jq '.context_window.current_usage.input_tokens // 0')
    cache_creation=$(echo "$input" | jq '.context_window.current_usage.cache_creation_input_tokens // 0')
    cache_read=$(echo "$input" | jq '.context_window.current_usage.cache_read_input_tokens // 0')
    cur=$((input_tokens + cache_creation + cache_read))
    size=$(echo "$input" | jq '.context_window.context_window_size')

    if [ "$size" -gt 0 ] 2>/dev/null; then
        used_pct=$((cur * 100 / size))
        remaining=$((100 - used_pct))
        # Auto-compact buffer is ~22.5% of context window
        auto_compact_buffer=22
        until_compact=$((remaining - auto_compact_buffer))
        if [ "$until_compact" -lt 0 ]; then until_compact=0; fi

        # Read previous state (format: prev_usage:last_change:last_tools)
        prev_usage=""
        last_change=""
        last_tools=""
        if [ -f "$state_file" ]; then
            IFS=: read -r prev_usage last_change last_tools < "$state_file"
        fi

        # Select circle symbol based on until_compact capacity
        if [ "$until_compact" -ge 45 ]; then
            circle="◔"
        elif [ "$until_compact" -ge 20 ]; then
            circle="◑"
        else
            circle="◕"
        fi

        # Calculate change from previous state and extract tools
        change=""
        tools=""

        if [ -n "$prev_usage" ] && [ "$prev_usage" -gt 0 ] 2>/dev/null; then
            if [ "$cur" -ne "$prev_usage" ]; then
                # Usage changed - calculate new change and extract tools
                prev_remaining=$((100 - prev_usage * 100 / size))
                change=$((remaining - prev_remaining))

                # Extract tool names from the last API call (same requestId)
                transcript_path=$(echo "$input" | jq -r '.transcript_path // empty')
                if [ -n "$transcript_path" ] && [ -f "$transcript_path" ]; then
                    # Get the last requestId that has tool_use, then extract all tools with that requestId
                    last_req_id=$(tac "$transcript_path" | grep -m1 '"type":"tool_use"' | jq -r '.requestId // empty' 2>/dev/null)
                    if [ -n "$last_req_id" ]; then
                        tools=$(grep "\"requestId\":\"$last_req_id\"" "$transcript_path" | jq -r '
                            .message.content[]? |
                            select(.type == "tool_use") |
                            .name
                        ' 2>/dev/null | sort | uniq -c | awk '{if($1>1) printf "%s(%d),",$2,$1; else printf "%s,",$2}' | sed 's/,$//')
                    fi
                fi

                # Save state when usage changes
                echo "${cur}:${change}:${tools}" > "$state_file"
            else
                # Usage unchanged - keep previous display until next change
                change="$last_change"
                tools="$last_tools"
            fi
        else
            # First call - just save current usage, no change to display
            echo "${cur}::" > "$state_file"
        fi

        # Format tool info
        tool_info=""
        if [ -n "$tools" ]; then
            tool_info=$(printf ' \033[90m%s\033[0m' "$tools")
        fi

        # Format context info with color-coded change (showing until_compact)
        if [ -z "$change" ]; then
            context_info=$(printf ' %s %d%%%s' "$circle" "$until_compact" "$tool_info")
        elif [ "$change" -eq 0 ] 2>/dev/null; then
            # Show -0% for zero change (cyan color)
            context_info=$(printf ' %s %d%%\033[36m-0%%\033[0m%s' "$circle" "$until_compact" "$tool_info")
        elif [ "$change" -gt 0 ] 2>/dev/null; then
            context_info=$(printf ' %s %d%%\033[36m%+d%%\033[0m%s' "$circle" "$until_compact" "$change" "$tool_info")
        else
            context_info=$(printf ' %s %d%%\033[35m%+d%%\033[0m%s' "$circle" "$until_compact" "$change" "$tool_info")
        fi
    fi
fi

printf '\033[01;34m/%s\033[0m%s%s%s' "$rel_path" "$git_info" "$diff_info" "$context_info"

おわりに

自分用だったので他にも表示されていますが、Claude Codeに尋ねてコンテキスト表示部だけ利用するなどカスタマイズしてください。

Discussion