Use 4 cache_control breakpoints for Anthropic Messages API#315505
Merged
Use 4 cache_control breakpoints for Anthropic Messages API#315505
Conversation
Mark the last cacheable block of the two most recent cacheable messages instead of just one. Combined with the existing tools + system breakpoints, this produces 4 cache_control markers — exactly Anthropic's per-request limit. The second (older) anchor provides a fallback within Anthropic's 20-block lookback window: if the tail anchor misses (TTL expiry on a slow tool call, or rare content drift), the older anchor still serves a cache hit covering everything up to it, so we lose at most one exchange instead of resetting the entire conversation cache.
Contributor
There was a problem hiding this comment.
Pull request overview
Updates Anthropic Messages API prompt caching to place cache-control “sliding anchors” on the last cacheable block of the two most recent cacheable messages (instead of just the most recent), improving resilience when the tail anchor misses. This works alongside the existing fixed tool/system breakpoints to stay within Anthropic’s 4-breakpoint per-request limit.
Changes:
- Mark cache_control on the last cacheable block of the two most recent cacheable messages.
- Update/extend unit tests to assert the new 2-message anchoring behavior and the resulting 4-marker layout when combined with tools + system.
Show a summary per file
| File | Description |
|---|---|
| extensions/copilot/src/platform/endpoint/node/messagesApi.ts | Extend message cache-control marking from 1 to 2 most-recent cacheable messages. |
| extensions/copilot/src/platform/endpoint/test/node/messagesApi.spec.ts | Update expectations to validate two-message anchoring and the 4-marker combined layout. |
Copilot's findings
- Files reviewed: 2/2 changed files
- Comments generated: 1
Contributor
|
Base:
|
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
dmitrivMS
approved these changes
May 10, 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.
Summary
Marks the last cacheable block of the two most recent cacheable messages instead of just one. Combined with the existing tools + system breakpoints, this produces 4
cache_controlmarkers — exactly Anthropic's per-request limit.Background
The Messages API allows up to 4
cache_controlbreakpoints per request. We were previously using 3:Anthropic's prompt cache is prefix-based, so a single tail anchor is sufficient under steady-state — the cached prefix grows with each turn. But under failure modes (TTL expiry on a slow tool call, rare content drift, or eviction) the single anchor offers no fallback, and the next turn writes a fresh tail anchor with no nearby cached prefix to latch onto, effectively resetting the conversation cache.
Change
Adds a second sliding anchor on the second-to-last cacheable message:
The fallback anchor lives within Anthropic's 20-block lookback window so a single miss costs at most one exchange instead of the whole conversation.
Validation
messagesApi.spec.tsupdated and passes (45 tests).caps total cc count at 4test.