Loading...
Claude Code daily usage quota tracker showing percentage of daily limit consumed with visual progress bar, time remaining, and budget pacing indicators.
#!/usr/bin/env bash
# Daily Usage Percentage Tracker for Claude Code
# Tracks progress toward daily usage limits
# Read JSON from stdin
read -r input
# Extract session cost
session_cost=$(echo "$input" | jq -r '.cost.total_cost_usd // 0')
# Daily usage tracking file (resets at midnight)
USAGE_DIR="${HOME}/.claude-code-usage"
mkdir -p "$USAGE_DIR"
# Get current date for daily reset
current_date=$(date +%Y-%m-%d)
USAGE_FILE="${USAGE_DIR}/daily-${current_date}.usage"
# Initialize usage file if doesn't exist
if [ ! -f "$USAGE_FILE" ]; then
echo "0" > "$USAGE_FILE"
fi
# Read cumulative daily usage
daily_usage=$(cat "$USAGE_FILE")
# Update with current session cost (simple addition)
# Note: This is per-session tracking, may need deduplication for accuracy
updated_usage=$(echo "$daily_usage + $session_cost" | bc)
echo "$updated_usage" > "$USAGE_FILE"
# CONFIGURABLE: Set your daily budget limit (default $10/day)
DAILY_LIMIT=10.00
# Calculate percentage of daily limit used
if (( $(echo "$DAILY_LIMIT > 0" | bc -l) )); then
usage_percentage=$(echo "scale=1; ($updated_usage * 100) / $DAILY_LIMIT" | bc)
else
usage_percentage=0
fi
# Convert to integer for comparisons
usage_percentage_int=$(echo "$usage_percentage / 1" | bc)
# Cap percentage at 100 for display (can exceed in practice)
if [ $usage_percentage_int -gt 100 ]; then
display_percentage=100
OVERFLOW=true
else
display_percentage=$usage_percentage_int
OVERFLOW=false
fi
# Calculate remaining budget
remaining=$(echo "$DAILY_LIMIT - $updated_usage" | bc)
# Color coding based on usage percentage
if [ $usage_percentage_int -lt 50 ]; then
USAGE_COLOR="\033[38;5;46m" # Green: <50% used
USAGE_ICON="✓"
USAGE_STATUS="ON TRACK"
elif [ $usage_percentage_int -lt 80 ]; then
USAGE_COLOR="\033[38;5;226m" # Yellow: 50-80% used
USAGE_ICON="⚠"
USAGE_STATUS="PACING HIGH"
elif [ $usage_percentage_int -lt 100 ]; then
USAGE_COLOR="\033[38;5;208m" # Orange: 80-100% used
USAGE_ICON="⏰"
USAGE_STATUS="NEAR LIMIT"
else
USAGE_COLOR="\033[38;5;196m" # Red: >100% used (over budget)
USAGE_ICON="✗"
USAGE_STATUS="OVER BUDGET"
fi
# Build progress bar (20 characters wide)
bar_filled=$(( display_percentage / 5 )) # Each char = 5%
bar_empty=$(( 20 - bar_filled ))
if [ $bar_filled -gt 0 ]; then
bar=$(printf "█%.0s" $(seq 1 $bar_filled))$(printf "░%.0s" $(seq 1 $bar_empty))
else
bar="░░░░░░░░░░░░░░░░░░░░"
fi
# Calculate hours remaining in day for pacing
current_hour=$(date +%H)
hours_remaining=$((24 - current_hour))
# Pacing indicator (are we on track for 24-hour period?)
expected_percentage=$(( (24 - hours_remaining) * 100 / 24 ))
if [ $usage_percentage_int -gt $expected_percentage ] && [ $hours_remaining -gt 0 ]; then
PACING="📈 ahead of pace"
elif [ $usage_percentage_int -lt $expected_percentage ] && [ $hours_remaining -gt 0 ]; then
PACING="📉 below pace"
else
PACING=""
fi
# Format remaining budget
if (( $(echo "$remaining > 0" | bc -l) )); then
remaining_display="\$${remaining} left"
else
remaining_display="\$0.00 left"
fi
RESET="\033[0m"
# Output statusline
if [ "$OVERFLOW" = true ]; then
echo -e "${USAGE_ICON} Daily: ${USAGE_COLOR}${bar}${RESET} ${usage_percentage}% | ${USAGE_STATUS} | ${remaining_display}"
else
echo -e "${USAGE_ICON} Daily: ${USAGE_COLOR}${bar}${RESET} ${usage_percentage}% | ${remaining_display} ${PACING}"
fi
{
"format": "bash",
"position": "left",
"refreshInterval": 2000
}Usage percentage not updating or stuck at 0%
Verify cost.total_cost_usd field exists: echo '$input' | jq .cost.total_cost_usd. Check usage file exists: cat ~/.claude-code-usage/daily-$(date +%Y-%m-%d).usage. Ensure bc is installed: which bc. Script uses simple addition - may need session deduplication for accuracy across multiple statusline updates.
Daily limit showing OVER BUDGET when under actual budget
Default DAILY_LIMIT is $10.00. Configure your actual limit in script: DAILY_LIMIT=25.00 (for $25/day budget). Verify limit calculation: usage_percentage = (updated_usage * 100) / DAILY_LIMIT. Check usage file value matches expected spending.
Usage not resetting at midnight
Script uses date-based filenames: daily-YYYY-MM-DD.usage. Each new day creates new file automatically. Old files remain for history. Check current file: ls ~/.claude-code-usage/daily-*.usage. If date command timezone incorrect, reset may occur at wrong time. Verify: date +%Y-%m-%d.
Pacing indicator showing incorrect ahead/below pace status
Pacing compares actual usage % vs expected % based on hours elapsed. Formula: expected = (24 - hours_remaining) * 100 / 24. At noon (12 hours), expected is 50%. If actual usage is 40%, shows 'below pace'. Adjust logic if you have non-24-hour daily windows.
Remaining budget showing negative or incorrect values
Remaining = DAILY_LIMIT - updated_usage. If negative, script displays '$0.00 left' and OVER BUDGET status. Verify DAILY_LIMIT setting matches your actual budget. Check updated_usage calculation: cat usage file. bc arithmetic: echo '$DAILY_LIMIT - $updated_usage' | bc.
Multiple sessions causing duplicate cost additions
Script adds session_cost every update - can over-count if same session updates multiple times. For accuracy, track by unique session_id and update (don't add) session totals. Alternative: integrate with official usage API if available. Current implementation optimized for real-time awareness, not perfect accounting.
Loading reviews...