diff --git a/packages/opencode/src/provider/provider.ts b/packages/opencode/src/provider/provider.ts index b11ca9368..77a02b861 100644 --- a/packages/opencode/src/provider/provider.ts +++ b/packages/opencode/src/provider/provider.ts @@ -815,6 +815,29 @@ export namespace Provider { opts.signal = combined } + // Reasoning models require max_completion_tokens instead of max_tokens + // when using openai-compatible or azure provider + const isOpenAICompatible = + model.api.npm === "@ai-sdk/openai-compatible" || model.api.npm === "@ai-sdk/azure" + if (isOpenAICompatible && model.capabilities.reasoning) { + if (opts.body && typeof opts.body === "string") { + try { + const body = JSON.parse(opts.body) + if (body.max_tokens !== undefined) { + body.max_completion_tokens = body.max_tokens + delete body.max_tokens + opts.body = JSON.stringify(body) + } + } catch (e) { + log.warn("Failed to transform max_tokens parameter for reasoning model", { + providerID: model.providerID, + modelID: model.id, + error: e, + }) + } + } + } + return fetchFn(input, { ...opts, // @ts-ignore see here: https://github.com/oven-sh/bun/issues/16682