Record prompt cache diagnostics in `provider_details`: on by default for OpenAI Responses, `anthropic_cache_diagnostics` opt-in
Requests provider prompt-cache diagnostics and records the provider's answer in ModelResponse.provider_details, so a cache miss can be explained, not just detected. This feeds the prompt-cache-stability work in #6534; that PR's instrumentation is not touched here.
- OpenAI Responses, on by default for GPT-5.6 and later on the OpenAI API: the most recent OpenAI response in the history goes out as
prompt_cache_options.comparison_response_id, and the answer is recorded underprovider_details['prompt_cache_diagnostics']. The raw OpenAI object looks like{'type': 'cache_hit'}or{'type': 'cache_miss', 'reason': 'model_changed', 'cache_missed_tokens': 4175, 'comparison_reusable_tokens': 4175}. The newopenai_prompt_cache_diagnostics=Falsesetting turns it off. A new profile flag,openai_responses_supports_prompt_cache_diagnostics, is set only byOpenAIProvider.model_profile, because OpenAI-compatible endpoints reject the field. It is explicitly off for Codex, which was not tested. - Anthropic, off by default: the new
anthropic_cache_diagnostics=Truesetting sendsdiagnostics: {previous_message_id: <latest msg_ id or null>}. The answer is recorded underprovider_details['cache_diagnostics']as Anthropic'sdiagnosticsobject:{'cache_miss_reason': {'type': 'tools_changed', 'cache_missed_input_tokens': 3713}}, or{'cache_miss_reason': None}while the comparison is still pending. The key is absent when no divergence was found. The field is never sent through the Bedrock, Bedrock Mantle, Vertex or Foundry client classes. - A baseline is sent only when it is the latest response from the same provider and has the provider's own ID prefix (
msg_orresp_). Both APIs return a 400 for an ID with another prefix. - Both streaming and non-streaming responses are covered. Anthropic reports on
message_start. OpenAI's diagnostics are read only from terminal events, becauseresponse.in_progressreportsunavailablebefore the comparison finishes.
Why Anthropic is opt-in
The live probes below found no cost, cache or latency downside on either provider. Anthropic, however, has two documented request-level costs that "always on" would impose on every user:
- HIPAA: API and data retention lists cache diagnostics as HIPAA eligible: No, and "under HIPAA readiness, the API blocks requests that include a 'No' feature and returns a
400error". If this were on by default, every request from a HIPAA-enabled org would fail. I could not test this, because our org is not HIPAA-enabled. - Retention: Anthropic is "ZDR eligible (qualified)": it stores a fingerprint (hashes and token counts, no prompt content) only for requests that include
diagnostics. Sending the field on every request would change the retention footprint of every Anthropic user without their knowledge.
OpenAI documents no equivalent. Diagnostics records are ZDR-compatible, and requests work with store=false.
Evidence (live probes, 2026-09-29)
Cost and cache behavior (Anthropic claude-haiku-4-5, about 6.4K-token cached system prompt, 2 reps each)
turn 1 diagnostics | turn 2 diagnostics | turn 1 cache write | turn 2 cache read | turn 2 diagnostics |
|---|---|---|---|---|
| omitted | omitted | 6424 / 6423 | 6424 / 6423 | null |
null | previous id | 6425 / 6422 | 6425 / 6422 | null |
null | omitted | 6421 / 6425 | 6421 / 6425 | null |
| omitted | previous id | 6424 / 6423 | 6424 / 6423 | previous_message_not_found |
Usage fields were identical across conditions, with no extra usage line. The field is not part of the cache key: a cache written without it is read with it, and the reverse. A baseline that didn't opt in returns previous_message_not_found, which is why the first turn sends null.
Cost and cache behavior (OpenAI gpt-5.6-sol, about 3.2K-token prefix, 2 reps each)
turn 1 prompt_cache_options | turn 2 comparison_response_id | turn 2 cached tokens | turn 2 diagnostics |
|---|---|---|---|
| omitted | omitted | 3175 / 3177 | none |
| omitted | set | 3176 / 3178 | cache_hit |
| set | set | 3178 / 3176 | cache_hit |
No baseline opt-in is needed, and usage is unchanged. With store=false on the baseline, the comparing request or both, the result was cache_hit every time.
Latency (turn 2 only, alternating order)
| provider | with | without |
|---|---|---|
Anthropic claude-haiku-4-5, N=10 each | median 0.510s, mean 0.520s, stdev 0.043s | median 0.508s, mean 0.517s, stdev 0.046s |
OpenAI gpt-5.6-sol, N=16 each | median 1.241s, mean 1.286s, max 1.851s | median 1.279s, mean 2.358s, max 17.751s (a single API tail outlier) |
A first OpenAI N=10 run had its 17.7s outlier on the with side. The rerun above had it on the without side, so it is API tail noise.
Unsupported targets
| target | result |
|---|---|
Anthropic API: claude-opus-5-5, claude-sonnet-5, claude-sonnet-4-5, claude-haiku-4-5 | accepted (retired models 404 with or without the field) |
Anthropic on Bedrock (AsyncAnthropicBedrock, InvokeModel) | 400 diagnostics: Extra inputs are not permitted |
Anthropic on Bedrock Mantle (AsyncAnthropicBedrockMantle) | 400 diagnostics: Extra inputs are not permitted |
| Anthropic on Vertex / Foundry | not tested (no model access or key); documented as "not available" |
| Pydantic AI Gateway, Anthropic route | accepted; real diagnostics |
Anthropic-compatible: OpenRouter /api/v1/messages, xAI | silently ignored (IDs are gen-… / UUIDs) |
Anthropic API with resp_…, chatcmpl-…, empty or over-256-char ID | 400 must be the id from a prior /v1/messages response (starts with msg_) |
Anthropic API with an unknown msg_… ID | 200, previous_message_not_found |
OpenAI Responses: gpt-5.5, gpt-5.4, gpt-5-mini, gpt-4.1-mini, gpt-4o-mini, o4-mini | accepted, always unavailable |
OpenAI Responses: gpt-5.6-sol, gpt-5.6-luna | accepted; real diagnostics |
OpenAI Chat Completions (via extra_body) on gpt-5.6-sol, gpt-5.5, gpt-4.1-mini | 400 Unknown parameter: 'prompt_cache_options.comparison_response_id' |
OpenRouter Responses API (openai/gpt-5.6-sol) | 400 Invalid Responses API request |
| Pydantic AI Gateway, OpenAI Responses route | accepted; real diagnostics |
| Azure OpenAI | not tested (the key in .env returns 401) |
OpenAI with a msg_… / chatcmpl-… ID | 400 Expected an ID that begins with 'resp' |
OpenAI with an unknown resp_… ID | 200, comparison_response_not_found |
ID requirements and model switches
- OpenAI:
store=falseworks on either side. The docs say ZDR is compatible. - Switching to another model of the same provider (as
FallbackModeldoes) works: Anthropic returnsmodel_changedfor haiku-4-5 → sonnet-4-5, and OpenAI returnscache_miss/model_changedfor sol → luna. - A response from another provider is never used as the baseline. A foreign ID returns a 400 in both directions.
- No beta header is needed on either side. Anthropic's
cache-diagnosis-2026-04-07is no longer required. - SDK support:
anthropic>=1.9.0andopenai>=3.19.0, our current minimums, both ship the typed request and response fields.
Miss reasons observed live: Anthropic returned tools_changed, system_changed, model_changed, messages_changed and previous_message_not_found. OpenAI returned cache_hit, cache_miss/model_changed, comparison_response_not_found and unavailable. On OpenAI, adding a tool gave unavailable rather than tools_changed (best-effort, as documented).
Tests
-
Recorded: an Anthropic two-turn clean hit followed by a deliberate
tools_changedmiss, plus a streamedtools_changed; Anthropic with a foreign baseline (another provider's ID, or an OpenRoutergen-ID), which sendsnull; an OpenAI GPT-5.6 two-turncache_hitfollowed by amodel_changedswitch underopenai_store=False, plus a streamedcache_hit; and OpenAIgpt-5.5, where the field is not sent. Request bodies are asserted withrequest_capture. -
Mocked: the Anthropic client-class gating (Bedrock, Mantle, Vertex, Foundry and default-off); an Anthropic
pendingresult; an Anthropic other provider with amsg_ID; and OpenAI option merging, the opt-out, another provider's ID, an Azureresp_ID, a non-resp_ID, no history, and OpenRouter. -
Mutations that make at least one test fail: dropping the
msg_orresp_prefix check; dropping the provider-name match; dropping the client-class gate; dropping streaming or non-streaming recording on either side; flipping the OpenAI default; defaulting the profile flag to on. -
Related to #6534
Checklist
- No breaking changes in accordance with the version policy.
- Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver.
- PR title is fit for the release changelog.
🤖 Generated with Claude Code