Created
October 19, 2025 10:46
-
-
Save kasbah/c80bf78546d2ec200efe3d9167ad0d1d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import subprocess | |
| import json | |
| from datetime import datetime | |
| import sys | |
| from pathlib import Path | |
| CACHE_FILE = Path.home() / ".cache" / "claude_status.json" | |
| def load_cache(): | |
| if CACHE_FILE.exists(): | |
| with open(CACHE_FILE, "r") as f: | |
| data = json.load(f) | |
| return data["response"], True | |
| return None, False | |
| def save_cache(response): | |
| CACHE_FILE.parent.mkdir(parents=True, exist_ok=True) | |
| with open(CACHE_FILE, "w") as f: | |
| json.dump({"response": response, "timestamp": datetime.now().timestamp()}, f) | |
| auth_file = Path.home() / ".local" / "share" / "opencode" / "auth.json" | |
| try: | |
| with open(auth_file, "r") as f: | |
| auth_data = json.load(f) | |
| oauth_token = auth_data.get("anthropic", {}).get("access") | |
| if not oauth_token: | |
| print("Claude: Error - no auth token") | |
| sys.exit(1) | |
| except Exception: | |
| print("Claude: Error - no auth token") | |
| sys.exit(1) | |
| curl_cmd = ( | |
| f"curl -H 'Accept: application/json, text/plain, */*' " | |
| f"-H 'Content-Type: application/json' " | |
| f"-H 'User-Agent: claude-code/2.0.22' " | |
| f"-H 'Authorization: Bearer {oauth_token}' " | |
| f"-H 'anthropic-beta: oauth-2025-04-20' " | |
| f"--compressed -H 'Connection: close' " | |
| f"https://api.anthropic.com/api/oauth/usage" | |
| ) | |
| response = None | |
| is_cached = False | |
| try: | |
| result = subprocess.run( | |
| curl_cmd, shell=True, capture_output=True, text=True, timeout=5 | |
| ) | |
| response = json.loads(result.stdout) | |
| save_cache(response) | |
| except: | |
| response, is_cached = load_cache() | |
| if response is None: | |
| print("Claude: Error - network error") | |
| sys.exit(0) | |
| def format_time_remaining(reset_time_str): | |
| if not reset_time_str: | |
| return "N/A", 0 | |
| try: | |
| reset_time = datetime.fromisoformat(reset_time_str.replace("Z", "+00:00")) | |
| now = datetime.now(reset_time.tzinfo) | |
| diff = (reset_time - now).total_seconds() | |
| if diff <= 0: | |
| return "0m", 0 | |
| days = int(diff // 86400) | |
| hours = int((diff % 86400) // 3600) | |
| minutes = int((diff % 3600) // 60) | |
| if days > 0: | |
| return f"{days}d {hours}h{minutes:02d}m", diff | |
| elif hours > 0: | |
| return f"{hours}h{minutes:02d}m", diff | |
| else: | |
| return f"{minutes}m", diff | |
| except: | |
| return "N/A", 0 | |
| def get_util_color(util): | |
| if isinstance(util, int): | |
| if util < 30: | |
| return "green" | |
| elif util < 70: | |
| return "orange" | |
| else: | |
| return "red" | |
| return "grey" | |
| def get_time_color(seconds, max_seconds): | |
| if seconds == 0: | |
| return "grey" | |
| # For 5 hour window: max is 5 hours = 18000 seconds | |
| # For 7 day window: max is 7 days = 604800 seconds | |
| ratio = seconds / max_seconds | |
| if ratio > 0.7: # More than 70% time remaining | |
| return "orange" | |
| elif ratio > 0.3: # 30-70% time remaining | |
| return "grey" | |
| else: # Less than 30% time remaining | |
| return "green" | |
| five_hour_util = response.get("five_hour", {}).get("utilization", "N/A") | |
| five_hour_reset = response.get("five_hour", {}).get("resets_at", "") | |
| five_hour_time, five_hour_seconds = format_time_remaining(five_hour_reset) | |
| five_hour_util_color = get_util_color(five_hour_util) | |
| five_hour_time_color = get_time_color(five_hour_seconds, 18000) | |
| seven_day_util = response.get("seven_day", {}).get("utilization", "N/A") | |
| seven_day_reset = response.get("seven_day", {}).get("resets_at", "") | |
| seven_day_time, seven_day_seconds = format_time_remaining(seven_day_reset) | |
| seven_day_util_color = get_util_color(seven_day_util) | |
| seven_day_time_color = get_time_color(seven_day_seconds, 604800) | |
| cache_indicator = " (cached)" if is_cached else "" | |
| print( | |
| f"Claude: <fc={five_hour_util_color}>{five_hour_util}</fc>% / <fc={five_hour_time_color}>{five_hour_time}</fc> — <fc={seven_day_util_color}>{seven_day_util}</fc>% / <fc={seven_day_time_color}>{seven_day_time}</fc>{cache_indicator}" | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment