Skip to content

fix(ai): enable prompt caching on Azure Foundry Claude models - #66686

Open
kitlangton wants to merge 2 commits into
devfrom
fix-azure-prompt-caching
Open

kitlangton wants to merge 2 commits into
devfrom
fix-azure-prompt-caching

Conversation

@kitlangton

@kitlangton kitlangton commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Claude models on Azure AI Foundry now cache prompts instead of paying full input price on every step.
Foundry serves Claude over Anthropic's Messages wire format, which requires inline cache markers in the request body, and OpenCode skipped Azure's Messages route when placing them. Azure OpenAI Chat and Responses deployments still use Azure's automatic prefix caching and are unchanged, while custom SDK routes built on the Anthropic Messages protocol now also receive inline cache markers by default.

🔍 Origin: reported in #62546 (closes #62546)

❌ Before

  1. You send a step to a Claude deployment on Azure AI Foundry.
  2. OpenCode checks a hand-kept allowlist of route names that omitted azure-anthropic-messages.
  3. Both automatic and explicit cache policies are skipped, so every step resends tools, system instructions, and conversation history uncached.

✅ After

  1. You send a step to a Claude deployment on Azure AI Foundry.
  2. OpenCode checks the route's wire protocol (anthropic-messages) instead of a per-provider route name.
  3. Inline cache_control breakpoints are placed on tools, system boundaries, and the conversation tail, matching every other Anthropic Messages provider.
- allowlist of 15 per-provider route IDs (missing "azure-anthropic-messages")
- check request.model.route.id -> skip Azure Messages
+ allowlist of 9 wire protocol IDs ("anthropic-messages", ...)
+ check request.model.route.protocol -> cache every Anthropic Messages route

📊 Proof

Case Before After
Azure Foundry Claude default ("auto") prompt caching ❌ No cache_control markers ✅ Marks tools, system, and tail
Custom SDK routes built on anthropic-messages ("auto" and { tools: true, ttl: "1h" }) ❌ Skipped when given a new route ID ✅ Marks breakpoints with requested TTL
Azure OpenAI Chat and Responses routes ✅ No inline markers ✅ No inline markers

Both new tests in packages/ai/test/cache-policy.test.ts fail without the cache-policy.ts change (31 pass, 0 fail with it; full @opencode/ai suite 2,310 pass).

👀 Read: packages/ai/src/cache-policy.ts (RESPECTS_INLINE_HINTS and selectPolicy) — keying on route.protocol folds all eight anthropic-messages routes into one entry while keeping openrouter-chat, vercel-ai-gateway-messages, and alibaba-chat on their per-model filters. The rest is routine.

🔍 Also found independently in a code audit

Checks run
  • bun run test (in packages/ai): 2,310 pass, 29 skip, 0 fail
  • bun test test/cache-policy.test.ts --rerun-each 10 --randomize (in packages/ai): 310 pass, 0 fail
  • bun run typecheck (in packages/ai): 0 errors
  • bun run typecheck:tsc (in packages/ai): 0 errors
  • bun run check (repo root): 0 errors

Added by the opencode bot (AI-generated).

Fixes #62546

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good. I enumerated every registered language-model route (route ID and protocol) and checked it against the old route-ID allowlist and the new protocol allowlist: only azure-anthropic-messages changes (it now gets markers). Every other route is unchanged, including the per-model rules for OpenRouter, Vercel AI Gateway and alibaba-chat, and Azure OpenAI Chat and Responses. Both new cache-policy cases fail on the base and pass here. The full packages/ai suite passes (2,310 pass, 0 fail), and typecheck:tsc is clean. I couldn't check it against a live Foundry deployment.

Public API: unchanged. The allowlist is internal, and custom routes built on anthropic-messages with a new ID now get markers by default, as described.

The open PR #62547 fixes the same bug, so one of the two should be closed.

One inline note on the new loop test: the added table entry already covers the fix.

Fixes #62546

Comment thread packages/ai/test/cache-policy.test.ts Outdated
),
)

it.effect("every registered Anthropic Messages route emits inline cache markers", () =>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The azure-anthropic-messages entry added to messagesModels above already covers the fix. This loop test is harder to follow than it needs to be: it dynamically imports every provider, picks routes by a -messages naming heuristic, asserts a magic >= 12 count that will need bumping whenever routes change, and runs two compiles per route. Since the policy is now keyed on protocol, could this be cut down to one explicit case (e.g. a custom AnthropicMessages.route.with({ id: "custom-messages" }) getting markers), or dropped?

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The new commit fixes my earlier test comment: the loop over all registered routes is gone, and one explicit custom anthropic-messages route test replaces it. That test and the Azure case both fail on the base and pass here. The full packages/ai suite passes (2,310 pass, 0 fail), and typecheck:tsc is clean. The public API is unchanged. Nothing new to raise.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Azure: Claude on Microsoft Foundry gets no prompt cache breakpoints

1 participant