Skip to content

Instantly share code, notes, and snippets.

@ajitid
Last active February 2, 2026 20:26
Show Gist options
  • Select an option

  • Save ajitid/4bd498187b66c61acbcd44fbabfa903b to your computer and use it in GitHub Desktop.

Select an option

Save ajitid/4bd498187b66c61acbcd44fbabfa903b to your computer and use it in GitHub Desktop.
claude code config
{
"model": "sonnet",
"statusLine": {
"type": "command",
"command": "~/.claude/statusline.sh",
"padding": 0
},
"enabledPlugins": {
"rust-analyzer-lsp@claude-plugins-official": true
}
}
#!/bin/bash
# Status line script for Claude Code - shows cumulative token usage + usage limits
input=$(cat)
SESSION_ID=$(echo "$input" | jq -r '.session_id')
MODEL=$(echo "$input" | jq -r '.model.display_name')
COST=$(echo "$input" | jq -r '.cost.total_cost_usd // 0' | xargs printf "%.2f")
# Context window usage (current branch only)
CONTEXT_SIZE=$(echo "$input" | jq -r '.context_window.context_window_size')
CURRENT_USAGE=$(echo "$input" | jq '.context_window.current_usage')
if [ "$CURRENT_USAGE" != "null" ]; then
# Use current_usage for actual context window usage
CONTEXT_USED=$(echo "$CURRENT_USAGE" | jq '(.input_tokens // 0) + (.cache_creation_input_tokens // 0) + (.cache_read_input_tokens // 0)')
USED_K=$((CONTEXT_USED / 1000))
else
# Fallback to percentage if current_usage not available
USED_PCT=$(echo "$input" | jq -r '.context_window.used_percentage // 0')
USED_K=$((CONTEXT_SIZE * USED_PCT / 100 / 1000))
fi
SIZE_K=$((CONTEXT_SIZE / 1000))
# Total session tokens (across all branches) - for cost tracking
SESSION_INPUT=$(echo "$input" | jq -r '.context_window.total_input_tokens')
SESSION_OUTPUT=$(echo "$input" | jq -r '.context_window.total_output_tokens')
SESSION_TOTAL=$((SESSION_INPUT + SESSION_OUTPUT))
SESSION_TOTAL_K=$((SESSION_TOTAL / 1000))
# Cache metrics
CACHE_READ=$(echo "$input" | jq -r '.context_window.current_usage.cache_read_input_tokens // 0')
# Fetch usage limits from Anthropic API
USAGE_INFO=""
TOKEN=$(security find-generic-password -s "Claude Code-credentials" -w 2>/dev/null | jq -r '.claudeAiOauth.accessToken // empty')
if [ -n "$TOKEN" ]; then
USAGE_DATA=$(curl -s -H "Authorization: Bearer $TOKEN" \
-H "anthropic-beta: oauth-2025-04-20" \
-H "Accept: application/json" \
"https://api.anthropic.com/api/oauth/usage" 2>/dev/null)
if [ $? -eq 0 ] && [ -n "$USAGE_DATA" ]; then
FIVE_HOUR=$(echo "$USAGE_DATA" | jq -r '.five_hour.utilization // empty' 2>/dev/null)
SEVEN_DAY=$(echo "$USAGE_DATA" | jq -r '.seven_day.utilization // empty' 2>/dev/null)
FIVE_HOUR_RESET=$(echo "$USAGE_DATA" | jq -r '.five_hour.resets_at // empty' 2>/dev/null)
SEVEN_DAY_RESET=$(echo "$USAGE_DATA" | jq -r '.seven_day.resets_at // empty' 2>/dev/null)
if [ -n "$FIVE_HOUR" ] && [ -n "$SEVEN_DAY" ]; then
# Convert to integer (API returns floats like 41.0)
FIVE_HOUR_INT=$(printf "%.0f" "$FIVE_HOUR" 2>/dev/null)
SEVEN_DAY_INT=$(printf "%.0f" "$SEVEN_DAY" 2>/dev/null)
# Calculate time until reset
NOW=$(date -u +%s)
FIVE_H_TIME=""
SEVEN_D_TIME=""
if [ -n "$FIVE_HOUR_RESET" ]; then
FIVE_RESET_TS=$(date -j -u -f "%Y-%m-%dT%H:%M:%S" "$(echo $FIVE_HOUR_RESET | cut -d+ -f1 | cut -d. -f1)" +%s 2>/dev/null)
if [ -n "$FIVE_RESET_TS" ]; then
DIFF=$((FIVE_RESET_TS - NOW))
HOURS=$((DIFF / 3600))
MINS=$(((DIFF % 3600) / 60))
if [ $HOURS -gt 0 ]; then
FIVE_H_TIME="${HOURS}h${MINS}m"
else
FIVE_H_TIME="${MINS}m"
fi
fi
fi
if [ -n "$SEVEN_DAY_RESET" ]; then
SEVEN_RESET_TS=$(date -j -u -f "%Y-%m-%dT%H:%M:%S" "$(echo $SEVEN_DAY_RESET | cut -d+ -f1 | cut -d. -f1)" +%s 2>/dev/null)
if [ -n "$SEVEN_RESET_TS" ]; then
DIFF=$((SEVEN_RESET_TS - NOW))
DAYS=$((DIFF / 86400))
HOURS=$(((DIFF % 86400) / 3600))
if [ $DAYS -gt 0 ]; then
SEVEN_D_TIME="${DAYS}d${HOURS}h"
else
SEVEN_D_TIME="${HOURS}h"
fi
fi
fi
USAGE_INFO="5h:${FIVE_HOUR_INT}%${FIVE_H_TIME} 7d:${SEVEN_DAY_INT}%${SEVEN_D_TIME}"
fi
fi
fi
# Build status line
# Show session total if significantly different from context usage (indicating branching)
SESSION_INFO=""
if [ $SESSION_TOTAL_K -gt $((USED_K + 50)) ]; then
SESSION_INFO=" (Session:${SESSION_TOTAL_K}K)"
fi
# Order: rate limits | model context | cost cache | session id
PARTS=""
# Rate limits first (most actionable)
if [ -n "$USAGE_INFO" ]; then
PARTS="${USAGE_INFO} | "
fi
# Model + context window
PARTS="${PARTS}${MODEL} ${USED_K}K/${SIZE_K}K${SESSION_INFO} | "
# Cost + cache
if [ "$CACHE_READ" -gt 0 ]; then
CACHE_K=$((CACHE_READ / 1000))
PARTS="${PARTS}\$${COST} Cache ${CACHE_K}K"
else
PARTS="${PARTS}\$${COST}"
fi
# Full session ID
PARTS="${PARTS} | ${SESSION_ID}"
echo "$PARTS"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment