Add a general `Caching` docs page covering how providers cache, cost, invalidation and monitoring

Merged
DO
2mo ago
+302-98
  • Part of #6528 (slice D, the prompt caching contract page)

Turns docs/capabilities/caching.md into the general guide to prompt caching, structured like the Thinking page and retitled Caching. It tells users to add Caching() to their agents. Caching can't be on by default, because cache writes cost more than uncached input, but an agent without it pays full price on every request to providers that only cache when asked. The separate docs/prompt-caching.md this PR originally added is folded into the page. Each fact appears in one place.

Page outline:

  1. Intro: what prompt caching is; the prefix rule (a hit reuses an unchanged beginning of the current request; appending is fine, earlier changes break it); and the recommendation to add Caching(), with messages=False for agents making one-off requests.
  2. Unified caching settings: Caching() and the cache=True equivalent, with a compact table of what it does per provider: nothing cached without it / implicit plus extra breakpoints / no effect. Subsections:
    • Configuring caching: values, retention (TTL) guidance, precedence, cache=False.
    • Caching only the stable prefix: a runnable example with long shared instructions.
    • Provider translation: the single place that says what the unified setting sends to each provider.
    • Going beyond the unified setting: when to use CachePoint or provider-specific settings.
  3. How providers cache: native behavior and API-linked provider-specific settings for Anthropic, Bedrock, OpenAI, OpenRouter (including Gemini's fixed cached system instructions), Google (cached-content resources own instructions and tools) and other providers. Each links to the provider page's caching section.
  4. Cost, scoped to the providers whose write and read prices it lists.
  5. What invalidates a cache, including Anthropic thinking block binding, and Prefix-stability guarantees.
  6. Monitoring cache efficiency: a runnable check that compares two responses' cache_write_tokens and cache_read_tokens (with message_history), usage fields, span signals, Warn On Cache Busts, a debugging order with links to the Anthropic and OpenAI cache diagnostics, and why no warning doesn't prove caching works.
  7. Rules for extension authors, which are also kept in capabilities/AGENTS.md.

Other changes:

  • Agents page: a tip to add Caching() to every agent.
  • building-pydantic-ai-agents skill:
    • The basic agent example includes Caching().
    • A Key Practices bullet says to add it by default.
    • The caching reference uses Caching(), adds retention guidance and a low-hit-rate debugging checklist, and notes that providers like Google ignore CachePoint.
    • COMMON-TASKS.md gets a "Debug Low Cache Hit Rate" entry.
  • Provider pages:
    • The OpenAI page covers implicit caching and retention before GPT-5.6, and links the current developers.openai.com guide anchors.
    • The Anthropic and Bedrock caching examples continue a conversation over a long shared document.
    • The Bedrock page says bedrock_cache_messages can add a second cache point.
    • xAI sticky routing gets its own #cache-sticky-routing section.
  • Logfire: an unexpected collapse is described as a detected miss, not a diagnosed cause, with a pointer to the debugging order.
  • Warn On Cache Busts (page and README): the example combines Caching() with WarnOnCacheBusts(), and the "never warns" scope now only covers CacheBustWarning.
  • Nav: removes the never-published core-concepts/prompt-caching entry, so no redirect is needed, and gives the page the aliases caching and prompt-caching.
  • Tests: tests/test_examples.py gets mock responses for the new runnable examples.

Every claim was checked against main: the unified-setting translation in each model class, precedence keys, the OpenAI GPT-5.6 gating, OpenRouter's Gemini tool-cache gate, Google ignoring CachePoint, bedrock_cache_messages adding the 20-block lookback point, and the instruction sort and prefix-stability tests.

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