This document explains how model information is identified and managed in the Amazon Bedrock provider implementation (bedrock.ts). It focuses on the sequence of operations that determine the costModelConfig property, which is crucial for token counting, pricing, and other features.
The costModelConfig property is set through different paths depending on the input configuration and response data from Bedrock. Below is a sequence diagram of how model identification works:
sequenceDiagram
participant Constructor
participant parseArn
participant parseBaseModelId
participant getModelById
participant getModel
Constructor->>parseArn: Initialize (if awsCustomArn provided)
parseArn->>parseBaseModelId: Extract region prefix from modelId
parseBaseModelId-->>parseArn: Return modelId without prefix
parseArn->>parseArn: Determine if cross-region inference
parseArn-->>Constructor: Return arnInfo with modelId and crossRegionInference flag
Constructor->>getModel: Call getModel()
getModel->>getModelById: Lookup model
getModelById-->>getModel: Return model info
getModel-->>Constructor: Return model config
Constructor->>Constructor: Set this.costModelConfig
sequenceDiagram
participant createMessage
participant parseArn
participant getModelById
createMessage->>parseArn: Process stream event with invokedModelId
parseArn->>parseArn: Extract modelId
parseArn-->>createMessage: Return invokedModelArn
createMessage->>getModelById: Call getModelById with invokedModelArn.modelId
getModelById-->>createMessage: Return invokedModel
createMessage->>createMessage: Set invokedModel.id = modelConfig.id
createMessage->>createMessage: Set this.costModelConfig = invokedModel
Input:
const handler = new AwsBedrockHandler({
apiModelId: "anthropic.claude-3-5-sonnet-20241022-v2:0",
awsAccessKey: "ACCESS_KEY",
awsSecretKey: "SECRET_KEY",
awsRegion: "us-east-1",
})
Sequence:
getModel()getModel() calls getModelById("anthropic.claude-3-5-sonnet-20241022-v2:0")getModelById() looks up the model in bedrockModelsthis.costModelConfig is set to:
{
id: "anthropic.claude-3-5-sonnet-20241022-v2:0",
info: {
maxTokens: 4096,
contextWindow: 128000,
inputPrice: 3,
outputPrice: 15,
// other model properties...
}
}
Input:
const handler = new AwsBedrockHandler({
apiModelId: "anthropic.claude-3-5-sonnet-20241022-v2:0",
awsAccessKey: "ACCESS_KEY",
awsSecretKey: "SECRET_KEY",
awsRegion: "us-east-1",
awsCustomArn: "arn:aws:bedrock:us-east-1::foundation-model/anthropic.claude-3-5-sonnet-20241022-v2:0",
})
Sequence:
parseArn("arn:aws:bedrock:us-east-1::foundation-model/anthropic.claude-3-5-sonnet-20241022-v2:0")parseArn() extracts:
{
isValid: true,
region: "us-east-1",
modelType: "foundation-model",
modelId: "anthropic.claude-3-5-sonnet-20241022-v2:0",
crossRegionInference: false
}
Constructor sets this.arnInfo to the result
Constructor calls getModel()
getModel() calls getModelById("anthropic.claude-3-5-sonnet-20241022-v2:0")
getModelById() looks up the model in bedrockModels
this.costModelConfig is set to:
{
id: "anthropic.claude-3-5-sonnet-20241022-v2:0", // Note: ID is not the ARN since it's a foundation-model
info: {
maxTokens: 4096,
contextWindow: 128000,
inputPrice: 3,
outputPrice: 15,
// other model properties...
}
}
Input:
const handler = new AwsBedrockHandler({
apiModelId: "anthropic.claude-3-5-sonnet-20241022-v2:0",
awsAccessKey: "ACCESS_KEY",
awsSecretKey: "SECRET_KEY",
awsRegion: "us-west-2",
awsCustomArn: "arn:aws:bedrock:us-west-2:123456789012:prompt-router/my-router",
})
Sequence:
parseArn("arn:aws:bedrock:us-west-2:123456789012:prompt-router/my-router")parseArn() extracts:
{
isValid: true,
region: "us-west-2",
modelType: "prompt-router",
modelId: "my-router",
crossRegionInference: false
}
Constructor sets this.arnInfo to the result
Constructor calls getModel()
getModel() calls getModelById("my-router")
getModelById() doesn't find "my-router" in bedrockModels, returns default model info
Since this.arnInfo.modelType is "prompt-router" (not "foundation-model"), getModel() sets the ID to the full ARN
this.costModelConfig is set to:
{
id: "arn:aws:bedrock:us-west-2:123456789012:prompt-router/my-router", // Full ARN as ID
info: {
// Default model info for prompt routers
maxTokens: 4096,
contextWindow: 128000,
inputPrice: 3,
outputPrice: 15,
// other model properties...
}
}
Input:
const handler = new AwsBedrockHandler({
apiModelId: "anthropic.claude-3-5-sonnet-20241022-v2:0",
awsAccessKey: "ACCESS_KEY",
awsSecretKey: "SECRET_KEY",
awsRegion: "eu-west-1",
awsUseCrossRegionInference: true,
})
Sequence:
getModel()getModel() calls getModelById("anthropic.claude-3-5-sonnet-20241022-v2:0")getModelById() looks up the model in bedrockModelsawsUseCrossRegionInference is true, getModel() gets the prefix for "eu-west-1" (which is "eu.")getModel() prepends "eu." to the model IDthis.costModelConfig is set to:
{
id: "eu.anthropic.claude-3-5-sonnet-20241022-v2:0", // Note the "eu." prefix
info: {
maxTokens: 4096,
contextWindow: 128000,
inputPrice: 3,
outputPrice: 15,
// other model properties...
}
}
Initial Input:
const handler = new AwsBedrockHandler({
awsAccessKey: "ACCESS_KEY",
awsSecretKey: "SECRET_KEY",
awsRegion: "us-west-2",
awsCustomArn: "arn:aws:bedrock:us-west-2:123456789012:prompt-router/my-router",
})
Initial Sequence (same as Example 3):
this.costModelConfig is initially set to:
{
id: "arn:aws:bedrock:us-west-2:123456789012:prompt-router/my-router",
info: {
// Default model info for prompt routers
maxTokens: 4096,
contextWindow: 128000,
inputPrice: 3,
outputPrice: 15,
// other properties...
}
}
Stream Event with invokedModelId:
{
trace: {
promptRouter: {
invokedModelId: "arn:aws:bedrock:us-west-2:123456789012:inference-profile/anthropic.claude-3-5-sonnet-20241022-v2:0",
usage: {
inputTokens: 150,
outputTokens: 250
}
}
}
}
Stream Processing Sequence:
createMessage() encounters the stream event with invokedModelIdparseArn("arn:aws:bedrock:us-west-2:123456789012:inference-profile/anthropic.claude-3-5-sonnet-20241022-v2:0")parseArn() extracts:
{
isValid: true,
region: "us-west-2",
modelType: "inference-profile",
modelId: "anthropic.claude-3-5-sonnet-20241022-v2:0",
crossRegionInference: false
}
createMessage() calls getModelById("anthropic.claude-3-5-sonnet-20241022-v2:0")
getModelById() looks up the model in bedrockModels and returns the model info
createMessage() sets invokedModel.id to the original router ID
this.costModelConfig is updated to:
{
id: "arn:aws:bedrock:us-west-2:123456789012:prompt-router/my-router", // Keeps router ID
info: {
// Claude 3.5 Sonnet model info
maxTokens: 4096,
contextWindow: 128000,
inputPrice: 3,
outputPrice: 15,
// other Claude-specific properties...
}
}
This ensures that:
Input:
const handler = new AwsBedrockHandler({
apiModelId: "anthropic.claude-3-5-sonnet-20241022-v2:0",
awsAccessKey: "ACCESS_KEY",
awsSecretKey: "SECRET_KEY",
awsRegion: "us-east-1",
awsCustomArn:
"arn:aws:bedrock:us-west-2:123456789012:inference-profile/us.anthropic.claude-3-5-sonnet-20241022-v2:0",
})
Sequence:
parseArn("arn:aws:bedrock:us-west-2:123456789012:inference-profile/us.anthropic.claude-3-5-sonnet-20241022-v2:0")parseArn() extracts region and calls parseBaseModelId("us.anthropic.claude-3-5-sonnet-20241022-v2:0")parseBaseModelId() recognizes "us." as a region prefix and removes itparseArn() returns:
{
isValid: true,
region: "us-west-2",
modelType: "inference-profile",
modelId: "anthropic.claude-3-5-sonnet-20241022-v2:0", // Note: prefix removed
crossRegionInference: true // Detected cross-region
}
Constructor sets this.arnInfo to the result and updates this.options.awsRegion to "us-west-2"
Constructor calls getModel()
getModel() calls getModelById("anthropic.claude-3-5-sonnet-20241022-v2:0")
getModelById() looks up the model in bedrockModels
Since this.arnInfo.modelType is "inference-profile" (not "foundation-model"), getModel() sets the ID to the full ARN
this.costModelConfig is set to:
{
id: "arn:aws:bedrock:us-west-2:123456789012:inference-profile/us.anthropic.claude-3-5-sonnet-20241022-v2:0", // Full ARN
info: {
maxTokens: 4096,
contextWindow: 128000,
inputPrice: 3,
outputPrice: 15,
// other model properties...
}
}
Input:
const handler = new AwsBedrockHandler({
apiModelId: "anthropic.claude-3-5-sonnet-20241022-v2:0",
awsAccessKey: "ACCESS_KEY",
awsSecretKey: "SECRET_KEY",
awsRegion: "ap-northeast-3", // Osaka region
awsCustomArn:
"arn:aws:bedrock:ap-northeast-3:123456789012:inference-profile/apne3.anthropic.claude-3-5-sonnet-20241022-v2:0",
})
Sequence:
parseArn("arn:aws:bedrock:ap-northeast-3:123456789012:inference-profile/apne3.anthropic.claude-3-5-sonnet-20241022-v2:0")parseArn() extracts region and calls parseBaseModelId("apne3.anthropic.claude-3-5-sonnet-20241022-v2:0")parseBaseModelId() recognizes "apne3." as a region prefix and removes itparseArn() returns:
{
isValid: true,
region: "ap-northeast-3",
modelType: "inference-profile",
modelId: "anthropic.claude-3-5-sonnet-20241022-v2:0", // Note: prefix removed
crossRegionInference: false // Not a cross-region prefix since apne3 maps to a single region
}
Constructor sets this.arnInfo to the result
Constructor calls getModel()
getModel() calls getModelById("anthropic.claude-3-5-sonnet-20241022-v2:0")
getModelById() looks up the model in bedrockModels
Since this.arnInfo.modelType is "inference-profile" (not "foundation-model"), getModel() sets the ID to the full ARN
this.costModelConfig is set to:
{
id: "arn:aws:bedrock:ap-northeast-3:123456789012:inference-profile/apne3.anthropic.claude-3-5-sonnet-20241022-v2:0", // Full ARN
info: {
maxTokens: 4096,
contextWindow: 128000,
inputPrice: 3,
outputPrice: 15,
// other model properties...
}
}
The system recognizes these region prefixes for cross-region inference:
| Prefix | Region ID | Description | Multi-Region |
|---|---|---|---|
| "us." | "us-east-1" | US East (N. Virginia) | Yes |
| "use1." | "us-east-1" | US East (N. Virginia) | No |
| "use2." | "us-east-2" | US East (Ohio) | No |
| "usw2." | "us-west-2" | US West (Oregon) | No |
| "eu." | "eu-west-1" | Europe (Ireland) | Yes |
| "euw1." | "eu-west-1" | Europe (Ireland) | No |
| "ap." | "ap-southeast-1" | Asia Pacific (Singapore) | Yes |
| "apne1." | "ap-northeast-1" | Asia Pacific (Tokyo) | No |
| "apne3." | "ap-northeast-3" | Asia Pacific (Osaka) | No |
| "ca." | "ca-central-1" | Canada (Central) | Yes |
| "sa." | "sa-east-1" | South America (São Paulo) | Yes |
| "apac." | "ap-southeast-1" | Default APAC region | Yes |
| "emea." | "eu-west-1" | Default EMEA region | Yes |
| "amer." | "us-east-1" | Default Americas region | Yes |
These prefixes are used to:
parseBaseModelId()getModel()Note on Multi-Region Prefixes:
crossRegionInference flag to true when detected in an ARNcrossRegionInference flag to falsecrossRegionInference flag affects how the system handles region-specific model configurationsThe Bedrock provider's model identification system follows these key principles:
This system ensures that: