Skip to content

Commit 5c10d8d

Browse files
committed
fix bedrock adaptive max tokens
1 parent c5d34c8 commit 5c10d8d

2 files changed

Lines changed: 91 additions & 1 deletion

File tree

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

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,46 @@ describe("Bedrock Fable contract", () => {
316316
});
317317
});
318318

319+
it("uses model maxTokens for Fable requests through simple options", async () => {
320+
const send = vi.spyOn(BedrockRuntimeClient.prototype, "send").mockResolvedValue({
321+
$metadata: { httpStatusCode: 200 },
322+
stream: streamEvents([
323+
{ messageStart: { role: ConversationRole.ASSISTANT } },
324+
{ messageStop: { stopReason: "end_turn" } },
325+
]),
326+
} as never);
327+
328+
const stream = streamSimpleBedrock(fableModel(), context());
329+
await stream.result();
330+
331+
const command = send.mock.calls[0]?.[0] as { input?: Record<string, unknown> };
332+
expect(command.input).toMatchObject({
333+
inferenceConfig: { maxTokens: 128_000 },
334+
additionalModelRequestFields: {
335+
thinking: { type: "adaptive", display: "summarized" },
336+
output_config: { effort: "high" },
337+
},
338+
});
339+
});
340+
341+
it("preserves explicit caller maxTokens for Fable simple options", async () => {
342+
const send = vi.spyOn(BedrockRuntimeClient.prototype, "send").mockResolvedValue({
343+
$metadata: { httpStatusCode: 200 },
344+
stream: streamEvents([
345+
{ messageStart: { role: ConversationRole.ASSISTANT } },
346+
{ messageStop: { stopReason: "end_turn" } },
347+
]),
348+
} as never);
349+
350+
const stream = streamSimpleBedrock(fableModel(), context(), {
351+
maxTokens: 12_000,
352+
});
353+
await stream.result();
354+
355+
const command = send.mock.calls[0]?.[0] as { input?: Record<string, unknown> };
356+
expect(command.input?.inferenceConfig).toEqual({ maxTokens: 12_000 });
357+
});
358+
319359
it("preserves explicit tool disabling", async () => {
320360
const send = vi.spyOn(BedrockRuntimeClient.prototype, "send").mockResolvedValue({
321361
$metadata: { httpStatusCode: 200 },
@@ -477,6 +517,7 @@ describe("Bedrock canonical Claude aliases", () => {
477517
name: "Production Claude",
478518
reasoning: false,
479519
params: { canonicalModelId },
520+
maxTokens: 128_000,
480521
thinkingLevelMap,
481522
});
482523

@@ -492,12 +533,48 @@ describe("Bedrock canonical Claude aliases", () => {
492533
const command = send.mock.calls[0]?.[0] as { input?: Record<string, unknown> };
493534
expect(command.input).toMatchObject({
494535
modelId: "production-claude",
495-
inferenceConfig: {},
536+
inferenceConfig: { maxTokens: 128_000 },
496537
additionalModelRequestFields: {
497538
thinking: { type: "adaptive", display: "summarized" },
498539
output_config: { effort: expectedEffort },
499540
},
500541
});
501542
},
502543
);
544+
545+
it("preserves explicit caller maxTokens for adaptive Claude aliases", async () => {
546+
const send = vi.spyOn(BedrockRuntimeClient.prototype, "send").mockResolvedValue({
547+
$metadata: { httpStatusCode: 200 },
548+
stream: streamEvents([
549+
{ messageStart: { role: ConversationRole.ASSISTANT } },
550+
{ messageStop: { stopReason: "end_turn" } },
551+
]),
552+
} as never);
553+
const model = bedrockModel({
554+
id: "production-claude",
555+
name: "Production Claude",
556+
reasoning: false,
557+
params: { canonicalModelId: "claude-opus-4-8" },
558+
maxTokens: 128_000,
559+
thinkingLevelMap: { xhigh: "xhigh", max: "max" },
560+
});
561+
562+
await streamSimpleBedrock(
563+
model,
564+
{ messages: [{ role: "user", content: "Reply briefly.", timestamp: 0 }] } as never,
565+
{
566+
reasoning: "xhigh",
567+
maxTokens: 12_000,
568+
},
569+
).result();
570+
571+
const command = send.mock.calls[0]?.[0] as { input?: Record<string, unknown> };
572+
expect(command.input).toMatchObject({
573+
inferenceConfig: { maxTokens: 12_000 },
574+
additionalModelRequestFields: {
575+
thinking: { type: "adaptive", display: "summarized" },
576+
output_config: { effort: "xhigh" },
577+
},
578+
});
579+
});
503580
});

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ function resolveSimpleBedrockOptions(
361361
if (usesClaudeFable5BedrockContract(model)) {
362362
return {
363363
...base,
364+
maxTokens: resolveModelBackedMaxTokens(model, base.maxTokens),
364365
reasoning: options?.reasoning ?? "high",
365366
thinkingBudgets: options?.thinkingBudgets,
366367
} satisfies BedrockOptions;
@@ -372,6 +373,10 @@ function resolveSimpleBedrockOptions(
372373
: undefined;
373374
return {
374375
...base,
376+
maxTokens:
377+
reasoning !== undefined
378+
? resolveModelBackedMaxTokens(model, base.maxTokens)
379+
: base.maxTokens,
375380
reasoning,
376381
} satisfies BedrockOptions;
377382
}
@@ -380,6 +385,7 @@ function resolveSimpleBedrockOptions(
380385
if (supportsAdaptiveThinking(model)) {
381386
return {
382387
...base,
388+
maxTokens: resolveModelBackedMaxTokens(model, base.maxTokens),
383389
reasoning: options.reasoning,
384390
thinkingBudgets: options.thinkingBudgets,
385391
} satisfies BedrockOptions;
@@ -412,6 +418,13 @@ function resolveSimpleBedrockOptions(
412418
} satisfies BedrockOptions;
413419
}
414420

421+
function resolveModelBackedMaxTokens(
422+
model: Model<"bedrock-converse-stream">,
423+
baseMaxTokens: number | undefined,
424+
): number {
425+
return baseMaxTokens ?? model.maxTokens;
426+
}
427+
415428
function handleContentBlockStart(
416429
event: ContentBlockStartEvent,
417430
blocks: Block[],

0 commit comments

Comments
 (0)