Add unified `cache` setting and `Caching` capability for cross-provider prompt caching

Merged
AD
2mo ago
+5328-245

Adds a unified, opt-in cache setting on ModelSettings and a Caching capability, mirroring unified thinking and Thinking. On the models that need the request to opt into prompt caching (Anthropic incl. the Bedrock, Vertex and Foundry SDK clients, Bedrock Claude and Nova, OpenRouter's Anthropic and Gemini routes, and OpenAI GPT-5.6+), cache=True / Caching() caches the tool definitions, static instructions and the growing conversation, and keeps reading the conversation back on Bedrock after wide parallel-tool-call turns (#9404). Nothing changes for code that doesn't set it.

What changes

Setting and semantics

  • ModelSettings.cache: True | False | '5m' | '30m' | '1h', unset by default. It's resolved in Model.prepare_request(), gated on the profile's supports_cache, so direct model.request() callers get it and each FallbackModel child resolves and snaps it against its own profile.
  • supports_cache means "request-side caching configuration exists". Google and OpenAI before GPT-5.6, which cache implicitly, no longer claim it, so the setting is a no-op there (the old Google warning is gone).
  • ModelRequestParameters.cache is the requested value, like thinking: provider-specific settings win in the adapter without changing it, and False is kept.
  • cache=False is documented as "disable library-managed caching": CachePoints and provider-specific settings still apply.
  • supported_cache_retentions defaults to (), and only tiers a request can ask for and the provider honors are advertised. Google's and OpenRouter Gemini's fictitious '5m' tiers are gone.
  • Provider-specific cache settings take precedence over the unified one whenever any is present, including False.

Caching only the stable prefix: cache also accepts a CacheConfig (retention, messages). With messages=False (or Caching(messages=False)) only the tool definitions and static instructions are cached, for many one-off conversations that share long instructions or tools: instruction and tool breakpoints instead of Anthropic's automatic caching, no conversation breakpoint (or lookback breakpoint) on Bedrock and OpenRouter, and mode='explicit' with the instruction breakpoint on GPT-5.6. A config that still caches the conversation resolves to the same plain value as before.

Caching capability: Caching(retention=True, *, messages=True), fixed id='caching', contributes ModelSettings(cache=...), registered in CAPABILITY_TYPES for specs, documented on a new Prompt Caching page (docs/capabilities/caching.md) and in the building-pydantic-ai-agents skill.

Per provider (cache=True)

RouteTranslation
Anthropic API, Foundryanthropic_cache='5m' (automatic caching)
Anthropic on AsyncAnthropicBedrock/AsyncAnthropicVertexanthropic_cache_instructions, _tool_definitions, _messages
Bedrock Converse Claude/Novabedrock_cache_instructions, _tool_definitions, _messages
OpenRouter Anthropic/Geminiopenrouter_cache_instructions, _tool_definitions, _messages
OpenAI GPT-5.6+ (OpenAI API)openai_prompt_cache_options={'mode': 'implicit', 'ttl': '30m'} + openai_cache_instructions=True (#7559's chaining gates still apply)

Bedrock 20-block lookback (#9404): wherever a moving message breakpoint is placed (Anthropic per-block message caching, bedrock_cache_messages, openrouter_cache_messages), a turn that adds 18+ blocks since the previous request's tail also gets a breakpoint on that tail, so the previous write is an explicit breakpoint rather than something the lookback has to reach. The helper is shared in models/_prompt_cache.py; the shared 4-breakpoint budget helper drops the oldest message breakpoints first.

Cache health

  • resolve_cache_retention() reflects the effective unified retention (the shortest tier for True, GPT-5.6's 30 minutes), composing with #9851, so WarnOnCacheBusts and instrumentation classify against what the request asked for.
  • New: when a request of at least 4,096 input tokens goes to a model with supports_cache that has no caching configured at all (no unified or provider-specific cache setting, no CachePoint) and reports zero cache usage, instrumentation sets pydantic_ai.cache.not_enabled and emits a pydantic_ai.cache.not_enabled span event, and WarnOnCacheBusts warns CacheNotEnabledWarning pointing to cache=True / Caching(), once per conversation and model. Any explicit setting, including cache=False, counts as a decision and isn't reported.

Recorded evidence

  • Anthropic, cache=True (claude-sonnet-5): request 1 writes 7,836 tokens, request 2 reads 7,836 ('1h': 7,839 → 7,839).
  • Bedrock, cache=True, direct model.request() (claude-sonnet-4-5): after a turn with 12 parallel tool calls (25 blocks), request 2 reads the whole 7,746-token prefix. A live probe without the extra breakpoint read 0 (and wrote 8,345) at both 12 and 30 calls; on claude-sonnet-5 the miss didn't reproduce.
  • OpenAI GPT-5.6 Responses, cache=True: request 1 writes 4,026 tokens, request 2 reads 4,026.

Checklist

  • Any AI generated code has been reviewed line-by-line by the human PR author, who stands by it.
  • No breaking changes in accordance with the version policy.
  • PR title is fit for the release changelog.

šŸ¤– Generated with Claude Code