Skip to content

Use 4 cache_control breakpoints for Anthropic Messages API#315505

Merged
bhavyaus merged 2 commits intomainfrom
dev/bhavyau/messages-api-4-cache-breakpoints
May 10, 2026
Merged

Use 4 cache_control breakpoints for Anthropic Messages API#315505
bhavyaus merged 2 commits intomainfrom
dev/bhavyau/messages-api-4-cache-breakpoints

Conversation

@bhavyaus
Copy link
Copy Markdown
Collaborator

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_control markers — exactly Anthropic's per-request limit.

Background

The Messages API allows up to 4 cache_control breakpoints per request. We were previously using 3:

Breakpoint Status
Last non-deferred tool definition fixed
Last system block fixed
Last cacheable block of the most recent message sliding

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:

tools[last]   ← fixed
system[last]  ← fixed
messages[N-1] ← sliding (fallback)
messages[N]   ← sliding (tail)

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.ts updated and passes (45 tests).
  • TypeScript clean.
  • The total of exactly 4 markers is asserted in the existing caps total cc count at 4 test.

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.
Copilot AI review requested due to automatic review settings May 10, 2026 01:06
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread extensions/copilot/src/platform/endpoint/node/messagesApi.ts Outdated
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 10, 2026

Base: 59f55282 Current: 2b7e3da1

No screenshot changes.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@bhavyaus bhavyaus merged commit a7221e5 into main May 10, 2026
25 checks passed
@bhavyaus bhavyaus deleted the dev/bhavyau/messages-api-4-cache-breakpoints branch May 10, 2026 03:47
@vs-code-engineering vs-code-engineering Bot added this to the 1.120.0 milestone May 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants