feat(profiles): `default_cache_retention` + `prompt_cache_outlook()` cold-window helper; rename `resolve_prompt_cache_retention()` to `resolve_cache_retention()`

Merged
DO
2mo ago
+826-52

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

  1. 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 (24h without 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' + explicit retention=
  2. prompt_cache_outlook(messages, *, profile | retention, now=None) -> 'warm'|'cold'|'unknown' in pydantic_ai.profiles — pure, stateless, callable with just a history. The idle clock anchors on the most recent ModelResponse timestamp — 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. Injectable now for testability; naive timestamps treated as UTC.
  3. CachePoint TTLs are respected: a CachePoint(ttl='1h') in the history extends the effective boundary to its TTL (assuming the serving provider honored it).
  4. 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', …) or None when they request nothing; the profile field is the provider default for that None case. prompt_cache_outlook(retention=...) replaces only the profile default, and cache points extend whichever applies, so a caller with a model in hand passes retention=model.resolve_cache_retention(model_settings) and gets the right boundary in every case. (Previously an explicit retention= also overrode cache-point TTLs; it no longer does, so a settings-resolved 5m next to a CachePoint(ttl='1h') can't produce a false 'cold'.) The docs example is now a before_model_request hook, which has both the model and the request's settings.
  5. 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 in Model.__init_subclass__: it gets a PydanticAIDeprecationWarning pointing at the class statement, and resolve_cache_retention is routed to its override. The deprecated base implementation, when reached through such an override's super() call, continues with the implementation above the outermost legacy override rather than dispatching on self (which would recurse). Tested both directions: old-name override honored under the new name (also through WrapperModel), 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.
  6. WrapperModel now forwards resolve_cache_retention(): Model's base implementation returns None, 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