Skip to content

Commit f8b4dd7

Browse files
fix(provider): send Anthropic's dashed native slug through the AI Gateway (anomalyco#44281)
Co-authored-by: Claude Sonnet 5 <[email protected]>
1 parent 611cc73 commit f8b4dd7

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

‎packages/opencode/src/provider/provider.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,12 @@ function custom(dep: CustomDep): Record<string, CustomLoader> {
844844
// The passthrough wrappers inject a CF_TEMP_TOKEN sentinel that the gateway strips before
845845
// dispatch, so upstream billing stays on the gateway (Unified Billing / stored BYOK).
846846
if (modelID.startsWith("openai/")) return aigateway(createOpenAI()(modelID.slice("openai/".length)))
847-
if (modelID.startsWith("anthropic/")) return aigateway(createAnthropic()(modelID.slice("anthropic/".length)))
847+
// models.dev lists Anthropic ids with dotted versions (claude-haiku-4.5); Anthropic's
848+
// Messages API expects dashed native slugs (claude-haiku-4-5), so translate before passing.
849+
// No native Anthropic slug contains a dot, so the blanket replacement is lossless here -
850+
// unlike OpenAI above, whose native ids (e.g. gpt-4.1) keep their dots and must not be touched.
851+
if (modelID.startsWith("anthropic/"))
852+
return aigateway(createAnthropic()(modelID.slice("anthropic/".length).replaceAll(".", "-")))
848853
// Workers AI is the only first-party provider whose upstream is Cloudflare itself, so it is
849854
// the only one that should receive the Cloudflare token as its upstream Authorization header.
850855
// The Unified API addresses Workers AI both with the explicit "workers-ai/" prefix and as

‎packages/opencode/test/provider/cf-ai-gateway-e2e.test.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ function extractUpstreamHeaders(body: unknown): Record<string, unknown> | undefi
165165
function gatewayModel(apiId: string, gatewayToken = "test") {
166166
const aigateway = createAiGateway({ accountId: "test", gateway: "test", apiKey: gatewayToken })
167167
if (apiId.startsWith("openai/")) return aigateway(createOpenAI()(apiId.slice("openai/".length)))
168-
if (apiId.startsWith("anthropic/")) return aigateway(createAnthropic()(apiId.slice("anthropic/".length)))
168+
if (apiId.startsWith("anthropic/"))
169+
return aigateway(createAnthropic()(apiId.slice("anthropic/".length).replaceAll(".", "-")))
169170
const isWorkersAi = apiId.startsWith("workers-ai/") || apiId.startsWith("@cf/")
170171
const unified = createUnified(isWorkersAi ? { apiKey: gatewayToken } : {})
171172
return aigateway(unified(apiId))
@@ -195,6 +196,17 @@ describe("cf-ai-gateway routing", () => {
195196
expect(upstream?.model).toBe("claude-sonnet-4-6")
196197
})
197198

199+
test("anthropic/* with a dotted models.dev id reaches Anthropic as a dashed native slug", async () => {
200+
// models.dev ids are dotted (claude-haiku-4.5); Anthropic's Messages API 404s unless the
201+
// version is dashed (claude-haiku-4-5). Regression guard for the dotted-id translation.
202+
await callThroughGateway("anthropic/claude-haiku-4.5", {})
203+
const step = firstStep(captured?.outerBody)
204+
expect(step?.provider).toBe("anthropic")
205+
expect(step?.endpoint).toBe("v1/messages")
206+
const upstream = extractUpstreamQuery(captured?.outerBody)
207+
expect(upstream?.model).toBe("claude-haiku-4-5")
208+
})
209+
198210
test("workers-ai models stay on the unified /compat route", async () => {
199211
await callThroughGateway("workers-ai/@cf/moonshotai/kimi-k2.6", {})
200212
const step = firstStep(captured?.outerBody)

0 commit comments

Comments
 (0)