Add unified `cache` setting and `Caching` capability for cross-provider prompt caching
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 inModel.prepare_request(), gated on the profile'ssupports_cache, so directmodel.request()callers get it and eachFallbackModelchild resolves and snaps it against its own profile.supports_cachemeans "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.cacheis the requested value, likethinking: provider-specific settings win in the adapter without changing it, andFalseis kept.cache=Falseis documented as "disable library-managed caching":CachePoints and provider-specific settings still apply.supported_cache_retentionsdefaults 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)
| Route | Translation |
|---|---|
| Anthropic API, Foundry | anthropic_cache='5m' (automatic caching) |
Anthropic on AsyncAnthropicBedrock/AsyncAnthropicVertex | anthropic_cache_instructions, _tool_definitions, _messages |
| Bedrock Converse Claude/Nova | bedrock_cache_instructions, _tool_definitions, _messages |
| OpenRouter Anthropic/Gemini | openrouter_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 forTrue, GPT-5.6's 30 minutes), composing with #9851, soWarnOnCacheBustsand 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_cachethat has no caching configured at all (no unified or provider-specific cache setting, noCachePoint) and reports zero cache usage, instrumentation setspydantic_ai.cache.not_enabledand emits apydantic_ai.cache.not_enabledspan event, andWarnOnCacheBustswarnsCacheNotEnabledWarningpointing tocache=True/Caching(), once per conversation and model. Any explicit setting, includingcache=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, directmodel.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; onclaude-sonnet-5the 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