feat(profiles): `default_cache_retention` + `prompt_cache_outlook()` cold-window helper; rename `resolve_prompt_cache_retention()` to `resolve_cache_retention()`
What
The data primitive for cache-cold-window scheduling: a documented provider prompt-cache retention window on ModelProfile, plus a pure helper to compute whether a message history's cached prefix is likely still warm. No behavior change anywhere — pure data + pure function.
Motivation: part of the cache-prefix-stability track (#6528). When a session has been idle past the provider's cache retention window, the next request pays full input price anyway, making history-mutating work (pruning, compaction, repair) effectively free at that moment (the Reasonix cold-resume pattern). See the framework-contract note §3.
How
ModelProfile.default_cache_retention: timedelta | None— how long after the last request the provider can still be expected to have the cached prefix available when the request doesn't ask for a specific retention. Populated documented-values-only, at the provider layer (retention is provider infrastructure, not a model-family fact — model profile functions never set it, so gateways and hosts don't inherit figures that don't apply to them):- Anthropic: 5m (covers direct API, Bedrock, and Vertex SDK paths — the cache is Anthropic-managed)
- OpenAI: 30m for GPT-5.6 and later — the models with explicit cache breakpoints (a documented, model-determined floor). Earlier models are left unset: OpenAI's retention default follows the organization's zero-data-retention configuration (
24hwithout ZDR,in_memory— 5–10 minutes — with it), which isn't knowable from the model, so there's no honest boundary to record - Bedrock: 5m for Claude, 30m for GPT-5.6, unset elsewhere (no documented TTL)
- OpenRouter: 5m for the providers whose cache control it passes through and documents (Anthropic, Gemini)
- Azure: unset for the same reason — retention spans in-memory (5–10 min, hard-removed within 1h) to 24h extended retention, which newer models use by default; there is no single honest figure, so
'unknown'+ explicitretention=
prompt_cache_outlook(messages, *, profile | retention, now=None) -> 'warm'|'cold'|'unknown'inpydantic_ai.profiles— pure, stateless, callable with just a history. The idle clock anchors on the most recentModelResponsetimestamp — the provider's last confirmed use of the cache. Requests are deliberately not considered: inside an agent run, the just-appended request is timestamped before history processors see it, so anchoring on it would make the history look permanently warm, and a request with no response after it never touched the provider's cache. Injectablenowfor testability; naive timestamps treated as UTC.CachePointTTLs are respected: aCachePoint(ttl='1h')in the history extends the effective boundary to its TTL (assuming the serving provider honored it).- Composes with
Model.resolve_cache_retention()(#7254, which landed while this PR was open): the resolver returns the retention requested by settings (anthropic_cache='1h',openai_prompt_cache_retention='24h', …) orNonewhen they request nothing; the profile field is the provider default for thatNonecase.prompt_cache_outlook(retention=...)replaces only the profile default, and cache points extend whichever applies, so a caller with a model in hand passesretention=model.resolve_cache_retention(model_settings)and gets the right boundary in every case. (Previously an explicitretention=also overrode cache-point TTLs; it no longer does, so a settings-resolved5mnext to aCachePoint(ttl='1h')can't produce a false'cold'.) The docs example is now abefore_model_requesthook, which has both the model and the request's settings. - Renames
Model.resolve_prompt_cache_retention()→Model.resolve_cache_retention(), keeping the old name as a deprecated alias (it shipped in 2.x). Pydantic AI only calls the new name, so a subclass that overrides only the old name is detected inModel.__init_subclass__: it gets aPydanticAIDeprecationWarningpointing at the class statement, andresolve_cache_retentionis routed to its override. The deprecated base implementation, when reached through such an override'ssuper()call, continues with the implementation above the outermost legacy override rather than dispatching onself(which would recurse). Tested both directions: old-name override honored under the new name (also throughWrapperModel),super()from one and two stacked legacy overrides, a new-name override below a legacy one, old-name callers reaching a new-name override, and a class overriding both. WrapperModelnow forwardsresolve_cache_retention():Model's base implementation returnsNone, so__getattr__never forwarded it and any wrapped model silently lost its settings-requested retention.
Semantics: expectation boundary, biased high
The only actionable outlook is 'cold' ("safe to mutate history — the cache is gone anyway"), so a too-low retention figure is the harmful error: it declares cold while the provider still holds the prefix, and the caller mutates away a live cache hit. When a provider documents a range, we therefore take the higher end — a false 'warm' merely defers maintenance, a false 'cold' costs money. When the figure depends on account configuration rather than the model (OpenAI pre-5.6, Azure), no value is honest in either direction, so the field stays unset: None/'unknown' means "treat like warm, never mutate on a guess", unless settings request a retention (resolved by Model.resolve_cache_retention()) or the user passes retention= explicitly.
Naming: no prompt_ prefix on ModelProfile cache fields, since everything in that family is about prompt caching. default_cache_retention (what you get if you ask for nothing) is meant to read alongside #7560's supported_cache_retentions (what you may request), and sits in the same slot of the TypedDict.
Deliberate scope
No RunContext.cache, no scheduler, no hooks — those layer on later per #6528.
Tests
Provider-level retention assertions (Anthropic, OpenAI GPT-5.6 vs. unset-pre-5.6, Bedrock incl. the gpt-oss negative case, OpenRouter incl. Gemini, Azure-unset), leak-regression guards on the model-family profile functions, warm/cold/unknown boundaries with injected now, response-anchor regressions (trailing fresh request, request-only history), CachePoint extension/precedence cases, resolution-matrix snapshots regenerated (they show gateways not inheriting retention), outlook × resolver composition, and the deprecation shim cases above. All new branches are mutation-tested.
🤖 Generated with Claude Code