Anthropic’s Claude models are now generally available through Amazon Bedrock.

Calling Claude through Bedrock slightly differs from how you would call Claude when using Anthropic’s client SDK’s. This guide will walk you through the process of completing an API call to Claude on Bedrock in either Python or TypeScript.

Note that this guide assumes you have already signed up for an AWS account and configured programmatic access.

Install and configure the AWS CLI

  1. Install a version of the AWS CLI at or newer than version 2.13.23
  2. Configure your AWS credentials using the AWS configure command (see Configure the AWS CLI) or find your credentials by navigating to “Command line or programmatic access” within your AWS dashboard and following the directions in the popup modal.
  3. Verify that your credentials are working:
    aws sts get-caller-identity
    

Install an SDK for accessing Bedrock

Anthropic's client SDKs support Bedrock. You can also use an AWS SDK like boto3 directly.

pip install -U "anthropic[bedrock]"
npm install @anthropic-ai/bedrock-sdk
pip install boto3>=1.28.59

Accessing Bedrock

Subscribe to Anthropic models

Go to the AWS Console > Bedrock > Model Access and request access to Anthropic models. Note that Anthropic model availability varies by region. See AWS documentation for latest information.

API model names

ModelBedrock API model name
Claude 3 Haikuanthropic.claude-3-haiku-20240307-v1:0
Claude 3 Sonnetanthropic.claude-3-sonnet-20240229-v1:0
Claude 3 Opusanthropic.claude-3-opus-20240229-v1:0

List available models

The following examples show how to print a list of all the Claude models available through Bedrock:

aws bedrock list-foundation-models --region=us-west-2 --by-provider anthropic --query "modelSummaries[*].modelId"
import boto3

bedrock = boto3.client(service_name="bedrock")
response = bedrock.list_foundation_models(byProvider="anthropic")

for summary in response["modelSummaries"]:
    print(summary["modelId"])

Making requests

The following examples shows how to generate text from Claude 3 Sonnet on Bedrock:

from anthropic import AnthropicBedrock

client = AnthropicBedrock(
    # Authenticate by either providing the keys below or use the default AWS credential providers, such as
    # using ~/.aws/credentials or the "AWS_SECRET_ACCESS_KEY" and "AWS_ACCESS_KEY_ID" environment variables.
    aws_access_key="<access key>",
    aws_secret_key="<secret key>",
    # Temporary credentials can be used with aws_session_token.
    # Read more at https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp.html.
    aws_session_token="<session_token>",
    # aws_region changes the aws region to which the request is made. By default, we read AWS_REGION,
    # and if that's not present, we default to us-east-1. Note that we do not read ~/.aws/config for the region.
    aws_region="us-west-2",
)

message = client.messages.create(
    model="anthropic.claude-3-sonnet-20240229-v1:0",
    max_tokens=256,
    messages=[{"role": "user", "content": "Hello, world"}]
)
print(message.content)
import AnthropicBedrock from '@anthropic-ai/bedrock-sdk';

const client = new AnthropicBedrock({
  // Authenticate by either providing the keys below or use the default AWS credential providers, such as
  // using ~/.aws/credentials or the "AWS_SECRET_ACCESS_KEY" and "AWS_ACCESS_KEY_ID" environment variables.
  awsAccessKey: '<access key>',
  awsSecretKey: '<secret key>',

  // Temporary credentials can be used with awsSessionToken.
  // Read more at https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp.html.
  awsSessionToken: '<session_token>',

  // awsRegion changes the aws region to which the request is made. By default, we read AWS_REGION,
  // and if that's not present, we default to us-east-1. Note that we do not read ~/.aws/config for the region.
  awsRegion: 'us-west-2',
});

async function main() {
  const message = await client.messages.create({
    model: 'anthropic.claude-3-sonnet-20240229-v1:0',
    max_tokens: 256,
    messages: [{"role": "user", "content": "Hello, world"}]
  });
  console.log(message);
}
main().catch(console.error);
import boto3
import json

bedrock = boto3.client(service_name="bedrock-runtime")
body = json.dumps({
  "max_tokens": 256,
  "messages": [{"role": "user", "content": "Hello, world"}],
  "anthropic_version": "bedrock-2023-05-31"
})

response = bedrock.invoke_model(body=body, modelId="anthropic.claude-3-sonnet-20240229-v1:0")

response_body = json.loads(response.get("body").read())
print(response_body.get("content"))

See our client SDKs for more details, and the official Bedrock docs here.