Repository navigation
fix(anthropic): new sessions rewrite the whole dynamic system prompt to the prompt cache - #196461
scoootscooob wants to merge 2 commits into
Conversation
…to the prompt cache The Anthropic transport puts one cache checkpoint after the stable system prefix and leaves the dynamic suffix (tooling, skills, memory, messaging) to the history checkpoints. That suffix ends with the Runtime facts, which name the session, so every new session wrote all of it again even though the text before the Runtime facts is the same for every session of an agent. Split the suffix at the Runtime region and, on a session's opening request only, spend one of the two history checkpoints on the part before it. Later requests keep both history checkpoints and send the same blocks unmarked. Co-Authored-By: Claude Fable 5.2 <[email protected]>
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
… system-block test The provider test that pins native Anthropic system blocks expected the old two-block layout. Assert the new layout there, for an opening request and for a request with history, instead of adding a separate test, so the test-line ceiling is unchanged. Co-Authored-By: Claude Fable 5.2 <[email protected]>
|
Thanks, Scott. The checkpoint placement is useful, but the split currently trims the newline between the shared guidance and |
|
Codex review: needs maintainer review before merge. What this changesThis PR checkpoints reusable dynamic system text before session-specific Runtime facts on opening Anthropic requests. Example: Sessions alpha and beta use identical agent instructions within the provider cache lifetime.
Review scores
ProductKind: Performance · Worth it: Yes · Fix scope: Complete Merge readiness✅ Ready for maintainer review Keep open: this maintainer-authored PR addresses a current cache-write inefficiency with a focused change; no introduced production correctness or security defect was found. Priority: P2 Before mergeNone. FindingsNone. Tests
Agent review detailsHow this fits togetherThe Anthropic payload policy converts prepared system prompts, tools, and conversation history into provider request blocks and allocates their four cache checkpoints. flowchart LR
A[Prepared agent prompt] --> B[Anthropic payload policy]
C[Tools and conversation history] --> B
B --> D[Stable system prefix]
B --> E[Shared dynamic instructions]
B --> F[Session Runtime facts]
D --> G[Anthropic request]
E --> G
F --> G
Technical reviewBest possible solution: Keep the existing payload owner and split only at the producer-delimited Runtime boundary, retaining both history checkpoints after assistant history exists. Do we have a high-confidence way to reproduce the issue? Current-main source places shared dynamic instructions and session-specific Runtime facts in the same unchecked suffix, matching the reported cross-session rewrite mechanism; no target code was executed during this read-only review. Is this the best way to solve the issue? The change uses the existing marker parser, prepared-prompt lifetime, and checkpoint allocator without relocating instructions or adding configuration; the added state is weakly held eligibility metadata rather than another prompt cache. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against a7ba22b91b85. Provenance checked
TestingProof path: in-process harness. SecurityNone. EvidenceWhat I checked:
Likely related people:
PR surfaceSource +42, Tests -2. Total +40 across 2 files. Added test files: 0. View PR surface stats
Preflight sourceSource unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified. Review metrics
LabelsLabel changes:
Label justifications:
Rank-up movesOptional improvements that raise the rating; they are not merge blockers.
Rating scale6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior. WorkflowClawSweeper edits this one comment on every review. Comment Reviewed November 10, 2026, 12:58 AM ET / 05:58 UTC. |
What Problem This Solves
Fixes: on Anthropic models, every new session writes the whole dynamic part of the system prompt (tooling, skills, memory, messaging guidance, about 27,000 characters in 2026.11.2) to the prompt cache again, even when another session of the same agent sent identical text moments earlier.
User Impact
User impact: starting a session on an Anthropic model costs less when another session of the same agent started recently. Deployments that open many short sessions (one per DM or channel, sub-agents, cron runs, evals) see the largest change. No configuration is needed and nothing changes for other providers.
Tradeoff to be aware of: a session's opening request now has one history checkpoint instead of two. Every later request is unchanged and keeps both.
Why This Change Was Made
The Anthropic transport places one checkpoint after the stable system prefix and leaves the dynamic suffix to the message checkpoints. That suffix ends with the
Runtime:facts, which name the session, so the first cache entry that covers the suffix is always specific to one session.The prompt builder already delimits the Runtime facts with the relocatable boundary. This change splits the dynamic suffix there and, only on a request with no assistant turn yet, spends one of the two history checkpoints on the text before it. All four checkpoints were already in use, so the opening request is the one place a checkpoint can be spared: it has no earlier history write to keep reachable. Once the session has history, the same three system blocks are sent with the checkpoint removed, and those requests still read the cached prefix through the history checkpoints.
Limits:
Evidence
Where it showed up: a 320-trial harness comparison on
claude-haiku-5-5(20 short multi-conversation tasks, 2.25 sessions per trial), with provider-reported usage read at a metering proxy.In the captured request bodies, the second system block of consecutive sessions was byte-identical up to
session=in the Runtime line (offset 26,978 of 27,257 characters in 2026.11.2).Live check of the mechanism against the Anthropic API (
claude-haiku-5-5; system text of about 9,100 + 18,000 tokens, then a Runtime line, then one user message; token counts are the API's):The last row is the property the "opening request only" rule depends on: a later request without the system checkpoint still reads everything.
Tests:
packages/ai/src/providers/anthropic.test.tsnow pins the new layout. It fails onmain's transport and passes with the change. It covers the opening request (three blocks, shared block marked, one history checkpoint) and a request with history (shared block unmarked, two history checkpoints). Net test lines: -2.vitest run packages/ai/src/: 2,402 pass and 6 fail on my machine; the 6 are OpenAI WebSocket retention fixtures that need--expose-gcand fail the same way without the change.oxlintandoxfmt --checkon both files: clean.pnpm tsgo:core: clean.Not done: I have not run an agent built from this branch end to end and measured its cost. The numbers above are from released builds plus the direct API check.
🤖 Generated with Claude Code