Skip to content

Commit e755957

Browse files
committed
fix(provider): respect limit.output config instead of silently capping at 32k
Closes anomalyco#29363 maxOutputTokens() used Math.min(model.limit.output, 32000) which always applied the 32k floor as a ceiling, making per-model limit.output config useless. Now uses limit.output directly when set > 0, falls back to OUTPUT_TOKEN_MAX only when no limit is configured, and preserves the OPENCODE_EXPERIMENTAL_OUTPUT_TOKEN_MAX env var as an optional ceiling.
1 parent 34b1045 commit e755957

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1259,7 +1259,12 @@ export function providerOptions(model: Provider.Model, options: { [x: string]: a
12591259
}
12601260

12611261
export function maxOutputTokens(model: Provider.Model, outputTokenMax = OUTPUT_TOKEN_MAX): number {
1262-
return Math.min(model.limit.output, outputTokenMax) || outputTokenMax
1262+
if (model.limit.output > 0) {
1263+
return outputTokenMax !== OUTPUT_TOKEN_MAX
1264+
? Math.min(model.limit.output, outputTokenMax)
1265+
: model.limit.output
1266+
}
1267+
return outputTokenMax
12631268
}
12641269

12651270
export function schema(model: Provider.Model, schema: JSONSchema7): JSONSchema7 {

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3810,3 +3810,49 @@ describe("ProviderTransform.providerOptions - ai-gateway-provider", () => {
38103810
expect(result).toEqual({ openaiCompatible: { reasoningEffort: "high" } })
38113811
})
38123812
})
3813+
3814+
describe("ProviderTransform.maxOutputTokens", () => {
3815+
const baseModel = {
3816+
id: "test/model",
3817+
providerID: "test",
3818+
api: { id: "model", url: "https://test.com", npm: "@ai-sdk/openai-compatible" },
3819+
name: "Test Model",
3820+
capabilities: {
3821+
temperature: true,
3822+
reasoning: false,
3823+
attachment: false,
3824+
toolcall: true,
3825+
input: { text: true, audio: false, image: false, video: false, pdf: false },
3826+
output: { text: true, audio: false, image: false, video: false, pdf: false },
3827+
interleaved: false,
3828+
},
3829+
cost: { input: 1, output: 1, cache: { read: 0, write: 0 } },
3830+
status: "active",
3831+
options: {},
3832+
headers: {},
3833+
}
3834+
3835+
const modelWithOutput = (output: number) =>
3836+
({ ...baseModel, limit: { context: 200_000, input: 0, output } }) as any
3837+
3838+
test("respects configured limit.output when set", () => {
3839+
expect(ProviderTransform.maxOutputTokens(modelWithOutput(384_000))).toBe(384_000)
3840+
})
3841+
3842+
test("falls back to OUTPUT_TOKEN_MAX when limit.output is 0", () => {
3843+
expect(ProviderTransform.maxOutputTokens(modelWithOutput(0))).toBe(32_000)
3844+
})
3845+
3846+
test("respects env var ceiling when set", () => {
3847+
expect(ProviderTransform.maxOutputTokens(modelWithOutput(384_000), 100_000)).toBe(100_000)
3848+
})
3849+
3850+
test("uses env var as fallback when limit.output is 0", () => {
3851+
expect(ProviderTransform.maxOutputTokens(modelWithOutput(0), 64_000)).toBe(64_000)
3852+
})
3853+
3854+
test("env var ceiling does not raise above configured limit", () => {
3855+
// When limit.output < env var ceiling, limit.output wins
3856+
expect(ProviderTransform.maxOutputTokens(modelWithOutput(50_000), 100_000)).toBe(50_000)
3857+
})
3858+
})

0 commit comments

Comments
 (0)