Skip to content

Commit 9a71624

Browse files
authored
fix(provider): scope thinking binding to Claude 5.1+ (anomalyco#46848)
1 parent ffbdee7 commit 9a71624

2 files changed

Lines changed: 173 additions & 29 deletions

File tree

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -684,35 +684,39 @@ function anthropicOmitsThinking(apiId: string) {
684684
return anthropicUsesModernAdaptiveThinking(apiId)
685685
}
686686

687-
// Opus 5, Sonnet 5, Fable 5.x, and Mythos 5.x think without a `thinking` parameter.
688-
function anthropicThinksByDefault(apiId: string) {
689-
const version = /claude-(?:[a-z]+-)?(\d+)(?:[.-](\d{1,2}))?(?:[.@-]|$)/i.exec(apiId)
687+
// Default to binding controls for Claude 5.1+ as enforcement expands to later models.
688+
// Mythos 5.1 explicitly does not run the conversation-prefix check.
689+
// https://platform.claude.com/docs/en/build-with-claude/thinking#preserved-in-conversation
690+
function anthropicBindsThinking(apiId: string) {
691+
// Capture either family/version order, without reading release dates as minor versions.
692+
const version = /claude-(?:([a-z]+)-)?(\d+)(?:[.-](\d{1,2}))?(?:-([a-z]+))?(?:[.@-]|$)/i.exec(apiId)
690693
if (!version) return false
691-
return Number(version[1]) >= 5
694+
const major = Number(version[2])
695+
const minor = Number(version[3] ?? 0)
696+
if (major === 5 && minor === 1 && (version[1] ?? version[4])?.toLowerCase() === "mythos") return false
697+
return major > 5 || (major === 5 && minor >= 1)
692698
}
693699

694700
// Fable 5.1 binds each thinking signature to the system prompt, tool list, and
695701
// messages above it, and rejects the request when any of that changes. opencode
696702
// re-renders parts of that prefix between turns (system prompt, tools, compaction),
697703
// so ask the API to drop the affected blocks instead of failing the request.
698-
// Models that do not run the check accept the field, so it is safe on every Claude.
704+
// Older model deployments may reject this field, even with thinking enabled.
699705
// The patched AI SDK adds the thinking-binding-controls beta whenever it is set.
700706
const ANTHROPIC_BLOCK_BINDING = { prefixMismatchBehavior: "drop_block" }
701707

702708
function anthropicBlockBinding(model: Provider.Model, options: { [x: string]: any }) {
703-
if (!model.api.id.toLowerCase().includes("claude")) return options
704-
const byDefault = anthropicThinksByDefault(model.api.id)
709+
if (!anthropicBindsThinking(model.api.id)) return options
705710
switch (model.api.npm) {
706711
case "@ai-sdk/anthropic":
707712
case "@ai-sdk/google-vertex/anthropic": {
708-
const thinking = options.thinking ?? (byDefault ? { type: "adaptive" } : undefined)
709-
if (!thinking || (thinking.type !== "adaptive" && thinking.type !== "enabled")) return options
713+
const thinking = options.thinking ?? { type: "adaptive" }
714+
if (thinking.type !== "adaptive" && thinking.type !== "enabled") return options
710715
return { ...options, thinking: { ...thinking, blockBinding: ANTHROPIC_BLOCK_BINDING } }
711716
}
712717
case "@ai-sdk/amazon-bedrock": {
713-
const reasoningConfig = options.reasoningConfig ?? (byDefault ? { type: "adaptive" } : undefined)
714-
if (!reasoningConfig || (reasoningConfig.type !== "adaptive" && reasoningConfig.type !== "enabled"))
715-
return options
718+
const reasoningConfig = options.reasoningConfig ?? { type: "adaptive" }
719+
if (reasoningConfig.type !== "adaptive" && reasoningConfig.type !== "enabled") return options
716720
return { ...options, reasoningConfig: { ...reasoningConfig, blockBinding: ANTHROPIC_BLOCK_BINDING } }
717721
}
718722
}

‎packages/opencode/test/provider/transform.test.ts‎

Lines changed: 157 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -851,39 +851,179 @@ describe("ProviderTransform.providerOptions", () => {
851851
const binding = { prefixMismatchBehavior: "drop_block" }
852852
const claude = (npm: string, id: string) =>
853853
createModel({ providerID: "custom", api: { id, url: "https://example.com", npm } })
854+
const sdks = [
855+
{ npm: "@ai-sdk/anthropic", key: "anthropic", option: "thinking" },
856+
{ npm: "@ai-sdk/google-vertex/anthropic", key: "anthropic", option: "thinking" },
857+
{ npm: "@ai-sdk/amazon-bedrock", key: "bedrock", option: "reasoningConfig" },
858+
]
854859

855860
test("adds blockBinding to explicit adaptive thinking on @ai-sdk/anthropic", () => {
856-
const model = claude("@ai-sdk/anthropic", "claude-opus-4-7")
861+
const model = claude("@ai-sdk/anthropic", "claude-fable-5-1")
857862
expect(ProviderTransform.providerOptions(model, { thinking: { type: "adaptive" }, effort: "high" })).toEqual({
858863
anthropic: { thinking: { type: "adaptive", blockBinding: binding }, effort: "high" },
859864
})
860865
})
861866

862-
test("adds blockBinding to explicit enabled thinking", () => {
863-
const model = claude("@ai-sdk/anthropic", "claude-sonnet-4-5")
867+
test("leaves explicit enabled thinking on older models alone", () => {
868+
const model = claude("@ai-sdk/anthropic", "claude-haiku-4-5")
864869
expect(ProviderTransform.providerOptions(model, { thinking: { type: "enabled", budgetTokens: 4000 } })).toEqual({
865-
anthropic: { thinking: { type: "enabled", budgetTokens: 4000, blockBinding: binding } },
870+
anthropic: { thinking: { type: "enabled", budgetTokens: 4000 } },
866871
})
867872
})
868873

869-
test("injects adaptive thinking for models that think by default when no variant is set", () => {
870-
for (const id of ["claude-fable-5-1", "claude-mythos-5-1", "claude-opus-5", "claude-sonnet-5"]) {
871-
const model = claude("@ai-sdk/anthropic", id)
872-
expect(ProviderTransform.providerOptions(model, {})).toEqual({
873-
anthropic: { thinking: { type: "adaptive", blockBinding: binding } },
874+
sdks.forEach((sdk) => {
875+
describe(sdk.npm, () => {
876+
test.each([
877+
"claude-fable-5-1",
878+
"claude-fable-5.1",
879+
"claude-5.1-fable",
880+
"global.anthropic.claude-fable-5-1",
881+
"us.anthropic.claude-fable-5-1-v1:0",
882+
"claude-fable-5-1@default",
883+
"CLAUDE-FABLE-5-1",
884+
"claude-opus-5-1",
885+
"claude-sonnet-5-2",
886+
"claude-mythos-5-2",
887+
"claude-mythos-5-10",
888+
"claude-opus-6",
889+
"claude-6-opus",
890+
"claude-mythos-6-20270901",
891+
])("adds binding for %s", (id) => {
892+
const model = claude(sdk.npm, id)
893+
expect(ProviderTransform.providerOptions(model, {})).toEqual({
894+
[sdk.key]: { [sdk.option]: { type: "adaptive", blockBinding: binding } },
895+
})
896+
expect(
897+
ProviderTransform.providerOptions(model, { [sdk.option]: { type: "adaptive", display: "summarized" } }),
898+
).toEqual({
899+
[sdk.key]: { [sdk.option]: { type: "adaptive", display: "summarized", blockBinding: binding } },
900+
})
901+
expect(
902+
ProviderTransform.providerOptions(model, { [sdk.option]: { type: "enabled", budgetTokens: 4000 } }),
903+
).toEqual({
904+
[sdk.key]: { [sdk.option]: { type: "enabled", budgetTokens: 4000, blockBinding: binding } },
905+
})
906+
expect(ProviderTransform.providerOptions(model, { [sdk.option]: { type: "disabled" } })).toEqual({
907+
[sdk.key]: { [sdk.option]: { type: "disabled" } },
908+
})
874909
})
875-
}
876-
})
877910

878-
test("does not inject thinking for models that are off by default", () => {
879-
for (const id of ["claude-opus-4-7", "claude-opus-4-5", "claude-sonnet-4-6", "claude-haiku-4-5"]) {
880-
const model = claude("@ai-sdk/anthropic", id)
881-
expect(ProviderTransform.providerOptions(model, {})).toEqual({ anthropic: {} })
882-
}
911+
test.each([
912+
"claude-haiku-4-5",
913+
"claude-opus-4-8",
914+
"claude-sonnet-4-6",
915+
"claude-opus-5",
916+
"claude-sonnet-5",
917+
"claude-fable-5",
918+
"claude-opus-5-0",
919+
"claude-opus-5-20260724",
920+
"global.anthropic.claude-opus-5",
921+
"us.anthropic.claude-opus-5",
922+
"claude-sonnet-5@default",
923+
"claude-mythos-5-1",
924+
"claude-mythos-5.1",
925+
"claude-5.1-mythos",
926+
"global.anthropic.claude-mythos-5-1-v1:0",
927+
"claude-mythos-5-1@default",
928+
"CLAUDE-MYTHOS-5-1",
929+
"claude-future",
930+
])("leaves thinking unchanged for %s", (id) => {
931+
const model = claude(sdk.npm, id)
932+
expect(ProviderTransform.providerOptions(model, {})).toEqual({ [sdk.key]: {} })
933+
expect(
934+
ProviderTransform.providerOptions(model, { [sdk.option]: { type: "adaptive", display: "summarized" } }),
935+
).toEqual({ [sdk.key]: { [sdk.option]: { type: "adaptive", display: "summarized" } } })
936+
expect(
937+
ProviderTransform.providerOptions(model, { [sdk.option]: { type: "enabled", budgetTokens: 4000 } }),
938+
).toEqual({ [sdk.key]: { [sdk.option]: { type: "enabled", budgetTokens: 4000 } } })
939+
})
940+
941+
test.each([
942+
["claude-opus-5", "default"],
943+
["claude-opus-5", "high"],
944+
["claude-sonnet-5", "default"],
945+
["claude-sonnet-5", "high"],
946+
["claude-mythos-5-1", "default"],
947+
["claude-mythos-5-1", "high"],
948+
["claude-haiku-4-5", "title"],
949+
])("omits binding from the %s %s request body and betas", async (id, mode) => {
950+
const requests: Request[] = []
951+
const capture = Object.assign(
952+
async (...args: Parameters<typeof fetch>) => {
953+
requests.push(new Request(...args))
954+
return Response.json(
955+
sdk.key === "bedrock"
956+
? {
957+
output: { message: { role: "assistant", content: [{ text: "ok" }] } },
958+
stopReason: "end_turn",
959+
usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
960+
}
961+
: {
962+
type: "message",
963+
id: "msg_1",
964+
model: "test-model",
965+
role: "assistant",
966+
content: [{ type: "text", text: "ok" }],
967+
stop_reason: "end_turn",
968+
usage: { input_tokens: 1, output_tokens: 1 },
969+
},
970+
)
971+
},
972+
{ preconnect: () => undefined },
973+
)
974+
const provider =
975+
sdk.key === "bedrock"
976+
? createAmazonBedrock({ apiKey: "test-key", region: "ap-southeast-1", fetch: capture })
977+
: sdk.npm === "@ai-sdk/google-vertex/anthropic"
978+
? createVertexAnthropic({
979+
project: "test-project",
980+
location: "global",
981+
generateAuthToken: async () => "test-token",
982+
fetch: capture,
983+
})
984+
: createAnthropic({ apiKey: "test-key", fetch: capture })
985+
const model = claude(
986+
sdk.npm,
987+
sdk.key === "bedrock"
988+
? `global.anthropic.${id}`
989+
: sdk.npm === "@ai-sdk/google-vertex/anthropic"
990+
? `${id}@default`
991+
: id,
992+
)
993+
const variants = ProviderTransform.variants(model)
994+
const options =
995+
mode === "title"
996+
? ProviderTransform.smallOptions({ ...model, variants })
997+
: mode === "high"
998+
? variants.high
999+
: {}
1000+
await generateText({
1001+
model: provider(model.api.id),
1002+
prompt: "hi",
1003+
maxOutputTokens: 32000,
1004+
providerOptions: ProviderTransform.providerOptions(model, options),
1005+
})
1006+
expect(requests).toHaveLength(1)
1007+
const body = await requests[0].json()
1008+
const fields = sdk.key === "bedrock" ? (body.additionalModelRequestFields ?? {}) : body
1009+
expect(fields.thinking).toEqual(
1010+
mode === "title"
1011+
? { type: "enabled", budget_tokens: 16000 }
1012+
: mode === "high"
1013+
? { type: "adaptive", display: "summarized" }
1014+
: undefined,
1015+
)
1016+
expect(fields.output_config).toEqual(mode === "high" ? { effort: "high" } : undefined)
1017+
expect(fields.anthropic_beta ?? []).not.toContain("thinking-binding-controls-2026-08-01")
1018+
expect(requests[0].headers.get("anthropic-beta")?.split(",") ?? []).not.toContain(
1019+
"thinking-binding-controls-2026-08-01",
1020+
)
1021+
})
1022+
})
8831023
})
8841024

8851025
test("leaves disabled thinking alone", () => {
886-
const model = claude("@ai-sdk/anthropic", "claude-sonnet-5")
1026+
const model = claude("@ai-sdk/anthropic", "claude-fable-5-1")
8871027
expect(ProviderTransform.providerOptions(model, { thinking: { type: "disabled" } })).toEqual({
8881028
anthropic: { thinking: { type: "disabled" } },
8891029
})

0 commit comments

Comments
 (0)