JuliusBrussee/caveman · error

Claude usage request failed with HTTP

Error message

Claude usage request failed with HTTP %d

What it means

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.

Solutions

  1. Refresh CAVEMAN_CLAUDE_SESSION_KEY with a current session
  2. Verify CAVEMAN_CLAUDE_ORG_ID matches a real organization for that session
  3. Retry after a delay on 5xx, and check claude.ai status for incidents
  4. Alternatively set CAVEMAN_CLAUDE_USAGE_JSON with an exported usage document to bypass the HTTP fetch

Example fix

// before
resp, _ := fetchUsage(staleKey, orgID) // 401
// after
resp, err := fetchUsage(currentSessionKey, orgID)
if err != nil {
	if strings.Contains(err.Error(), "HTTP 401") {
		// re-authenticate then retry
	}
}
Defensive patterns

Strategy: retry

Validate before calling

// Cannot pre-validate a remote status; ensure credentials are current before calling.
// e.g. probe: if session key older than N days, force re-auth first.

Try / catch

err := runUsage()
if err != nil && strings.Contains(err.Error(), "Claude usage request failed with HTTP") {
	var code int
	fmt.Sscanf(err.Error(), "Claude usage request failed with HTTP %d", &code)
	if code == 401 || code == 403 { /* refresh session */ } else if code >= 500 { /* retry with backoff */ }
}

Prevention

When it happens

Trigger: 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).

Common situations: Stale session cookie after re-login; incorrect organization id; rate limiting or temporary claude.ai incidents; corporate proxy intercepting with an error page status.

Understand the failure class

Background: "API error: {status}" and "HTTP 401/403/404/429/5xx" errors: non-2xx HTTP responses explained — this error's family across 27 libraries.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/4ec81a7ea6640ef1. Report an issue: GitHub.

Appendix: source

Thrown at proxy/internal/store/usage_import.go:149

		return nil, fmt.Errorf("Claude usage refresh needs CAVEMAN_CLAUDE_USAGE_JSON or CAVEMAN_CLAUDE_SESSION_KEY plus CAVEMAN_CLAUDE_ORG_ID")
	}
	if strings.Contains(orgID, "/") || strings.Contains(orgID, "..") {
		return nil, fmt.Errorf("invalid Claude organization id")
	}
	req, err := http.NewRequest("GET", "https://claude.ai/api/organizations/"+orgID+"/usage", nil)
	if err != nil {
		return nil, err
	}
	req.Header.Set("accept", "application/json")
	req.Header.Set("cookie", "sessionKey="+sessionKey)
	client := http.Client{Timeout: 15 * time.Second}
	resp, err := client.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	if resp.StatusCode < 200 || resp.StatusCode >= 300 {
		return nil, fmt.Errorf("Claude usage request failed with HTTP %d", resp.StatusCode)
	}
	return io.ReadAll(io.LimitReader(resp.Body, 4<<20))
}

func codexPaths(root string) ([]string, error) {
	paths, _, err := codexPathsUntil(root, nil)
	return paths, err
}

// codexPathsUntil preserves codexPaths' discovery contract while allowing the
// first-run behavior scan to stop a huge cold tree. Other import callers pass no
// deadline and retain their existing exhaustive walk.
func codexPathsUntil(root string, expired func() bool) ([]string, bool, error) {
	var paths []string
	timeBoxed := false
	stopped := func() bool {
		if expired == nil || !expired() {
			return false

View on GitHub (pinned to 3ee70a1026)