Amazon Bedrock Integration
Relevant source files
The following files were used as context for generating this wiki page:
The Amazon Bedrock Integration module enables the Anthropic Go SDK to communicate directly with Claude models hosted on Amazon Bedrock. It provides a seamless transition from the standard Anthropic API to the Bedrock Runtime by intercepting requests via middleware, handling AWS-specific authentication (SigV4 or Bearer Token), and remapping request/response formats to match Bedrock's requirements.
This integration is primarily contained within the bedrock package and leverages internal AWS authentication utilities. It supports both standard synchronous message creation and streaming responses using the Amazon EventStream protocol.
Sources: bedrock/bedrock.go:1-25, internal/awsauth/awsauth.go:1-20
Architecture and Middleware
The core of the integration is a custom HTTP middleware that transforms standard Anthropic API requests into a format compatible with the Amazon Bedrock Runtime API. This middleware handles URL path remapping, body modification, and authentication header injection.
Request Transformation Flow
When a request is initiated through a client configured with Bedrock options, the following transformations occur:
- Version Injection: If
anthropic_versionis missing from the body, it defaults tobedrock-2023-05-31. - Beta Header Routing: Headers like
anthropic-betaare removed from the HTTP headers and moved into the JSON request body under theanthropic_betafield. - Endpoint Remapping: Standard paths like
/v1/messagesare rewritten to Bedrock's structure:/model/{model_id}/invokeor/model/{model_id}/invoke-with-response-stream.
The diagram above shows the sequential transformation steps performed by the middleware to make a standard request compatible with AWS Bedrock.
Sources: bedrock/bedrock.go:189-235, bedrock/bedrock_test.go:133-150
Configuration and Authentication
The SDK provides multiple ways to configure the Bedrock client, supporting standard AWS credential chains, environment variables, and programmatic token providers.
Authentication Methods
| Method | Description | Configuration Source |
|---|---|---|
| AWS SigV4 | Standard AWS signing using Access Key, Secret Key, and optionally Session Token. | aws.Config.Credentials or AWS_REGION |
| Bearer Token | Uses a static token for authentication, specific to certain Bedrock configurations. | AWS_BEARER_TOKEN_BEDROCK or BearerAuthTokenProvider |
| Default Config | Automatically loads AWS configuration from the environment/shared files. | bedrock.WithLoadDefaultConfig() |
Sources: bedrock/bedrock.go:167-187, internal/awsauth/awsauth.go:121-145
Key Functions
WithConfig(cfg aws.Config): Returns aRequestOptionthat registers the Bedrock middleware using a pre-configured AWS config.WithLoadDefaultConfig(ctx context.Context, ...): Helper to load the default AWS SDK configuration and apply Bedrock settings.NewStaticBearerTokenProvider(token string): Creates a provider for static token-based authentication.
Sources: bedrock/bedrock.go:151-175, examples/bedrock-bearer-token/main.go:16-25
Streaming and EventStream Decoding
Bedrock uses the application/vnd.amazon.eventstream content type for streaming responses, which differs from the standard Server-Sent Events (SSE) used by the Anthropic API. The SDK includes a specialized eventstreamDecoder to bridge this gap.
EventStream Processing logic
The decoder listens for AWS EventStream chunks, extracts the base64-encoded payload, and converts it back into standard Anthropic stream events.
The sequence diagram illustrates how the binary protocol from AWS is translated into the iterative stream used by the Go SDK.
Sources: bedrock/bedrock.go:50-130
Implementation Example
Integrating Bedrock involves passing the bedrock-specific request options during client initialization.
// Initializing with default AWS environment variables
client := anthropic.NewClient(
bedrock.WithLoadDefaultConfig(context.Background()),
)
// The client now uses Bedrock-specific model IDs
message, err := client.Messages.New(ctx, anthropic.MessageNewParams{
Model: "us.anthropic.claude-sonnet-4-5-20250929-v1:0",
MaxTokens: 1024,
Messages: []anthropic.MessageParam{...},
})
Sources: examples/bedrock/main.go:11-30, examples/bedrock-bearer-token/main.go:30-40
URL Encoding and ARNs
The integration handles complex model identifiers, including Amazon Resource Names (ARNs) for foundation models and inference profiles. Colons and slashes in ARNs are automatically escaped for the RawPath while remaining readable in the standard Path.
| Model Identifier Type | Example Value | Remapped Path Segment |
|---|---|---|
| Model Name | claude-3-sonnet | /model/claude-3-sonnet/... |
| Foundation ARN | arn:aws:bedrock:us-east-1:123:foundation-model/anthropic.claude-3-v1:0 | /model/arn%3Aaws%3Abedrock.../invoke |
| Inference Profile | arn:aws:bedrock:us-east-1:123:application-inference-profile/profile-id | /model/arn%3Aaws%3Abedrock.../invoke |
Sources: bedrock/bedrock_test.go:20-55
The Amazon Bedrock Integration module successfully abstracts the complexities of AWS-specific protocols and authentication, allowing Go developers to use the familiar Anthropic SDK interface while targeting infrastructure hosted on AWS.