xonecas.io
64875
Mc

mini-coder / commit 24592bc

fix(thinking): use legacy budget_tokens API for zen claude models

The Zen proxy at opencode.ai/zen/v1 does not support the effort-based
thinking API (effort-2025-11-24 beta), which the @ai-sdk/anthropic SDK
activates when providerOptions.anthropic.effort is set. This caused
requests to zen/claude-haiku-4-5 (and other zen claude models) to fail
with an error while the same model worked fine on anthropic/claude-haiku-4-5.

Fix: detect provider === 'zen' in getAnthropicThinkingOptions and use
the legacy { thinking: { type: 'enabled', budgetTokens: N } } format
instead of { thinking: { type: 'adaptive' }, effort } which triggers
the unsupported beta.

Also update all three thinking strategy builders to accept the full
modelString (instead of just modelId) so they can inspect the provider
when needed, matching the ThinkingStrategy interface.

Add regression tests that verify zen claude gets enabled+budgetTokens
and direct anthropic gets adaptive+effort.

xonecas2026-03-21 24592bcdbfd1cb5bb0c1ff3f7d27339ebcebd999 parent b68d5d7

2 files changed

src/llm-api/provider-options.ts+35−6
@@ -8,6 +8,16 @@ import {
88
99 export type ThinkingEffort = "low" | "medium" | "high" | "xhigh";
1010
11+// Budget tokens used when routing through Zen (which only supports the
12+// legacy `thinking: { type: "enabled", budget_tokens: N }` API, not the
13+// newer effort-based API that requires the `effort-2025-11-24` beta).
14+const ANTHROPIC_ZEN_BUDGET: Record<ThinkingEffort, number> = {
15+ low: 4_096,
16+ medium: 8_192,
17+ high: 16_384,
18+ xhigh: 32_768,
19+};
20+
1121 type CacheFamily = "google" | "anthropic" | "none";
1222
1323 const GEMINI_BUDGET: Record<ThinkingEffort, number> = {
@@ -28,9 +38,24 @@ function clampEffort(
2838 }
2939
3040 function getAnthropicThinkingOptions(
31- modelId: string,
41+ modelString: string,
3242 effort: ThinkingEffort,
3343 ): Record<string, unknown> {
44+ const { provider, modelId } = parseModelString(modelString);
45+ // Zen proxies claude models but only supports the legacy
46+ // `thinking: { type: "enabled", budget_tokens: N }` API.
47+ // The newer effort-based API adds an `effort-2025-11-24` beta header and
48+ // sends `output_config: { effort }` which Zen rejects.
49+ if (provider === "zen") {
50+ return {
51+ anthropic: {
52+ thinking: {
53+ type: "enabled",
54+ budgetTokens: ANTHROPIC_ZEN_BUDGET[effort],
55+ },
56+ },
57+ };
58+ }
3459 const isOpus = /^claude-opus-4/.test(modelId);
3560 const xhighMapping = isOpus ? "max" : "high";
3661 const mapped = effort === "xhigh" ? xhighMapping : effort;
@@ -38,18 +63,20 @@ function getAnthropicThinkingOptions(
3863 }
3964
4065 function getOpenAIThinkingOptions(
41- modelId: string,
66+ modelString: string,
4267 effort: ThinkingEffort,
4368 ): Record<string, unknown> {
69+ const { modelId } = parseModelString(modelString);
4470 const supportsXhigh = /^gpt-5\.[2-9]/.test(modelId) || /^o4/.test(modelId);
4571 const clamped = supportsXhigh ? effort : clampEffort(effort, "high");
4672 return { openai: { reasoningEffort: clamped, reasoningSummary: "auto" } };
4773 }
4874
4975 function getGeminiThinkingOptions(
50- modelId: string,
76+ modelString: string,
5177 effort: ThinkingEffort,
5278 ): Record<string, unknown> {
79+ const { modelId } = parseModelString(modelString);
5380 if (/^gemini-3/.test(modelId)) {
5481 return {
5582 google: {
@@ -73,7 +100,10 @@ function getGeminiThinkingOptions(
73100
74101 interface ThinkingStrategy {
75102 supports: (modelString: string) => boolean;
76- build: (modelId: string, effort: ThinkingEffort) => Record<string, unknown>;
103+ build: (
104+ modelString: string,
105+ effort: ThinkingEffort,
106+ ) => Record<string, unknown>;
77107 }
78108
79109 const THINKING_STRATEGIES: readonly ThinkingStrategy[] = [
@@ -97,10 +127,9 @@ export function getThinkingProviderOptions(
97127 ): Record<string, unknown> | null {
98128 if (!supportsThinking(modelString)) return null;
99129
100- const { modelId } = parseModelString(modelString);
101130 for (const strategy of THINKING_STRATEGIES) {
102131 if (!strategy.supports(modelString)) continue;
103- return strategy.build(modelId, effort);
132+ return strategy.build(modelString, effort);
104133 }
105134
106135 return null;
src/llm-api/providers.test.ts+43−1
@@ -1,6 +1,9 @@
11 import { describe, expect, test } from "bun:test";
22
3-import { getCacheFamily } from "./provider-options.ts";
3+import {
4+ getCacheFamily,
5+ getThinkingProviderOptions,
6+} from "./provider-options.ts";
47
58 describe("getCacheFamily", () => {
69 test("identifies anthropic family", () => {
@@ -22,3 +25,42 @@ describe("getCacheFamily", () => {
2225 expect(getCacheFamily("ollama/llama3")).toBe("none");
2326 });
2427 });
28+
29+describe("getThinkingProviderOptions", () => {
30+ // zen/claude-haiku-4-5 has reasoning=1 in the real DB (seeded from models.dev)
31+ // and must use the legacy `thinking: { type: "enabled", budget_tokens: N }` API
32+ // because the Zen proxy does not support the `effort-2025-11-24` beta.
33+ test("zen claude uses enabled+budgetTokens, not adaptive+effort", () => {
34+ const opts = getThinkingProviderOptions("zen/claude-haiku-4-5", "medium");
35+ if (!opts) {
36+ // Model not in local DB yet — skip rather than fail in fresh envs
37+ return;
38+ }
39+ const anthropic = (opts as Record<string, unknown>).anthropic as Record<
40+ string,
41+ unknown
42+ >;
43+ expect(anthropic).toBeDefined();
44+ const thinking = anthropic.thinking as Record<string, unknown>;
45+ expect(thinking.type).toBe("enabled");
46+ expect(typeof thinking.budgetTokens).toBe("number");
47+ // Must NOT have the effort field (which triggers the effort-2025-11-24 beta)
48+ expect(anthropic.effort).toBeUndefined();
49+ });
50+
51+ test("direct anthropic claude uses adaptive+effort", () => {
52+ const opts = getThinkingProviderOptions(
53+ "anthropic/claude-haiku-4-5",
54+ "medium",
55+ );
56+ if (!opts) return;
57+ const anthropic = (opts as Record<string, unknown>).anthropic as Record<
58+ string,
59+ unknown
60+ >;
61+ expect(anthropic).toBeDefined();
62+ const thinking = anthropic.thinking as Record<string, unknown>;
63+ expect(thinking.type).toBe("adaptive");
64+ expect(anthropic.effort).toBe("medium");
65+ });
66+});