Using Claude on AWS Bedrock with Python (boto3)

Call Claude via AWS Bedrock in Python using boto3. Covers bedrock-runtime invoke_model, streaming, IAM setup, and cost comparison vs direct Anthropic API.

💥 50p impulse-buy: Power Prompts PDF (first 10 buyers) 30 battle-tested Claude Code prompts · 8-page PDF · paste into CLAUDE.md and never re-type a prompt again · 50p impulse-buy, no commitment

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.

Option A — Anthropic SDK's built-in Bedrock client (recommended)

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.

Option B — raw boto3 (no Anthropic SDK dependency)

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"])

Streaming via boto3

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()

IAM policy (minimal)

{
  "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"
  }]
}

Bedrock vs direct Anthropic API

FactorAWS BedrockAnthropic API direct
AuthIAM roles / AWS credentialsAnthropic API key
Data residencyStays in your AWS region / VPCLeaves to api.anthropic.com
ComplianceHIPAA-eligible, SOC2, FedRAMP (region-dependent)Anthropic SOC2 / GDPR
Pricing~10–15% premium over direct API + no free tierPay-per-token, free-tier available
LatencyOften lower within AWS (same-region)Baseline — shorter hop if outside AWS
SDK supportAnthropicBedrock or raw boto3anthropic 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.

Frequently asked questions

Why use Claude on AWS Bedrock instead of the Anthropic API directly?
Bedrock keeps all API traffic inside AWS, so data never leaves your VPC — essential for HIPAA, SOC2, and FedRAMP workloads. You also pay through your existing AWS billing, use IAM roles instead of API keys, and can apply Service Control Policies across your org.
Does Bedrock support Claude streaming?
Yes. Use `invoke_model_with_response_stream` with the bedrock-runtime client. The response arrives as an EventStream of binary chunks you decode from JSON.
Which Claude models are available on Bedrock?
As of 2026: `anthropic.claude-3-5-sonnet-20241022-v2:0`, `anthropic.claude-3-5-haiku-20241022-v1:0`, `anthropic.claude-3-opus-20240229-v1:0`, and `anthropic.claude-3-sonnet-20240229-v1:0`. Model IDs differ from the direct Anthropic SDK — check the Bedrock console for the region-specific ARN.
Is the Bedrock request format different from the Anthropic SDK?
Yes. Bedrock takes a raw JSON body with `anthropic_version`, `max_tokens`, and `messages` — you call `invoke_model` with `body=json.dumps(payload)` and parse the response similarly. The Anthropic Python SDK now has a built-in Bedrock client (`AnthropicBedrock`) that handles this for you.
How do I set up IAM permissions for Bedrock?
Attach the `AmazonBedrockFullAccess` managed policy to your role/user, or create a minimal custom policy allowing `bedrock:InvokeModel` and `bedrock:InvokeModelWithResponseStream` on the model ARN. No Anthropic API key needed — Bedrock authenticates via AWS credentials.

Free tools

Cost Calculator → API Cookbook → Diff Summarizer → Skills Browser →

More examples

Claude API Python QuickstartClaude API Node.js / TypeScript QuickstartClaude API Streaming in PythonClaude API Streaming in Node.js / TypeScriptClaude API Tool Use in PythonClaude API Tool Use in Node.js / TypeScript