Skip to content

Commit 9c929f5

Browse files
committed
1 parent ba46d1f commit 9c929f5

9 files changed

Lines changed: 146 additions & 8 deletions

File tree

‎packages/types/src/providers/bedrock.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const bedrockDefaultPromptRouterModelId: BedrockModelId = "anthropic.clau
1414
// feature.
1515
export const bedrockModels = {
1616
"anthropic.claude-sonnet-4-5-20250929-v1:0": {
17-
maxTokens: 8192,
17+
maxTokens: 64_000,
1818
contextWindow: 200_000,
1919
supportsImages: true,
2020
supportsPromptCache: true,
@@ -28,7 +28,7 @@ export const bedrockModels = {
2828
cachableFields: ["system", "messages", "tools"],
2929
},
3030
"anthropic.claude-sonnet-4-6": {
31-
maxTokens: 8192,
31+
maxTokens: 64_000,
3232
contextWindow: 200_000, // Default 200K, extendable to 1M with beta flag 'context-1m-2025-08-07'
3333
supportsImages: true,
3434
supportsPromptCache: true,
@@ -52,7 +52,8 @@ export const bedrockModels = {
5252
],
5353
},
5454
"anthropic.claude-sonnet-5": {
55-
maxTokens: 8192,
55+
maxTokens: 128_000,
56+
supportsMaxTokens: true,
5657
contextWindow: 1_000_000, // 1M context window native (no beta header required)
5758
supportsImages: true,
5859
supportsPromptCache: true,
@@ -186,7 +187,7 @@ export const bedrockModels = {
186187
],
187188
},
188189
"anthropic.claude-opus-4-7": {
189-
maxTokens: 8192,
190+
maxTokens: 128_000,
190191
contextWindow: 200_000, // Default 200K, extendable to 1M with beta flag 'context-1m-2025-08-07'
191192
supportsImages: true,
192193
supportsPromptCache: true,
@@ -210,7 +211,7 @@ export const bedrockModels = {
210211
],
211212
},
212213
"anthropic.claude-opus-4-8": {
213-
maxTokens: 8192,
214+
maxTokens: 128_000,
214215
contextWindow: 200_000, // Default 200K, extendable to 1M with beta flag 'context-1m-2025-08-07'
215216
supportsImages: true,
216217
supportsPromptCache: true,
@@ -236,7 +237,8 @@ export const bedrockModels = {
236237
],
237238
},
238239
"anthropic.claude-opus-5": {
239-
maxTokens: 8192,
240+
maxTokens: 128_000,
241+
supportsMaxTokens: true,
240242
contextWindow: 1_000_000, // 1M context window native (no beta header required)
241243
supportsImages: true,
242244
supportsPromptCache: true,

‎src/api/providers/__tests__/bedrock.spec.ts‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ import { HttpsProxyAgent } from "https-proxy-agent"
7070

7171
import { makeCreateMessageMetadata } from "../../../test-utils/api"
7272
import { clearAllMocks } from "../../../test-utils/reset"
73+
import { asyncStreamFrom, collectStream } from "../../../test-utils/stream"
74+
import type { ApiStreamChunk } from "../../transform/stream"
7375

7476
// Get access to the mocked functions
7577
const mockConverseStreamCommand = vi.mocked(ConverseStreamCommand)
@@ -94,6 +96,68 @@ describe("AwsBedrockHandler", () => {
9496
})
9597
})
9698

99+
it.each(["max_tokens", "end_turn", "tool_use", "stop_sequence"])(
100+
"reports truncation only for %s, after preserving usage",
101+
async (stopReason) => {
102+
handler["client"].send = vi.fn().mockResolvedValue({
103+
stream: asyncStreamFrom([
104+
{ contentBlockDelta: { delta: { reasoningContent: { text: "Still thinking" } } } },
105+
{ messageStop: { stopReason } },
106+
{ metadata: { usage: { inputTokens: 37, outputTokens: 16384 } } },
107+
]),
108+
})
109+
const chunks: ApiStreamChunk[] = []
110+
const consume = async () => {
111+
for await (const chunk of handler.createMessage("system", [{ role: "user", content: "hello" }])) {
112+
chunks.push(chunk)
113+
}
114+
}
115+
if (stopReason === "max_tokens") {
116+
await expect(consume()).rejects.toThrow(
117+
"Output token limit reached. Consider increasing Max Output Tokens",
118+
)
119+
} else {
120+
await expect(consume()).resolves.toBeUndefined()
121+
}
122+
expect(chunks).toEqual(
123+
expect.arrayContaining([
124+
{ type: "reasoning", text: "Still thinking" },
125+
expect.objectContaining({ type: "usage", inputTokens: 37, outputTokens: 16384 }),
126+
]),
127+
)
128+
},
129+
)
130+
131+
it.each([
132+
["anthropic.claude-sonnet-4-5-20250929-v1:0", 64_000],
133+
["anthropic.claude-sonnet-4-6", 64_000],
134+
["anthropic.claude-sonnet-5", 128_000],
135+
["anthropic.claude-opus-4-7", 128_000],
136+
["anthropic.claude-opus-4-8", 128_000],
137+
["anthropic.claude-opus-5", 128_000],
138+
] as const)("exposes %s output ceiling without raising request defaults", async (apiModelId, ceiling) => {
139+
for (const enableReasoningEffort of [true, false]) {
140+
for (const modelMaxTokens of [undefined, ceiling]) {
141+
const provider = new AwsBedrockHandler({ apiModelId, enableReasoningEffort, modelMaxTokens })
142+
expect(provider.getModel().info.maxTokens).toBe(ceiling)
143+
provider["client"].send = vi.fn().mockResolvedValue({ stream: asyncStreamFrom([]) })
144+
await collectStream(provider.createMessage("system", [{ role: "user", content: "hello" }]))
145+
const configurableWithoutReasoning = apiModelId.endsWith("-5")
146+
const expected =
147+
modelMaxTokens && (enableReasoningEffort || configurableWithoutReasoning)
148+
? ceiling
149+
: enableReasoningEffort
150+
? 16_384
151+
: 8192
152+
expect(mockConverseStreamCommand).toHaveBeenLastCalledWith(
153+
expect.objectContaining({
154+
inferenceConfig: expect.objectContaining({ maxTokens: expected }),
155+
}),
156+
)
157+
}
158+
}
159+
})
160+
97161
describe("getModel", () => {
98162
it("should return the correct model info for a standard model", () => {
99163
const modelInfo = handler.getModel()

‎src/api/providers/bedrock.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,7 @@ export class AwsBedrockHandler extends BaseProvider implements SingleCompletionH
601601
throw new Error("No stream available in the response")
602602
}
603603

604+
let outputLimitReached = false
604605
for await (const chunk of response.stream) {
605606
// Parse the chunk as JSON if it's a string (for tests)
606607
let streamEvent: StreamEvent
@@ -784,9 +785,16 @@ export class AwsBedrockHandler extends BaseProvider implements SingleCompletionH
784785
}
785786
// Handle message stop
786787
if (streamEvent.messageStop) {
788+
outputLimitReached = streamEvent.messageStop.stopReason === "max_tokens"
787789
continue
788790
}
789791
}
792+
// Bedrock sends usage metadata after messageStop. Preserve it before reporting truncation.
793+
if (outputLimitReached) {
794+
throw new Error(
795+
"Output token limit reached. Consider increasing Max Output Tokens in the model settings.",
796+
)
797+
}
790798
// Clear timeout after stream completes
791799
clearTimeout(timeoutId)
792800
} catch (error: unknown) {

‎src/shared/api.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ export const getModelMaxOutputTokens = ({
125125

126126
// For "Hybrid" reasoning models, discard the model's actual maxTokens for Anthropic contexts
127127
if (model.supportsReasoningBudget && isAnthropicContext) {
128+
if (model.supportsMaxTokens && settings?.modelMaxTokens != null && settings.modelMaxTokens > 0) {
129+
return model.maxTokens ? Math.min(settings.modelMaxTokens, model.maxTokens) : settings.modelMaxTokens
130+
}
128131
return ANTHROPIC_DEFAULT_MAX_TOKENS
129132
}
130133

‎webview-ui/playwright/gallery/stories.tsx‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,38 @@ const mermaidGantt = `gantt
1212
Ship release :active, release, after scope, 3d`
1313

1414
export const stories: Record<string, Story> = {
15+
"bedrock-output-budget": async ({ model = "anthropic.claude-opus-5" }) => {
16+
const [{ AppProviders }, { ThinkingBudget }, { bedrockModels, providerIdentifiers }] = await Promise.all([
17+
import("../AppProviders"),
18+
import("@/components/settings/ThinkingBudget"),
19+
import("@roo-code/types"),
20+
])
21+
const apiModelId = model === "anthropic.claude-opus-4-8" ? model : "anthropic.claude-opus-5"
22+
function Budget() {
23+
const [config, setConfig] = useState<import("@roo-code/types").ProviderSettings>({
24+
apiProvider: providerIdentifiers.bedrock,
25+
apiModelId,
26+
enableReasoningEffort: true,
27+
})
28+
return (
29+
<div className="w-[420px] p-4 flex flex-col gap-4 bg-vscode-editor-background">
30+
<h2>{apiModelId}</h2>
31+
<ThinkingBudget
32+
apiConfiguration={config}
33+
modelInfo={bedrockModels[apiModelId]}
34+
setApiConfigurationField={(field, value) =>
35+
setConfig((previous) => ({ ...previous, [field]: value }))
36+
}
37+
/>
38+
</div>
39+
)
40+
}
41+
return (
42+
<AppProviders>
43+
<Budget />
44+
</AppProviders>
45+
)
46+
},
1547
"accessibility-contrast": async () => {
1648
const { AccessibilityContrastGallery } =
1749
await import("@/components/ui/__tests__/AccessibilityContrast.visual.fixture")

‎webview-ui/src/components/settings/ThinkingBudget.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ export const ThinkingBudget = ({ apiConfiguration, setApiConfigurationField, mod
7474
// Models that advertise a user-configurable max output budget (e.g. Z.ai GLM) but do not
7575
// use the reasoning-budget slider. The reasoning-budget branch already renders its own
7676
// max-tokens control, so only surface this standalone slider when that branch is inactive.
77-
const isMaxTokensConfigurable = !!modelInfo && modelInfo.supportsMaxTokens && !isReasoningBudgetSupported
77+
const isMaxTokensConfigurable =
78+
!!modelInfo && modelInfo.supportsMaxTokens && (isReasoningSupported || !isReasoningBudgetSupported)
7879

7980
// "disable" turns off reasoning entirely; "none" is a valid reasoning level.
8081
// Both display as "None" in the UI but behave differently.

‎webview-ui/src/components/settings/__tests__/ThinkingBudget.spec.tsx‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { providerIdentifiers } from "@roo-code/types"
1+
import { bedrockModels, providerIdentifiers } from "@roo-code/types"
22
// npx vitest src/components/settings/__tests__/ThinkingBudget.spec.tsx
33

44
import React from "react"
@@ -79,6 +79,26 @@ describe("ThinkingBudget", () => {
7979
vi.clearAllMocks()
8080
})
8181

82+
it.each([
83+
["anthropic.claude-sonnet-4-5-20250929-v1:0", 64_000],
84+
["anthropic.claude-opus-4-8", 128_000],
85+
["anthropic.claude-sonnet-5", 128_000],
86+
["anthropic.claude-opus-5", 128_000],
87+
] as const)("allows selecting the documented Bedrock output ceiling for %s", (apiModelId, ceiling) => {
88+
render(
89+
<ThinkingBudget
90+
{...defaultProps}
91+
apiConfiguration={{ apiProvider: providerIdentifiers.bedrock, apiModelId, enableReasoningEffort: true }}
92+
modelInfo={bedrockModels[apiModelId]}
93+
/>,
94+
)
95+
const outputSlider = screen.getAllByTestId("slider")[0]
96+
expect(outputSlider).toHaveAttribute("max", String(ceiling))
97+
expect(outputSlider).toHaveValue("16384")
98+
fireEvent.change(outputSlider, { target: { value: String(ceiling) } })
99+
expect(defaultProps.setApiConfigurationField).toHaveBeenCalledWith("modelMaxTokens", ceiling)
100+
})
101+
82102
it("should render nothing when model information is unavailable", () => {
83103
const { container } = render(<ThinkingBudget {...defaultProps} modelInfo={undefined} />)
84104

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { expect, test } from "../../../../playwright/coverage-fixture"
2+
3+
test("renders the Bedrock binary-reasoning output budget control", async ({ page }) => {
4+
await page.goto("/")
5+
await page.waitForFunction(() => typeof window.mount === "function")
6+
await page.evaluate(() => window.mount({ story: "bedrock-output-budget" }))
7+
await expect(page.locator("[data-playwright-mounted]")).toHaveScreenshot("bedrock-output-budget.png")
8+
})
7.5 KB
Loading

0 commit comments

Comments
 (0)