{"record":{"id":"cf92117dc6027926","repo":"JuliusBrussee/caveman","slug":"parse-claude-usage-json-w","errorCode":null,"errorMessage":"parse Claude usage JSON: %w","messagePattern":"parse Claude usage JSON: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"proxy/internal/store/usage_import.go","lineNumber":117,"sourceCode":"\t\tif event.Basis != observedLocal && event.Basis != \"observed_provider\" {\n\t\t\treturn \"estimated\"\n\t\t}\n\t}\n\treturn observedLocal\n}\n\nfunc (s *Store) RefreshClaudeUsageFromEnv() (ImportSummary, error) {\n\traw := os.Getenv(\"CAVEMAN_CLAUDE_USAGE_JSON\")\n\tif raw == \"\" {\n\t\tfetched, err := fetchClaudeUsageJSON()\n\t\tif err != nil {\n\t\t\treturn ImportSummary{}, err\n\t\t}\n\t\traw = string(fetched)\n\t}\n\tvar v any\n\tif err := json.Unmarshal([]byte(raw), &v); err != nil {\n\t\treturn ImportSummary{}, fmt.Errorf(\"parse Claude usage JSON: %w\", err)\n\t}\n\tquotas := quotaEventsFromAny(\"anthropic\", \"claude_usage_link\", \"linked_api\", time.Now().UTC().Format(time.RFC3339), v)\n\tn, err := s.InsertQuotaEvents(quotas)\n\tif err != nil {\n\t\treturn ImportSummary{}, err\n\t}\n\treturn ImportSummary{Source: \"claude\", QuotaImported: n, Basis: \"linked_api\"}, nil\n}\n\nfunc fetchClaudeUsageJSON() ([]byte, error) {\n\tsessionKey := os.Getenv(\"CAVEMAN_CLAUDE_SESSION_KEY\")\n\torgID := os.Getenv(\"CAVEMAN_CLAUDE_ORG_ID\")\n\tif sessionKey == \"\" || orgID == \"\" {\n\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}","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/proxy/internal/store/usage_import.go#L99-L135","documentation":"RefreshClaudeUsageFromEnv parses the Claude usage document (from CAVEMAN_CLAUDE_USAGE_JSON or the fetched HTTP body) with json.Unmarshal into any. If the raw string is not valid JSON, the parse error is wrapped as \"parse Claude usage JSON\". The library requires well-formed JSON to extract quota events.","triggerScenarios":"CAVEMAN_CLAUDE_USAGE_JSON containing truncated/partial JSON, HTML error pages, or trailing garbage; the fetched claude.ai /usage endpoint returning non-JSON (login page, HTML error).","commonSituations":"Exported/pasted usage JSON saved incompletely; env var containing shell-escaped or quoted JSON that fails to unmarshal; claude.ai session expired so the fetch returned an HTML redirect page.","solutions":["Validate the JSON with a parser (jq, python -m json.tool) before setting CAVEMAN_CLAUDE_USAGE_JSON","Re-fetch usage with a valid session key so you get the JSON document, not an HTML page","Check the wrapped inner error (%w) for the exact JSON syntax offset and fix that spot","If pasting into an env var, ensure quoting preserves newlines and doesn't double-escape"],"exampleFix":"// before\nos.Setenv(\"CAVEMAN_CLAUDE_USAGE_JSON\", readFile(\"usage.html\")) // not JSON\n// after\ndata := readFile(\"usage.json\")\nif !json.Valid(data) {\n\treturn fmt.Errorf(\"usage file is not valid JSON\")\n}\nos.Setenv(\"CAVEMAN_CLAUDE_USAGE_JSON\", string(data))","handlingStrategy":"validation","validationCode":"if !json.Valid([]byte(raw)) {\n\treturn errors.New(\"CAVEMAN_CLAUDE_USAGE_JSON is not valid JSON\")\n}","typeGuard":null,"tryCatchPattern":"summary, err := store.RefreshClaudeUsageFromEnv(ctx)\nif err != nil {\n\tvar perr *json.SyntaxError\n\tif errors.As(err, &perr) {\n\t\tlog.Printf(\"usage JSON invalid at offset %d\", perr.Offset)\n\t}\n}","preventionTips":["Run json.Valid or jq over files before wiring them in","Confirm the fetch source returns application/json, not HTML","Avoid hand-editing exported JSON; re-export instead"],"tags":["json","parsing","usage-import"],"backgroundTag":"json-parse-error","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"}