init: claude-usage.py — dual-path Claude usage monitor

- Web API (curl_cffi + Chrome TLS fingerprint) as primary
- OAuth API (stdlib urllib) as fallback
- Burn rate analysis, daily marks, history logging
This commit is contained in:
Eagle 2026-03-19 07:15:45 +08:00
commit 850f003df1
2 changed files with 397 additions and 0 deletions

60
README.md Normal file
View file

@ -0,0 +1,60 @@
# 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
1. Install dependency: `pip install curl_cffi` (or use `uv run`)
2. Set `WEB_TOKEN` to your claude.ai sessionKey cookie
3. Set org ID in the API URLs
4. Place OAuth credentials at `~/.claude/.credentials.json`
## Usage
```bash
# 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)