Skip to content

Commit c4dab9e

Browse files
adamhillmrubens
andauthored
Improves model info detection for custom Bedrock ARNs (#3799)
* Improves model info detection for custom Bedrock ARNs Adds heuristics to better estimate model capabilities when using unknown or custom model ARNs, including context window and max tokens. Allows user overrides for key model parameters via provider settings, improving flexibility and reliability for non-standard model integrations. Fixes #3712 * Improves JSON syntax error handling in import flow Provides more informative error messages for JSON syntax errors by extracting the error position and formatting it for clarity during import. Enhances user feedback when invalid JSON is encountered. * Fixed failing tests * Delete pnpm-lock.yaml * Added Rory's cache fix from PR #3099 PR #3009 has an important fix, alerted to me by @jbbrown It was a one liner so I pulled it in. This brings up a question can we merge PR's in the GH UI? * Add Claude 4 and Opus 4 to modelID's Kept previous parameters, did not see any changes in those. * Fixed types being moved and me breaking the merge. * Fix merge --------- Co-authored-by: Matt Rubens <[email protected]>
1 parent 2fda064 commit c4dab9e

File tree

3 files changed

+75
-4
lines changed

3 files changed

+75
-4
lines changed

packages/types/src/provider-settings.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ const bedrockSchema = apiModelIdProviderModelSchema.extend({
100100
awsProfile: z.string().optional(),
101101
awsUseProfile: z.boolean().optional(),
102102
awsCustomArn: z.string().optional(),
103+
awsModelContextWindow: z.number().optional(),
103104
awsBedrockEndpointEnabled: z.boolean().optional(),
104105
awsBedrockEndpoint: z.string().optional(),
105106
})
@@ -285,6 +286,7 @@ export const PROVIDER_SETTINGS_KEYS = keysOf<ProviderSettings>()([
285286
"awsProfile",
286287
"awsUseProfile",
287288
"awsCustomArn",
289+
"awsModelContextWindow",
288290
"awsBedrockEndpointEnabled",
289291
"awsBedrockEndpoint",
290292
// Google Vertex

packages/types/src/providers/bedrock.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,8 @@ export const BEDROCK_DEFAULT_TEMPERATURE = 0.3
355355

356356
export const BEDROCK_MAX_TOKENS = 4096
357357

358+
export const BEDROCK_DEFAULT_CONTEXT = 128_000
359+
358360
export const BEDROCK_REGION_INFO: Record<
359361
string,
360362
{

src/api/providers/bedrock.ts

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
bedrockDefaultPromptRouterModelId,
2020
BEDROCK_DEFAULT_TEMPERATURE,
2121
BEDROCK_MAX_TOKENS,
22+
BEDROCK_DEFAULT_CONTEXT,
2223
BEDROCK_REGION_INFO,
2324
} from "@roo-code/types"
2425

@@ -192,6 +193,65 @@ export class AwsBedrockHandler extends BaseProvider implements SingleCompletionH
192193
this.client = new BedrockRuntimeClient(clientConfig)
193194
}
194195

196+
// Helper to guess model info from custom modelId string if not in bedrockModels
197+
private guessModelInfoFromId(modelId: string): Partial<ModelInfo> {
198+
// Define a mapping for model ID patterns and their configurations
199+
const modelConfigMap: Record<string, Partial<ModelInfo>> = {
200+
"claude-4": {
201+
maxTokens: 8192,
202+
contextWindow: 200_000,
203+
supportsImages: true,
204+
supportsPromptCache: true,
205+
},
206+
"claude-3-7": {
207+
maxTokens: 8192,
208+
contextWindow: 200_000,
209+
supportsImages: true,
210+
supportsPromptCache: true,
211+
},
212+
"claude-3-5": {
213+
maxTokens: 8192,
214+
contextWindow: 200_000,
215+
supportsImages: true,
216+
supportsPromptCache: true,
217+
},
218+
"claude-4-opus": {
219+
maxTokens: 4096,
220+
contextWindow: 200_000,
221+
supportsImages: true,
222+
supportsPromptCache: true,
223+
},
224+
"claude-3-opus": {
225+
maxTokens: 4096,
226+
contextWindow: 200_000,
227+
supportsImages: true,
228+
supportsPromptCache: true,
229+
},
230+
"claude-3-haiku": {
231+
maxTokens: 4096,
232+
contextWindow: 200_000,
233+
supportsImages: true,
234+
supportsPromptCache: true,
235+
},
236+
}
237+
238+
// Match the model ID to a configuration
239+
const id = modelId.toLowerCase()
240+
for (const [pattern, config] of Object.entries(modelConfigMap)) {
241+
if (id.includes(pattern)) {
242+
return config
243+
}
244+
}
245+
246+
// Default fallback
247+
return {
248+
maxTokens: BEDROCK_MAX_TOKENS,
249+
contextWindow: BEDROCK_DEFAULT_CONTEXT,
250+
supportsImages: false,
251+
supportsPromptCache: false,
252+
}
253+
}
254+
195255
override async *createMessage(
196256
systemPrompt: string,
197257
messages: Anthropic.Messages.MessageParam[],
@@ -640,16 +700,24 @@ export class AwsBedrockHandler extends BaseProvider implements SingleCompletionH
640700
info: JSON.parse(JSON.stringify(bedrockModels[bedrockDefaultPromptRouterModelId])),
641701
}
642702
} else {
703+
// Use heuristics for model info, then allow overrides from ProviderSettings
704+
const guessed = this.guessModelInfoFromId(modelId)
643705
model = {
644706
id: bedrockDefaultModelId,
645-
info: JSON.parse(JSON.stringify(bedrockModels[bedrockDefaultModelId])),
707+
info: {
708+
...JSON.parse(JSON.stringify(bedrockModels[bedrockDefaultModelId])),
709+
...guessed,
710+
},
646711
}
647712
}
648713

649-
// If modelMaxTokens is explicitly set in options, override the default
714+
// Always allow user to override detected/guessed maxTokens and contextWindow
650715
if (this.options.modelMaxTokens && this.options.modelMaxTokens > 0) {
651716
model.info.maxTokens = this.options.modelMaxTokens
652717
}
718+
if (this.options.awsModelContextWindow && this.options.awsModelContextWindow > 0) {
719+
model.info.contextWindow = this.options.awsModelContextWindow
720+
}
653721

654722
return model
655723
}
@@ -684,8 +752,7 @@ export class AwsBedrockHandler extends BaseProvider implements SingleCompletionH
684752
}
685753
}
686754

687-
modelConfig.info.maxTokens = modelConfig.info.maxTokens || BEDROCK_MAX_TOKENS
688-
755+
// Don't override maxTokens/contextWindow here; handled in getModelById (and includes user overrides)
689756
return modelConfig as { id: BedrockModelId | string; info: ModelInfo }
690757
}
691758

0 commit comments

Comments
 (0)