Claude Code ステータスラインを v2.1.80 の新フィールドでアップデートした

概要

Claude Code v2.1.80 で statusline スクリプトに渡される JSON に新しいフィールドが追加された。これを機にステータスラインの表示を見直して、コスト表示の正確性向上とレート制限情報の追加を行った。

v2.1.80 で追加された主なフィールド

rate_limits(claude.ai Pro/Max のみ)

  • rate_limits.five_hour.used_percentage — 5時間ローリングウィンドウの使用率 (0-100)
  • rate_limits.five_hour.resets_at — リセット時刻(Unix epoch)
  • rate_limits.seven_day.used_percentage — 7日ウィンドウの使用率
  • rate_limits.seven_day.resets_at — リセット時刻

cost(正確なコスト情報)

  • cost.total_cost_usd — セッションの実コスト(USD、キャッシュ割引反映済み)
  • cost.total_duration_ms — セッション経過時間
  • cost.total_api_duration_ms — API待ち時間
  • cost.total_lines_added / total_lines_removed — 追加/削除行数

その他

  • workspace.project_dir / workspace.current_dir
  • session_id, transcript_path, version
  • output_style.name
  • worktree.*(worktree 使用時)

ステータスラインのアップデート

Before

Opus 4.6  ctx [#####---------------] 25%  cost $0.5400

コストは input/output トークン数から Opus 4 の料金($15/M input, $75/M output)で自前計算していた。キャッシュ割引は考慮されず、概算値だった。レート制限情報は表示なし。

#!/usr/bin/env bash

input=$(cat)

model=$(echo "$input" | jq -r '.model.display_name // "Unknown"')
used_pct=$(echo "$input" | jq -r '.context_window.used_percentage // empty')
total_input=$(echo "$input" | jq -r '.context_window.total_input_tokens // 0')
total_output=$(echo "$input" | jq -r '.context_window.total_output_tokens // 0')

# Build progress bar (20 chars wide)
if [ -n "$used_pct" ]; then
  filled=$(echo "$used_pct" | awk '{printf "%d", ($1 * 20 / 100) + 0.5}')
  empty=$((20 - filled))
  bar=""
  for i in $(seq 1 $filled); do bar="${bar}#"; done
  for i in $(seq 1 $empty); do bar="${bar}-"; done
  context_str="[${bar}] ${used_pct}%"
else
  context_str="[--------------------] --%"
fi

# Estimate cost using Claude Opus 4 pricing
cost=$(awk -v i="$total_input" -v o="$total_output" 'BEGIN {
  cost = (i / 1000000 * 15) + (o / 1000000 * 75)
  printf "$%.4f", cost
}')

printf "%s  ctx %s  cost %s" "$model" "$context_str" "$cost"

After

Opus 4.6  ctx [#####---------------] 25%  cost $0.5400  5h: 12%  7d: 3%

変更点:

  • コスト: トークンからの自前計算 → cost.total_cost_usd に変更。キャッシュ割引も反映された正確な値になった
  • レート制限: rate_limits から 5時間 / 7日の使用率を末尾に追加。Pro/Max 以外の環境ではフィールドが存在しないため自動的に非表示になる
#!/usr/bin/env bash

input=$(cat)

model=$(echo "$input" | jq -r '.model.display_name // "Unknown"')
used_pct=$(echo "$input" | jq -r '.context_window.used_percentage // empty')
cost=$(echo "$input" | jq -r '.cost.total_cost_usd // 0')

# Build progress bar (20 chars wide)
if [ -n "$used_pct" ]; then
  filled=$(echo "$used_pct" | awk '{printf "%d", ($1 * 20 / 100) + 0.5}')
  empty=$((20 - filled))
  bar=""
  for i in $(seq 1 $filled); do bar="${bar}#"; done
  for i in $(seq 1 $empty); do bar="${bar}-"; done
  context_str="[${bar}] ${used_pct}%"
else
  context_str="[--------------------] --%"
fi

# Format cost
cost_str=$(awk -v c="$cost" 'BEGIN { printf "$%.4f", c }')

# Rate limits (claude.ai Pro/Max only)
rate_str=""
five_h=$(echo "$input" | jq -r '.rate_limits.five_hour.used_percentage // empty')
seven_d=$(echo "$input" | jq -r '.rate_limits.seven_day.used_percentage // empty')
if [ -n "$five_h" ] && [ -n "$seven_d" ]; then
  rate_str=$(printf "  5h: %s%%  7d: %s%%" "$five_h" "$seven_d")
fi

printf "%s  ctx %s  cost %s%s" "$model" "$context_str" "$cost_str" "$rate_str"