JuliusBrussee/caveman · error

parse Claude usage JSON

Error message

parse Claude usage JSON: %w

What it means

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.

Solutions

  1. Validate the JSON with a parser (jq, python -m json.tool) before setting CAVEMAN_CLAUDE_USAGE_JSON
  2. Re-fetch usage with a valid session key so you get the JSON document, not an HTML page
  3. Check the wrapped inner error (%w) for the exact JSON syntax offset and fix that spot
  4. If pasting into an env var, ensure quoting preserves newlines and doesn't double-escape

Example fix

// before
os.Setenv("CAVEMAN_CLAUDE_USAGE_JSON", readFile("usage.html")) // not JSON
// after
data := readFile("usage.json")
if !json.Valid(data) {
	return fmt.Errorf("usage file is not valid JSON")
}
os.Setenv("CAVEMAN_CLAUDE_USAGE_JSON", string(data))
Defensive patterns

Strategy: validation

Validate before calling

if !json.Valid([]byte(raw)) {
	return errors.New("CAVEMAN_CLAUDE_USAGE_JSON is not valid JSON")
}

Try / catch

summary, err := store.RefreshClaudeUsageFromEnv(ctx)
if err != nil {
	var perr *json.SyntaxError
	if errors.As(err, &perr) {
		log.Printf("usage JSON invalid at offset %d", perr.Offset)
	}
}

Prevention

When it happens

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

Common situations: 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.

Understand the failure class

Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.

Related errors


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

Appendix: source

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

		if event.Basis != observedLocal && event.Basis != "observed_provider" {
			return "estimated"
		}
	}
	return observedLocal
}

func (s *Store) RefreshClaudeUsageFromEnv() (ImportSummary, error) {
	raw := os.Getenv("CAVEMAN_CLAUDE_USAGE_JSON")
	if raw == "" {
		fetched, err := fetchClaudeUsageJSON()
		if err != nil {
			return ImportSummary{}, err
		}
		raw = string(fetched)
	}
	var v any
	if err := json.Unmarshal([]byte(raw), &v); err != nil {
		return ImportSummary{}, fmt.Errorf("parse Claude usage JSON: %w", err)
	}
	quotas := quotaEventsFromAny("anthropic", "claude_usage_link", "linked_api", time.Now().UTC().Format(time.RFC3339), v)
	n, err := s.InsertQuotaEvents(quotas)
	if err != nil {
		return ImportSummary{}, err
	}
	return ImportSummary{Source: "claude", QuotaImported: n, Basis: "linked_api"}, nil
}

func fetchClaudeUsageJSON() ([]byte, error) {
	sessionKey := os.Getenv("CAVEMAN_CLAUDE_SESSION_KEY")
	orgID := os.Getenv("CAVEMAN_CLAUDE_ORG_ID")
	if sessionKey == "" || orgID == "" {
		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")
	}

View on GitHub (pinned to 3ee70a1026)