Skip to content

Commit 9039a17

Browse files
committed
fix(provider): preserve small-model output budgets
1 parent 8ee9544 commit 9039a17

2 files changed

Lines changed: 48 additions & 2 deletions

File tree

‎packages/opencode/src/provider/output-token-budget.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,15 +279,21 @@ export function clampOutputTokens(input: {
279279
if (!context || context <= OUTPUT_TOKEN_FLOOR) return requested
280280
if (inputTokens + requested + margin <= context) return requested
281281

282+
// Do not reject a model for failing to reach a floor above its own reservation.
283+
const floor = Math.min(OUTPUT_TOKEN_FLOOR, requested)
282284
const clamped = Math.floor(context - inputTokens - margin)
283-
if (clamped < OUTPUT_TOKEN_FLOOR) {
285+
if (clamped < floor) {
286+
// If only the estimator margin causes the failure, preserve a reservation that still fits
287+
// the declared window. The provider remains authoritative for its exact tokenizer.
288+
const withoutMargin = Math.floor(context - inputTokens)
289+
if (withoutMargin >= floor) return Math.min(requested, withoutMargin)
284290
throw new OutputTokenBudgetError({
285291
modelID: input.model.id,
286292
providerID: input.model.providerID,
287293
inputTokens,
288294
requested,
289295
context,
290-
floor: OUTPUT_TOKEN_FLOOR,
296+
floor,
291297
})
292298
}
293299
log.warn("clamped output token reservation to fit context window", {

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4764,6 +4764,12 @@ describe("output token budget", () => {
47644764
expect(clampOutputTokens({ model, requested: 16_384, inputTokens: 60_000 })).toBe(16_384)
47654765
})
47664766

4767+
test("never demands more headroom than the model's own output reservation", () => {
4768+
const model = createWindowModel({ context: 8_192, output: 512 })
4769+
expect(clampOutputTokens({ model, requested: 512, inputTokens: 7_169 })).toBe(512)
4770+
expect(() => clampOutputTokens({ model, requested: 512, inputTokens: 8_000 })).toThrow(OutputTokenBudgetError)
4771+
})
4772+
47674773
test("does not clamp a window too small to hold even a floor-sized completion", () => {
47684774
// A declared window this small is a placeholder or a test fixture, not a real limit. Failing
47694775
// the request client-side on numbers we do not believe would be worse than letting the
@@ -5066,6 +5072,18 @@ describe("output token budget", () => {
50665072
headerSources: [{ aiGatewayHeaders: { "anthropic-beta": "context-1m-2025-08-07" } }],
50675073
}),
50685074
).toBe(1_000_000)
5075+
expect(
5076+
effectiveContextWindow({
5077+
model,
5078+
headerSources: [{ "Anthropic-Beta": "context-1m-2025-08-07" }],
5079+
}),
5080+
).toBe(1_000_000)
5081+
expect(
5082+
effectiveContextWindow({
5083+
model,
5084+
headerSources: [{ aiGatewayHeaders: { "anthropic-beta": "interleaved-thinking-2025-05-14" } }],
5085+
}),
5086+
).toBe(200_000)
50695087
})
50705088

50715089
test("clamps fixed reasoning budgets with the output reservation without mutating inputs", () => {
@@ -5081,6 +5099,28 @@ describe("output token budget", () => {
50815099
expect(options.thinking.budgetTokens).toBe(16_000)
50825100
})
50835101

5102+
test("preserves non-JSON provider options while lowering reasoning budgets", () => {
5103+
const callback = () => undefined
5104+
const options = {
5105+
thinking: { budgetTokens: 31_999 },
5106+
keepUndefined: undefined,
5107+
keepFunction: callback,
5108+
keepBigInt: 10n,
5109+
nested: [{ thinkingBudget: 31_999 }, { untouched: "value" }],
5110+
}
5111+
const result = clampReasoningBudget(options, 8_192)
5112+
expect(result.thinking.budgetTokens).toBe(7_168)
5113+
expect(result.nested[0]!.thinkingBudget).toBe(7_168)
5114+
expect(result.nested[1]!.untouched).toBe("value")
5115+
expect("keepUndefined" in result).toBeTrue()
5116+
expect(result.keepFunction).toBe(callback)
5117+
expect(result.keepBigInt).toBe(10n)
5118+
expect(options.thinking.budgetTokens).toBe(31_999)
5119+
5120+
const unchanged = { thinking: { budgetTokens: 512 } }
5121+
expect(clampReasoningBudget(unchanged, 8_192)).toBe(unchanged)
5122+
})
5123+
50845124
test("fails clearly when reasoning and visible output cannot both fit", () => {
50855125
expect(() => clampReasoningBudget({ thinking: { budgetTokens: 16_000 } }, 1_500)).toThrow(
50865126
/cannot preserve the configured reasoning budget/,

0 commit comments

Comments
 (0)