{"record":{"id":"4ec81a7ea6640ef1","repo":"JuliusBrussee/caveman","slug":"claude-usage-request-failed-with-http-d","errorCode":null,"errorMessage":"Claude usage request failed with HTTP %d","messagePattern":"Claude usage request failed with HTTP (.+?)","errorType":"http","errorClass":null,"httpStatus":null,"severity":"error","filePath":"proxy/internal/store/usage_import.go","lineNumber":149,"sourceCode":"\t\treturn nil, fmt.Errorf(\"Claude usage refresh needs CAVEMAN_CLAUDE_USAGE_JSON or CAVEMAN_CLAUDE_SESSION_KEY plus CAVEMAN_CLAUDE_ORG_ID\")\n\t}\n\tif strings.Contains(orgID, \"/\") || strings.Contains(orgID, \"..\") {\n\t\treturn nil, fmt.Errorf(\"invalid Claude organization id\")\n\t}\n\treq, err := http.NewRequest(\"GET\", \"https://claude.ai/api/organizations/\"+orgID+\"/usage\", nil)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\treq.Header.Set(\"accept\", \"application/json\")\n\treq.Header.Set(\"cookie\", \"sessionKey=\"+sessionKey)\n\tclient := http.Client{Timeout: 15 * time.Second}\n\tresp, err := client.Do(req)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tdefer resp.Body.Close()\n\tif resp.StatusCode < 200 || resp.StatusCode >= 300 {\n\t\treturn nil, fmt.Errorf(\"Claude usage request failed with HTTP %d\", resp.StatusCode)\n\t}\n\treturn io.ReadAll(io.LimitReader(resp.Body, 4<<20))\n}\n\nfunc codexPaths(root string) ([]string, error) {\n\tpaths, _, err := codexPathsUntil(root, nil)\n\treturn paths, err\n}\n\n// codexPathsUntil preserves codexPaths' discovery contract while allowing the\n// first-run behavior scan to stop a huge cold tree. Other import callers pass no\n// deadline and retain their existing exhaustive walk.\nfunc codexPathsUntil(root string, expired func() bool) ([]string, bool, error) {\n\tvar paths []string\n\ttimeBoxed := false\n\tstopped := func() bool {\n\t\tif expired == nil || !expired() {\n\t\t\treturn false","sourceCodeStart":131,"sourceCodeEnd":167,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/proxy/internal/store/usage_import.go#L131-L167","documentation":"fetchClaudeUsageJSON performs a GET to claude.ai's usage endpoint and treats any non-2xx HTTP status as a failure, wrapping the status code in this error. The body is intentionally not read beyond the status check, so the error signals an upstream/API rejection.","triggerScenarios":"claude.ai returns 401/403 for an expired or invalid session key, 404 for a wrong org id, or 5xx during an outage — any status outside [200,300).","commonSituations":"Stale session cookie after re-login; incorrect organization id; rate limiting or temporary claude.ai incidents; corporate proxy intercepting with an error page status.","solutions":["Refresh CAVEMAN_CLAUDE_SESSION_KEY with a current session","Verify CAVEMAN_CLAUDE_ORG_ID matches a real organization for that session","Retry after a delay on 5xx, and check claude.ai status for incidents","Alternatively set CAVEMAN_CLAUDE_USAGE_JSON with an exported usage document to bypass the HTTP fetch"],"exampleFix":"// before\nresp, _ := fetchUsage(staleKey, orgID) // 401\n// after\nresp, err := fetchUsage(currentSessionKey, orgID)\nif err != nil {\n\tif strings.Contains(err.Error(), \"HTTP 401\") {\n\t\t// re-authenticate then retry\n\t}\n}","handlingStrategy":"retry","validationCode":"// Cannot pre-validate a remote status; ensure credentials are current before calling.\n// e.g. probe: if session key older than N days, force re-auth first.","typeGuard":null,"tryCatchPattern":"err := runUsage()\nif err != nil && strings.Contains(err.Error(), \"Claude usage request failed with HTTP\") {\n\tvar code int\n\tfmt.Sscanf(err.Error(), \"Claude usage request failed with HTTP %d\", &code)\n\tif code == 401 || code == 403 { /* refresh session */ } else if code >= 500 { /* retry with backoff */ }\n}","preventionTips":["Rotate session keys regularly before expiry","Retry 5xx with exponential backoff","Keep an exported usage JSON fallback (CAVEMAN_CLAUDE_USAGE_JSON) for outages"],"tags":["http","network","usage-import"],"backgroundTag":"http-error-response","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}