From 34cf183c4c9088c50699b49eec3fb23490ab1d54 Mon Sep 17 00:00:00 2001 From: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Date: Tue, 6 Oct 2026 23:57:20 -0500 Subject: [PATCH] feat(ai): support between_tools thinking in Anthropic Messages (#53601) --- packages/ai/src/protocols/anthropic-messages.ts | 17 ++++++++++------- packages/ai/src/protocols/bedrock-converse.ts | 4 +++- .../ai/src/providers/amazon-bedrock-mantle.ts | 4 ++-- packages/ai/test/effort-updates.test.ts | 16 ++++++++++++++++ .../provider/anthropic-thinking-binding.test.ts | 3 ++- 5 files changed, 33 insertions(+), 11 deletions(-) diff --git a/packages/ai/src/protocols/anthropic-messages.ts b/packages/ai/src/protocols/anthropic-messages.ts index fd46b61118b..5bb2d27d24d 100644 --- a/packages/ai/src/protocols/anthropic-messages.ts +++ b/packages/ai/src/protocols/anthropic-messages.ts @@ -285,7 +285,7 @@ const AnthropicThinkingEnabled = Schema.Struct({ ...AnthropicThinkingFields, }) const AnthropicThinkingAdaptive = Schema.Struct({ type: Schema.tag("adaptive"), ...AnthropicThinkingFields }) -const AnthropicThinkingDisabled = Schema.Struct({ type: Schema.tag("disabled") }) +const AnthropicThinkingDisabled = Schema.Struct({ type: Schema.Literals(["disabled", "between_tools"]) }) const AnthropicThinking = Schema.Union([AnthropicThinkingEnabled, AnthropicThinkingAdaptive, AnthropicThinkingDisabled]) type AnthropicThinking = typeof AnthropicThinking.Type @@ -1001,18 +1001,21 @@ const lowerMessages = Effect.fnUntraced(function* (request: LLMRequest, breakpoi // TODO: Move per-model capability heuristics (`supportsEffortUpdates`, `supportsNativeSystemUpdates`, // `supportsThinkingBlockBinding`) into explicit model/provider `compatibility` metadata so the protocol // only reads `request.model.compatibility`. +const isThinkingOff = Schema.is(AnthropicThinkingDisabled) + // Per-turn effort started with Claude Opus 5 and every Claude 5.1 model; later versions of any family inherit it. -const supportsEffortUpdates = (model: LLMRequest["model"]) => { - const override = model.compatibility?.supportsEffortUpdates +const supportsEffortUpdates = (request: LLMRequest) => { + if (isThinkingOff(request.providerOptions?.thinking)) return false + const override = request.model.compatibility?.supportsEffortUpdates if (override !== undefined) return override - const version = claudeVersion(model.id) + const version = claudeVersion(request.model.id) if (version === undefined) return false if (version.family === "opus" && version.major >= 5) return true return version.major > 5 || (version.major === 5 && version.minor >= 1) } const applyThinkingBindingDefault = (model: LLMRequest["model"], thinking: AnthropicThinking | undefined) => { - if (thinking?.type === "disabled") return thinking + if (isThinkingOff(thinking)) return thinking if (!supportsThinkingBlockBinding(model)) return thinking return { ...(thinking ?? { type: "adaptive" as const }), @@ -1607,7 +1610,7 @@ export const protocol = Protocol.make({ }), step, }, - supportsEffortUpdates: (request) => supportsEffortUpdates(request.model), + supportsEffortUpdates, }) export const transport = < @@ -1653,7 +1656,7 @@ function requiredBetaHeaders(body: Pick { - const override = request.model.compatibility?.supportsEffortUpdates - if (override !== undefined) return override + if (!(AnthropicMessages.protocol.supportsEffortUpdates?.(request) ?? false)) return false + if (request.model.compatibility?.supportsEffortUpdates !== undefined) return true const version = claudeVersion(request.model.id) return version !== undefined && (version.major > 5 || (version.major === 5 && version.minor >= 1)) }, diff --git a/packages/ai/test/effort-updates.test.ts b/packages/ai/test/effort-updates.test.ts index 3031a988656..5a955fa2224 100644 --- a/packages/ai/test/effort-updates.test.ts +++ b/packages/ai/test/effort-updates.test.ts @@ -268,6 +268,22 @@ describe("Anthropic Messages effort updates", () => { }), ) + it.effect("strips markers when thinking is disabled or between_tools", () => + Effect.gen(function* () { + const sonnet = anthropic("claude-sonnet-5-5") + const betweenTools = yield* compileRequest( + LLM.request({ + model: sonnet, + messages: conversation, + providerOptions: { thinking: { type: "between_tools" }, effort: "low" }, + }), + ) + + expect(systemMessages(betweenTools.body)).toHaveLength(0) + expect(betweenTools.body.output_config).toEqual({ effort: "low" }) + }), + ) + it.effect("strips markers for Opus 5.0 on Bedrock Mantle Messages while lowering Opus 5.5", () => Effect.gen(function* () { const mantle = AmazonBedrockMantle.configure({ apiKey: "test", region: "us-east-1" }) diff --git a/packages/ai/test/provider/anthropic-thinking-binding.test.ts b/packages/ai/test/provider/anthropic-thinking-binding.test.ts index 3df8e873b77..a9d560763fa 100644 --- a/packages/ai/test/provider/anthropic-thinking-binding.test.ts +++ b/packages/ai/test/provider/anthropic-thinking-binding.test.ts @@ -44,6 +44,7 @@ it.effect("preserves explicit thinking settings and combines required beta heade Effect.gen(function* () { for (const thinking of [ { type: "disabled" }, + { type: "between_tools" }, { type: "adaptive", block_binding: { prefix_mismatch_behavior: "error" } }, ] as const) { const request = LLM.request({ @@ -55,7 +56,7 @@ it.effect("preserves explicit thinking settings and combines required beta heade const prepared = yield* AnthropicMessages.route.prepareTransport(compiled.body, request) expect(compiled.body.thinking).toEqual(thinking) expect(prepared.request.headers["anthropic-beta"]).toBe( - thinking.type === "disabled" + thinking.type === "disabled" || thinking.type === "between_tools" ? "interleaved-thinking-2025-05-14,compact-2026-01-12" : "interleaved-thinking-2025-05-14,compact-2026-01-12,thinking-binding-controls-2026-08-01", )