How to use Claude on AWS Bedrock
Quick answer
Use the
boto3 client for bedrock-runtime to call Claude models on AWS Bedrock by specifying the model ID like anthropic.claude-3-5-sonnet-20241022-v2:0 and passing chat messages in the converse or invoke_model method. Parse the response JSON to extract the generated text from response["output"]["message"]["content"][0]["text"].PREREQUISITES
Python 3.8+AWS CLI configured with credentialspip install boto3
Setup
Install the boto3 library and configure your AWS credentials with permissions to access Bedrock. Set your AWS region to one that supports Bedrock, such as us-east-1.
Run:
pip install boto3Ensure your AWS credentials are configured via ~/.aws/credentials or environment variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.
pip install boto3 Step by step
This example shows how to send a chat completion request to the Claude model on AWS Bedrock using boto3. It uses the converse method with the model ID anthropic.claude-3-5-sonnet-20241022-v2:0 and extracts the assistant's reply.
import boto3
client = boto3.client('bedrock-runtime', region_name='us-east-1')
messages = [
{"role": "user", "content": [{"type": "text", "text": "Hello Claude, how are you?"}]}
]
response = client.converse(
modelId="anthropic.claude-3-5-sonnet-20241022-v2:0",
messages=messages
)
# Extract the generated text
text = response['output']['message']['content'][0]['text']
print("Claude says:", text) output
Claude says: Hello! I'm doing well, thank you. How can I assist you today?
Common variations
- Using
invoke_modelmethod: You can also callinvoke_modelwith a JSON body for more control. - Changing model: Replace
modelIdwith other Claude versions available on Bedrock. - Streaming: Currently, Bedrock does not support streaming responses via
boto3.
import json
body = json.dumps({
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 512,
"messages": [
{"role": "user", "content": [{"type": "text", "text": "Explain AI in simple terms."}]}
]
})
response = client.invoke_model(
modelId="anthropic.claude-3-5-sonnet-20241022-v2:0",
body=body
)
result = json.loads(response['body'])
text = result['output']['message']['content'][0]['text']
print("Claude explains:", text) output
Claude explains: AI is a technology that allows machines to learn from data and perform tasks that usually require human intelligence.
Troubleshooting
- AccessDeniedException: Ensure your AWS IAM user or role has Bedrock permissions.
- Model not found: Verify the
modelIdis correct and available in your region. - Malformed request: Check the JSON structure of
messagesorbodymatches Bedrock API specs.
Key Takeaways
- Use boto3's bedrock-runtime client to call Claude models on AWS Bedrock.
- Pass chat messages as JSON with role and content arrays per Bedrock API format.
- Extract generated text from response['output']['message']['content'][0]['text'].
- Ensure AWS credentials and permissions are properly configured for Bedrock access.
- Use invoke_model for raw JSON requests and converse for structured chat calls.