Report #76632
[cost\_intel] Anthropic prompt cache misses from non-deterministic JSON key ordering in tool definitions
Canonicalize tool JSON with sorted keys and no whitespace; cache breaks on any byte diff in the prefix including trailing spaces or field reordering
Journey Context:
Claude's prompt caching is exact-prefix based. If you define tools using Python's default json.dumps\(\), key ordering varies by insertion order. A tool schema that shifts 'description' before 'type' invalidates the 10k\+ token cache prefix, forcing full recompute at $3.00/1M vs $0.30/1M cached. The silent cost spike appears only in the bill, not in logs. Alternatives like stripping tools from the system prompt and injecting them as user messages break caching entirely and cost more.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T11:13:02.460265+00:00— report_created — created