Repository navigation
fix(ai): layer prompt cache breakpoints - #38725
Merged
Merged
Conversation
kitlangton
enabled auto-merge (squash)
July 24, 2026 17:54
jinhuang712
pushed a commit
to jinhuang712/opencode
that referenced
this pull request
Sep 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Use Anthropic and Bedrock's four cache breakpoints as layered reusable prefixes:
This preserves tool-prefix reuse, base-agent reuse across matching sessions, full-system reuse within a project, and rolling reuse through long tool loops.
Before / After
Before
cache: "auto"marked the last tool, only the last system block, and the latest user message. The missing first-system boundary prevented reuse of the stable base agent independently from project instructions. During long tool loops, the fixed user-message marker eventually fell beyond Anthropic and Bedrock's 20-block lookback, forcing the large prefix to be rewritten.After
The first and distinct last system blocks expose both stable system layers. The conversation marker advances to the final message on every request, keeping the previous marker near the tail as tool results accumulate. A single system block is marked once, and existing manual hints count against the shared four-marker budget before automatic placement.
The recorded 11-call regression reads 12,288 cached tokens on the appended request and writes only 19 new tokens.
How
packages/ai/src/cache-policy.tsadds first/last system placement, preserves the rolling one-message tail, and budgets automatic markers after counting manual hints.packages/ai/src/schema/options.tsandpackages/ai/README.mddocument the four reusable prefix layers and deterministic tool-order requirement.packages/ai/test/cache-policy.test.tsverifies Anthropic and Bedrock wire placement, one-system deduplication, direct policy-level budgeting, manual-hint preservation, and idempotent reapplication.packages/ai/test/provider/anthropic-messages-cache.recorded.test.tsaccepts either a cold cache write or a warm cache read on the first request while requiring the appended request to reuse the established prefix.packages/ai/test/provider/bedrock-converse.test.tsopts an unrelated media-lowering assertion out of default cache placement.Scope
This PR changes only the Anthropic/Bedrock automatic cache-breakpoint policy. Tool definitions are already deterministically sorted by Core's tool registry; this PR does not change tool registration or ordering. It deliberately excludes GPT-Sol control coverage, request-prefix diagnostics, and TUI cache-reuse reporting.
Testing
bun run testinpackages/ai: 446 passed, 28 skipped, 0 failedbun typecheckinpackages/ai: passedbun turbo typecheck --concurrency=3: 33 tasks passedgit diff --check: passedNo cassette re-recording was needed: the recorded long-tool request has one system block, so first/last system deduplication leaves its wire body unchanged.
Flow