Record prompt cache diagnostics in `provider_details`: on by default for OpenAI Responses, `anthropic_cache_diagnostics` opt-in

Merged
DO
2mo ago
+3888-7

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 under provider_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 new openai_prompt_cache_diagnostics=False setting turns it off. A new profile flag, openai_responses_supports_prompt_cache_diagnostics, is set only by OpenAIProvider.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=True setting sends diagnostics: {previous_message_id: <latest msg_ id or null>}. The answer is recorded under provider_details['cache_diagnostics'] as Anthropic's diagnostics object: {'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_ or resp_). 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, because response.in_progress reports unavailable before 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:

  1. 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 400 error". 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.
  2. 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 diagnosticsturn 2 diagnosticsturn 1 cache writeturn 2 cache readturn 2 diagnostics
omittedomitted6424 / 64236424 / 6423null
nullprevious id6425 / 64226425 / 6422null
nullomitted6421 / 64256421 / 6425null
omittedprevious id6424 / 64236424 / 6423previous_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_optionsturn 2 comparison_response_idturn 2 cached tokensturn 2 diagnostics
omittedomitted3175 / 3177none
omittedset3176 / 3178cache_hit
setset3178 / 3176cache_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)

providerwithwithout
Anthropic claude-haiku-4-5, N=10 eachmedian 0.510s, mean 0.520s, stdev 0.043smedian 0.508s, mean 0.517s, stdev 0.046s
OpenAI gpt-5.6-sol, N=16 eachmedian 1.241s, mean 1.286s, max 1.851smedian 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

targetresult
Anthropic API: claude-opus-5-5, claude-sonnet-5, claude-sonnet-4-5, claude-haiku-4-5accepted (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 / Foundrynot tested (no model access or key); documented as "not available"
Pydantic AI Gateway, Anthropic routeaccepted; real diagnostics
Anthropic-compatible: OpenRouter /api/v1/messages, xAIsilently ignored (IDs are gen-… / UUIDs)
Anthropic API with resp_…, chatcmpl-…, empty or over-256-char ID400 must be the id from a prior /v1/messages response (starts with msg_)
Anthropic API with an unknown msg_… ID200, previous_message_not_found
OpenAI Responses: gpt-5.5, gpt-5.4, gpt-5-mini, gpt-4.1-mini, gpt-4o-mini, o4-miniaccepted, always unavailable
OpenAI Responses: gpt-5.6-sol, gpt-5.6-lunaaccepted; real diagnostics
OpenAI Chat Completions (via extra_body) on gpt-5.6-sol, gpt-5.5, gpt-4.1-mini400 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 routeaccepted; real diagnostics
Azure OpenAInot tested (the key in .env returns 401)
OpenAI with a msg_… / chatcmpl-… ID400 Expected an ID that begins with 'resp'
OpenAI with an unknown resp_… ID200, comparison_response_not_found

ID requirements and model switches

  • OpenAI: store=false works on either side. The docs say ZDR is compatible.
  • Switching to another model of the same provider (as FallbackModel does) works: Anthropic returns model_changed for haiku-4-5 → sonnet-4-5, and OpenAI returns cache_miss / model_changed for 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-07 is no longer required.
  • SDK support: anthropic>=1.9.0 and openai>=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_changed miss, plus a streamed tools_changed; Anthropic with a foreign baseline (another provider's ID, or an OpenRouter gen- ID), which sends null; an OpenAI GPT-5.6 two-turn cache_hit followed by a model_changed switch under openai_store=False, plus a streamed cache_hit; and OpenAI gpt-5.5, where the field is not sent. Request bodies are asserted with request_capture.

  • Mocked: the Anthropic client-class gating (Bedrock, Mantle, Vertex, Foundry and default-off); an Anthropic pending result; an Anthropic other provider with a msg_ ID; and OpenAI option merging, the opt-out, another provider's ID, an Azure resp_ ID, a non-resp_ ID, no history, and OpenRouter.

  • Mutations that make at least one test fail: dropping the msg_ or resp_ 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