Skip to content

Instantly share code, notes, and snippets.

@planecore
Forked from vildanbina/README.md
Last active February 28, 2026 20:00
Show Gist options
  • Select an option

  • Save planecore/1c3ac2bec91f9a9b9f009f9cf0079d09 to your computer and use it in GitHub Desktop.

Select an option

Save planecore/1c3ac2bec91f9a9b9f009f9cf0079d09 to your computer and use it in GitHub Desktop.
Show 5h/7d API usage in your terminal statusline (macOS)

CC Usage Statusline (macOS)

Shows your remaining 5-hour and 7-day Claude API capacity with visual bars and reset times.

Usage: 5h ━━━━━━━━━━ 98% (02:00) │ 7d ━━━━━━━━━╌ 90% (Fri 08:00)

Reads credentials from the macOS Keychain (Claude Code-credentials).

Install

curl -o ~/.local/bin/cc-usage-statusline.sh \
  https://gist.githubusercontent.com/planecore/1c3ac2bec91f9a9b9f009f9cf0079d09/raw/cc-usage-statusline.sh
chmod +x ~/.local/bin/cc-usage-statusline.sh

Requires jq and curl.

Claude Code Status Line

Add to ~/.claude/settings.json:

{
  "statusLine": {
    "type": "command",
    "command": "~/.local/bin/cc-usage-statusline.sh"
  }
}

Configuration

Variable Default Description
CC_CACHE_TTL 60 Cache lifetime in seconds

Results are cached for 1 minute, with API calls rate-limited to every 30 seconds.

#!/bin/bash
#
# Show 5h + 7d API remaining capacity in your statusline
#
set -euo pipefail
CACHE="$HOME/.cache/cc-usage.txt"
LOCK="$HOME/.cache/cc-usage.lock"
TTL="${CC_CACHE_TTL:-60}"
get_mtime() {
stat -f '%m' "$1" 2>/dev/null || echo 0
}
# Build a 10-segment bar: ━ for remaining, ╌ for used
make_bar() {
local remaining=$1
local filled=$(( (remaining + 5) / 10 ))
[[ $filled -gt 10 ]] && filled=10
[[ $filled -lt 0 ]] && filled=0
local empty=$(( 10 - filled ))
if [[ $filled -gt 0 ]]; then printf '%0.s━' $(seq 1 $filled); fi
if [[ $empty -gt 0 ]]; then printf '%0.s╌' $(seq 1 $empty); fi
}
# Return cached if fresh
if [[ -f "$CACHE" ]]; then
age=$(($(date +%s) - $(get_mtime "$CACHE")))
[[ $age -lt $TTL ]] && cat "$CACHE" && exit 0
fi
# Rate limit API calls
if [[ -f "$LOCK" ]]; then
age=$(($(date +%s) - $(get_mtime "$LOCK")))
[[ $age -lt 30 ]] && { [[ -f "$CACHE" ]] && cat "$CACHE"; exit 0; }
fi
touch "$LOCK"
# Get token from macOS Keychain
creds=$(security find-generic-password -s "Claude Code-credentials" -w 2>/dev/null) || true
[[ -z "$creds" ]] && echo "[No creds]" && exit 1
token=$(echo "$creds" | jq -r '.claudeAiOauth.accessToken // empty' 2>/dev/null)
[[ -z "$token" ]] && echo "[Bad token]" && exit 1
# Fetch usage
resp=$(curl -s --max-time 5 \
"https://api.anthropic.com/api/oauth/usage" \
-H "Authorization: Bearer $token" \
-H "anthropic-beta: oauth-2025-04-20" 2>/dev/null) || true
[[ -z "$resp" ]] && { [[ -f "$CACHE" ]] && cat "$CACHE"; echo "[Timeout]"; exit 1; }
session=$(echo "$resp" | jq -r '.five_hour.utilization // empty' 2>/dev/null)
weekly=$(echo "$resp" | jq -r '.seven_day.utilization // empty' 2>/dev/null)
h5_resets=$(echo "$resp" | jq -r '.five_hour.resets_at // empty' 2>/dev/null)
d7_resets=$(echo "$resp" | jq -r '.seven_day.resets_at // empty' 2>/dev/null)
[[ -z "$session" || -z "$weekly" ]] && { [[ -f "$CACHE" ]] && cat "$CACHE"; echo "[Error]"; exit 1; }
# Calculate remaining
h5_left=$(awk "BEGIN { printf \"%.0f\", 100 - $session }")
d7_left=$(awk "BEGIN { printf \"%.0f\", 100 - $weekly }")
# Format reset times (convert UTC to local)
h5_epoch=$(date -juf "%Y-%m-%dT%H:%M:%S" "${h5_resets%%.*}" "+%s" 2>/dev/null || echo "0")
d7_epoch=$(date -juf "%Y-%m-%dT%H:%M:%S" "${d7_resets%%.*}" "+%s" 2>/dev/null || echo "0")
h5_time=$(date -jf "%s" "$h5_epoch" "+%H:%M" 2>/dev/null || echo "?")
d7_time=$(date -jf "%s" "$d7_epoch" "+%a %H:%M" 2>/dev/null || echo "?")
h5_bar=$(make_bar "$h5_left")
d7_bar=$(make_bar "$d7_left")
echo "Usage: 5h ${h5_bar} ${h5_left}% (${h5_time}) │ 7d ${d7_bar} ${d7_left}% (${d7_time})" | tee "$CACHE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment