Call Claude via AWS Bedrock in Python using boto3. Covers bedrock-runtime invoke_model, streaming, IAM setup, and cost comparison vs direct Anthropic API.
AWS Bedrock lets you call Claude models inside your AWS account — no Anthropic API key, no egress to api.anthropic.com, full IAM control. Enterprise teams on HIPAA, FedRAMP, or SOC2 audits often require this path.
pip install anthropic[bedrock] boto3
import anthropic
# Reads AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY / AWS_REGION from env
# or uses any boto3 credential source (instance profile, SSO, etc.)
client = anthropic.AnthropicBedrock(aws_region="us-east-1")
message = client.messages.create(
model="anthropic.claude-3-5-sonnet-20241022-v2:0",
max_tokens=1024,
messages=[{"role": "user", "content": "Summarize the risks of LLM prompt injection."}]
)
print(message.content[0].text)
AnthropicBedrock uses the same interface as anthropic.Anthropic — you can swap the client and keep the rest of your code identical.
import boto3, json
bedrock = boto3.client("bedrock-runtime", region_name="us-east-1")
payload = {
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 512,
"messages": [{"role": "user", "content": "What is AWS Bedrock?"}]
}
response = bedrock.invoke_model(
modelId="anthropic.claude-3-5-haiku-20241022-v1:0",
body=json.dumps(payload),
contentType="application/json",
accept="application/json"
)
body = json.loads(response["body"].read())
print(body["content"][0]["text"])
import boto3, json
bedrock = boto3.client("bedrock-runtime", region_name="us-east-1")
payload = {
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 2048,
"messages": [{"role": "user", "content": "Write a detailed security threat model."}]
}
response = bedrock.invoke_model_with_response_stream(
modelId="anthropic.claude-3-5-sonnet-20241022-v2:0",
body=json.dumps(payload),
contentType="application/json",
accept="application/json"
)
for event in response["body"]:
chunk = json.loads(event["chunk"]["bytes"])
if chunk.get("type") == "content_block_delta":
print(chunk["delta"].get("text", ""), end="", flush=True)
print()
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": [
"bedrock:InvokeModel",
"bedrock:InvokeModelWithResponseStream"
],
"Resource": "arn:aws:bedrock:us-east-1::foundation-model/anthropic.claude-3-5-sonnet-20241022-v2:0"
}]
}
| Factor | AWS Bedrock | Anthropic API direct |
|---|---|---|
| Auth | IAM roles / AWS credentials | Anthropic API key |
| Data residency | Stays in your AWS region / VPC | Leaves to api.anthropic.com |
| Compliance | HIPAA-eligible, SOC2, FedRAMP (region-dependent) | Anthropic SOC2 / GDPR |
| Pricing | ~10–15% premium over direct API + no free tier | Pay-per-token, free-tier available |
| Latency | Often lower within AWS (same-region) | Baseline — shorter hop if outside AWS |
| SDK support | AnthropicBedrock or raw boto3 | anthropic Python / JS SDK |
For cost estimation across Bedrock and direct API, paste your token counts into the Claude API Cost Calculator. For authentication patterns when using the direct API, see the Anthropic API authentication guide.