Keep `Planning` tool loops in the prompt cache by leading the plan reminder with its `CachePoint`
[!NOTE]
On hold for a native turn-scoped part primitive in core (e.g. SystemPromptPart(..., scope='turn'), rendered as Anthropic's turn-scoped system message where supported, with core anchoring the cache breakpoint). An ephemeral tail still drops earlier thinking blocks on models that bind thinking to the exact preceding history, so Planning will be rebuilt on that primitive instead. The measurements below remain useful evidence.
Planning spliced its plan reminder onto each request's tail and anchored its CachePoint on the last user prompt. In a tool loop the content between that prompt and the reminder is every tool turn since the prompt, and no breakpoint ever covered it. Anthropic's automatic caching wrote its entry at the reminder, which the next request doesn't contain, so cache reads stuck at the last user prompt and the tool loop was re-written to cache on every request. Thanks to @aweis89 for the report, the no-API-key repro and the production numbers in #9275, and for pydantic/pydantic-ai-harness#1125 scoping the old "never invalidates the prompt cache" claim.
The reminder stays ephemeral (per-request, never persisted), but now leads with the CachePoint. Providers attach a leading breakpoint to the content right before it: the last tool result or user prompt, which is exactly what the next request starts with. So each request reads the previous one's content from cache, and only the reminder itself is re-sent uncached. A request with no content before the reminder (an instructions-only first request) gets no breakpoint. SystemReminders already places its breakpoint this way.
Stacked on #9582 (merged into this branch): OpenAI's GPT-5.6+ explicit breakpoints raised on a leading CachePoint, so the OpenAI part of the diff here is that PR's. Once it merges, this diff is only Planning.
Live measurements (real APIs, scripted tool loop, fresh prefix per run)
Claude Haiku 4.5, anthropic_cache=True, write_plan then 4 Ć lookup:
main: read=7718 write=500 | 7718/627 | 7718/754 | 7718/927 | 7718/1059 | 7718/1246 | 7718/1352
this PR: read=8023 write=201 | 8151/201 | 8279/201 | 8407/308 | 8643/204 | 8774/255 | 8955/179
GPT-5.6, explicit breakpoints on, Chat Completions and Responses: reads track the previous request's input on both (Chat 5629 ā 6226, Responses 5930 ā 6649).
Why not persist the reminder
An earlier revision of this PR appended the reminder to history when the plan changed. It fixed caching (Opus 5.5 reads tracked the full input), but it put a text block right after tool results on some turns and not others. In live runs that made Claude Haiku 4.5 and Sonnet 4.6 return empty responses on the following turns: 2 output retries per run in 3/3 Haiku runs, one of which exhausted its retries, and 2 on Sonnet 4.6, against 0 on main. Opus 5.5 was unaffected. This revision: 0 retries in 3 Haiku runs and 1 Sonnet run. It also leaves history unchanged, so there is no compatibility impact.
Tests:
- The reminder leads with a
CachePointin a tool loop, including for retry-prompt results. - No breakpoint without content to attach to.
- Nothing is persisted: no reminder in history and no user text after a tool result.
- A streamed Anthropic wire test checks that the breakpoint is on the block right before the reminder (a
tool_result) and that each request's cached prefix is a prefix of the next.
Four of them fail on main. They catch: always leading with a breakpoint, never leading with one, dropping the retry-prompt case, and the wrong TTL.
The reminder is still a hand-rolled ephemeral tail, so it can move to a core primitive for one-turn messages if one is added.
- Closes #9275
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