@@ -190,6 +190,141 @@ const CONTEXT_WINDOW_TABLE: Array<[pattern: RegExp, tokens: number]> = [
190190 // Qwen3 Coder — 131k (Qwen3 standard)
191191 [/^qwen3-/, 131_000],
192192 ];
193+export type ThinkingEffort = "low" | "medium" | "high" | "xhigh";
194+
195+// Maps model-ID patterns (tested against the bare model ID, not provider/id)
196+// to whether the model supports thinking/reasoning.
197+// Matched in order; first match wins.
198+const REASONING_MODELS: Array<RegExp> = [
199+ // Anthropic — Claude 3.5+ and Claude 3.7+ support extended thinking
200+ /^claude-3-5-sonnet/,
201+ /^claude-3-7/,
202+ /^claude-sonnet-4/,
203+ /^claude-opus-4/,
204+ // OpenAI — o-series and GPT-5 reasoning models
205+ /^o1/,
206+ /^o3/,
207+ /^o4/,
208+ /^gpt-5/,
209+ // Google — Gemini 2.5+ and Gemini 3.x
210+ /^gemini-2\.5/,
211+ /^gemini-3/,
212+];
213+
214+export function supportsThinking(modelString: string): boolean {
215+ const { modelId } = parseModelString(modelString);
216+ return REASONING_MODELS.some((p) => p.test(modelId));
217+}
218+
219+// Token budgets for Anthropic extended thinking (budget_tokens mode).
220+// Adaptive-mode models (claude-sonnet-4-6+, claude-opus-4+) use effort
221+// strings instead; detected by model ID below.
222+const ANTHROPIC_BUDGET: Record<ThinkingEffort, number> = {
223+ low: 4_096,
224+ medium: 8_192,
225+ high: 16_384,
226+ xhigh: 32_768,
227+};
228+
229+// Clamp xhigh → high for providers that don't support it.
230+function clampEffort(
231+ effort: ThinkingEffort,
232+ max: ThinkingEffort,
233+): ThinkingEffort {
234+ const ORDER: ThinkingEffort[] = ["low", "medium", "high", "xhigh"];
235+ const i = ORDER.indexOf(effort);
236+ const m = ORDER.indexOf(max);
237+ return ORDER[Math.min(i, m)] as ThinkingEffort;
238+}
239+
240+export function getThinkingProviderOptions(
241+ modelString: string,
242+ effort: ThinkingEffort,
243+): Record<string, unknown> | null {
244+ if (!supportsThinking(modelString)) return null;
245+
246+ const { provider, modelId } = parseModelString(modelString);
247+
248+ // Anthropic (direct or via Zen routing through @ai-sdk/anthropic)
249+ if (
250+ provider === "anthropic" ||
251+ (provider === "zen" && modelId.startsWith("claude-"))
252+ ) {
253+ // Claude 3.7+, Sonnet 4.x, Opus 4.x support adaptive effort strings.
254+ const isAdaptive =
255+ /^claude-3-7/.test(modelId) ||
256+ /^claude-sonnet-4/.test(modelId) ||
257+ /^claude-opus-4/.test(modelId);
258+
259+ if (isAdaptive) {
260+ // Adaptive: effort string ("low"|"medium"|"high"|"max")
261+ // xhigh maps to "max" for Opus; clamp to "high" for others.
262+ const isOpus = /^claude-opus-4/.test(modelId);
263+ const mapped = effort === "xhigh" ? (isOpus ? "max" : "high") : effort;
264+ return { anthropic: { thinking: { type: "adaptive" }, effort: mapped } };
265+ }
266+
267+ // Extended thinking: budget_tokens integer
268+ const budget = ANTHROPIC_BUDGET[effort];
269+ return {
270+ anthropic: {
271+ thinking: { type: "enabled", budgetTokens: budget },
272+ betas: ["interleaved-thinking-2025-05-14"],
273+ },
274+ };
275+ }
276+
277+ // OpenAI o-series and GPT-5 (direct or via Zen routing through @ai-sdk/openai)
278+ if (
279+ provider === "openai" ||
280+ (provider === "zen" &&
281+ (modelId.startsWith("o") || modelId.startsWith("gpt-5")))
282+ ) {
283+ // xhigh only valid on gpt-5.2+, o4+; clamp for others.
284+ const supportsXhigh = /^gpt-5\.[2-9]/.test(modelId) || /^o4/.test(modelId);
285+ const clamped = supportsXhigh ? effort : clampEffort(effort, "high");
286+ return { openai: { reasoningEffort: clamped } };
287+ }
288+
289+ // Google Gemini (direct or via Zen routing through @ai-sdk/google)
290+ if (
291+ provider === "google" ||
292+ (provider === "zen" && modelId.startsWith("gemini-"))
293+ ) {
294+ // Gemini 3.x: thinkingLevel enum. 2.5.x: budgetTokens.
295+ if (/^gemini-3/.test(modelId)) {
296+ // No xhigh for Gemini 3.
297+ const level = clampEffort(effort, "high");
298+ return {
299+ google: {
300+ thinkingConfig: {
301+ includeThoughts: true,
302+ thinkingLevel: level,
303+ },
304+ },
305+ };
306+ }
307+ // Gemini 2.5: budget tokens. Capped at 24575 per API limit.
308+ const GEMINI_BUDGET: Record<ThinkingEffort, number> = {
309+ low: 4_096,
310+ medium: 8_192,
311+ high: 16_384,
312+ xhigh: 24_575,
313+ };
314+ return {
315+ google: {
316+ thinkingConfig: {
317+ includeThoughts: true,
318+ thinkingBudget: GEMINI_BUDGET[effort],
319+ },
320+ },
321+ };
322+ }
323+
324+ // Unrecognised provider with a reasoning model (e.g. future providers) —
325+ // return null and let the call proceed without thinking options.
326+ return null;
327+}
193328
194329 /**
195330 * Return the known context window size (in tokens) for a model string.