Claude API usage monitor — curl_cffi + OAuth dual-path
- Web API (curl_cffi + Chrome TLS fingerprint) as primary - OAuth API (stdlib urllib) as fallback - Burn rate analysis, daily marks, history logging |
||
|---|---|---|
| claude-usage.py | ||
| README.md | ||
claude-usage
Claude API usage monitor with dual-path authentication.
Two API Paths
1. Web API (Primary) — curl_cffi + sessionKey cookie
- Endpoint:
claude.ai/api/organizations/{org_id}/usage - Uses curl_cffi
impersonate="chrome"to bypass Cloudflare TLS fingerprint (JA3/JA4) detection - Returns extra_usage, prepaid/credits balance
- sessionKey needs manual refresh from browser devtools
2. OAuth API (Fallback) — stdlib urllib + Bearer token
- Endpoint:
api.anthropic.com/api/oauth/usage - Standard Python TLS (no fingerprint needed)
- Token auto-refreshes via refresh_token
- No extra_usage/prepaid data
Setup
- Install dependency:
pip install curl_cffi(or useuv run) - Set
WEB_TOKENto your claude.ai sessionKey cookie - Set org ID in the API URLs
- Place OAuth credentials at
~/.claude/.credentials.json
Usage
# Show usage
uv run claude-usage.py
# Mark daily baseline (run at 00:00)
uv run claude-usage.py --mark
# Log snapshot to history
uv run claude-usage.py --log
# Force OAuth API (skip Web API)
uv run claude-usage.py --oauth
Output
📊 Token Usage (claude.ai web @ 07:04:49)
Session ░░░░░░░░░░ 1% used 🟢 ↻4h55m
Weekly ███████▊░░ 78% used 🟢 ↻1d3h +20%
Sonnet ▏░░░░░░░░░ 2% used 🟢 ↻1d5h
--- 燃燒速率 ---
完美速率: 14.3%/天
當前速率: 13.4%/天(93% ratio)
可以超速 141%喔! 🏎️
Credits
Original: 拍拍 🦅 (Eagle) Bash reference: 踏踏 🐾 (Tata)