feat(supervisor): gate quota on the real claude.ai usage % (OAuth usage endpoint) #243
Reference in New Issue
Block a user
Delete Branch "worktree-supervisor-usage-endpoint"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
רקע
חיים רצה לראות את אחוז-השימוש האמיתי (ה"29%" שמופיע ב-Claude Code/claude.ai). מחקר בתיעוד אנטרופיק + בדיקה חיה מצא endpoint לא-מתועד
GET https://api.anthropic.com/api/oauth/usageשמחזיר בדיוק את אותם אחוזים (5-שעות / שבועי / שבועי-Opus) + זמני-איפוס.מה ה-PR עושה (שכבת לוגיקה/נתון בלבד — host-side)
subscription_usage()— קורא token מ-~/.claude/.credentials.jsonו-GET ל-/api/oauth/usageעם UAclaude-code/*(חובה, אחרת 429). מחזיר{five_hour, seven_day, seven_day_opus, ...}אוNone.quota_available()— עכשיו מעדיף את ה-endpoint: הדריינר מתחדש רק כשחלונות 5-שעות + שבועי + שבועי-Opus כולם <100% (המחלץ רץ Opus). מדויק יותר מבדיקת-ה-probe ורואה כל מגבלה כמו ה-UI. fallback לבדיקתclaude -pהזעירה אם ה-endpoint לא זמין (הוא לא-רשמי, עלול להשתנות).statusמדפיס עכשיו את האחוזים החיים + זמני-איפוס.אימות חי
subscription_usage()→five_hour.utilization=37%(תאם ל-UI).quota_available()→True. הסקריפט עוברast.parse.מה לא נכלל (שער-עיצוב)
הצגת האחוז על דף
/operationsהיא שינוי-UI ויזואלי → חייב לעבור קודם דרך שער Claude Design (web-ui/AGENTS.md). נדחה לשלב נפרד דרך השער. ה-PR הזה הוא רק התשתית (host-side), בלי deploy.Invariants
🤖 Generated with Claude Code
The supervisor's quota check used a tiny `claude -p` probe to decide whether the claude.ai subscription had room. That works but is indirect (an Opus-adjacent round trip) and only answers yes/no. Anthropic exposes the actual utilization — the same 5-hour / weekly / weekly-Opus percentages the Claude Code status bar shows — via the (undocumented) GET /api/oauth/usage endpoint. - subscription_usage(): reads the OAuth token from ~/.claude/.credentials.json and GETs /api/oauth/usage with the required `claude-code/*` User-Agent (without it the request hits an aggressively rate-limited bucket and 429s). Returns the parsed {five_hour, seven_day, seven_day_opus, ...} or None on any failure. - quota_available(): now prefers the endpoint — a drain run resumes only when the 5-hour, weekly, AND weekly-Opus windows are all <100% (the extractor runs Opus). More precise than the probe and sees every limit the way the UI does. Falls back to the `claude -p` probe when the endpoint is unreachable (it's undocumented and may change). - `status` subcommand now prints the live percentages + reset times. Note: this is the data/logic layer only. Surfacing the % on the /operations page is a visual UI change and must go through the Claude Design gate first (web-ui/AGENTS.md) — deferred. Invariants: G1 (resume decision driven by the authoritative usage state). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>