Skip to content

Commit 9b59060

Browse files
committed
fix(bedrock): honor adaptive model max tokens
1 parent b14a95b commit 9b59060

2 files changed

Lines changed: 81 additions & 0 deletions

File tree

‎extensions/amazon-bedrock/stream.runtime.test.ts‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,68 @@ describe("Bedrock thinking effort mapping", () => {
179179
expect(testing.buildAdditionalModelRequestFields(model, options)).toBeUndefined();
180180
});
181181

182+
it("uses the model maxTokens cap for adaptive Claude thinking requests", () => {
183+
const model = bedrockModel({
184+
id: "us.anthropic.claude-opus-4-8",
185+
name: "Claude Opus 4.8",
186+
reasoning: true,
187+
contextWindow: 1_000_000,
188+
maxTokens: 128_000,
189+
});
190+
const options = testing.resolveSimpleBedrockOptions(model, { reasoning: "high" });
191+
192+
expect(options.maxTokens).toBe(128_000);
193+
expect(options.reasoning).toBe("high");
194+
expect(testing.buildAdditionalModelRequestFields(model, options)).toEqual({
195+
thinking: { type: "adaptive", display: "summarized" },
196+
output_config: { effort: "high" },
197+
});
198+
});
199+
200+
it.each([4096, 8192, 16_384])(
201+
"does not turn fallback maxTokens %s into an adaptive cap",
202+
(maxTokens) => {
203+
const model = bedrockModel({
204+
id: "us.anthropic.claude-opus-4-8",
205+
name: "Claude Opus 4.8",
206+
reasoning: true,
207+
maxTokens,
208+
});
209+
const options = testing.resolveSimpleBedrockOptions(model, { reasoning: "high" });
210+
211+
expect(options.maxTokens).toBeUndefined();
212+
expect(options.reasoning).toBe("high");
213+
},
214+
);
215+
216+
it("preserves explicit maxTokens caps for adaptive Claude thinking requests", () => {
217+
const model = bedrockModel({
218+
id: "us.anthropic.claude-opus-4-8",
219+
name: "Claude Opus 4.8",
220+
reasoning: true,
221+
contextWindow: 1_000_000,
222+
maxTokens: 128_000,
223+
});
224+
const options = testing.resolveSimpleBedrockOptions(model, {
225+
reasoning: "high",
226+
maxTokens: 32_000,
227+
});
228+
229+
expect(options.maxTokens).toBe(32_000);
230+
});
231+
182232
it("forces adaptive thinking for Bedrock Mythos Preview when callers omit reasoning", () => {
183233
const model = bedrockModel({
184234
id: "us.anthropic.claude-mythos-preview",
185235
name: "US Claude Mythos Preview",
186236
reasoning: true,
237+
contextWindow: 1_000_000,
238+
maxTokens: 128_000,
187239
});
188240
const options = testing.resolveSimpleBedrockOptions(model, {});
189241

190242
expect(options.reasoning).toBe("high");
243+
expect(options.maxTokens).toBe(128_000);
191244
expect(testing.buildAdditionalModelRequestFields(model, options)).toEqual({
192245
thinking: { type: "adaptive", display: "summarized" },
193246
output_config: { effort: "high" },
@@ -281,6 +334,15 @@ describe("Bedrock Fable contract", () => {
281334
} as never;
282335
}
283336

337+
it("uses the model maxTokens cap for simple Fable options", () => {
338+
const options = testing.resolveSimpleBedrockOptions(fableModel(), {});
339+
340+
expect(options).toMatchObject({
341+
maxTokens: 128_000,
342+
reasoning: "high",
343+
});
344+
});
345+
284346
it("sends always-adaptive high effort without unsupported request controls", async () => {
285347
const send = vi.spyOn(BedrockRuntimeClient.prototype, "send").mockResolvedValue({
286348
$metadata: { httpStatusCode: 200 },

‎extensions/amazon-bedrock/stream.runtime.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,20 @@ function normalizeFableToolChoice(
9090
return toolChoice;
9191
}
9292

93+
// OpenClaw synthesizes these caps when the provider's real output limit is unknown.
94+
// Keep them out of Bedrock adaptive requests so Bedrock can use its native default.
95+
const OPENCLAW_FALLBACK_MODEL_MAX_TOKENS = new Set([4096, 8192, 16_384]);
96+
97+
function resolveAdaptiveBedrockMaxTokens(
98+
model: Model<"bedrock-converse-stream">,
99+
baseMaxTokens: number | undefined,
100+
): number | undefined {
101+
if (baseMaxTokens !== undefined) {
102+
return baseMaxTokens;
103+
}
104+
return OPENCLAW_FALLBACK_MODEL_MAX_TOKENS.has(model.maxTokens) ? undefined : model.maxTokens;
105+
}
106+
93107
/** Stream a Bedrock Converse request using Bedrock-specific options. */
94108
export const streamBedrock: StreamFunction<"bedrock-converse-stream", BedrockOptions> = (
95109
model: Model<"bedrock-converse-stream">,
@@ -361,6 +375,7 @@ function resolveSimpleBedrockOptions(
361375
if (usesClaudeFable5BedrockContract(model)) {
362376
return {
363377
...base,
378+
maxTokens: resolveAdaptiveBedrockMaxTokens(model, base.maxTokens),
364379
reasoning: options?.reasoning ?? "high",
365380
thinkingBudgets: options?.thinkingBudgets,
366381
} satisfies BedrockOptions;
@@ -372,6 +387,9 @@ function resolveSimpleBedrockOptions(
372387
: undefined;
373388
return {
374389
...base,
390+
...(reasoning !== undefined
391+
? { maxTokens: resolveAdaptiveBedrockMaxTokens(model, base.maxTokens) }
392+
: {}),
375393
reasoning,
376394
} satisfies BedrockOptions;
377395
}
@@ -380,6 +398,7 @@ function resolveSimpleBedrockOptions(
380398
if (supportsAdaptiveThinking(model)) {
381399
return {
382400
...base,
401+
maxTokens: resolveAdaptiveBedrockMaxTokens(model, base.maxTokens),
383402
reasoning: options.reasoning,
384403
thinkingBudgets: options.thinkingBudgets,
385404
} satisfies BedrockOptions;

0 commit comments

Comments
 (0)