The official Python library for the anthropic API
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Use Claude models via AWS Bedrock with automatic authentication and region configuration.
pip install anthropic[bedrock]class AnthropicBedrock:
"""Synchronous client for Claude on AWS Bedrock."""
def __init__(
self,
*,
aws_access_key: str | None = None,
aws_secret_key: str | None = None,
aws_session_token: str | None = None,
aws_region: str | None = None,
aws_profile: str | None = None,
timeout: float | httpx.Timeout = DEFAULT_TIMEOUT,
max_retries: int = DEFAULT_MAX_RETRIES,
default_headers: dict[str, str] | None = None,
http_client: httpx.Client | None = None,
):
"""
Initialize Bedrock client.
Parameters:
aws_access_key: AWS access key ID (or AWS_ACCESS_KEY_ID env var)
aws_secret_key: AWS secret access key (or AWS_SECRET_ACCESS_KEY env var)
aws_session_token: AWS session token (or AWS_SESSION_TOKEN env var)
aws_region: AWS region (or AWS_REGION env var, default: us-east-1)
aws_profile: AWS profile from ~/.aws/credentials
timeout: Request timeout
max_retries: Maximum retry attempts
default_headers: Custom headers
http_client: Custom httpx.Client
"""
...
class AsyncAnthropicBedrock:
"""Asynchronous client for Claude on AWS Bedrock."""
# Same parameters as AnthropicBedrock
...# Claude 3.5 on Bedrock
"anthropic.claude-3-5-sonnet-20241022-v2:0"
"anthropic.claude-3-5-sonnet-20240620-v1:0"
"anthropic.claude-3-5-haiku-20241022-v1:0"
# Claude 3 on Bedrock
"anthropic.claude-3-opus-20240229-v1:0"
"anthropic.claude-3-sonnet-20240229-v1:0"
"anthropic.claude-3-haiku-20240307-v1:0"from anthropic import AnthropicBedrock
client = AnthropicBedrock() # Uses environment variables
message = client.messages.create(
model="anthropic.claude-3-5-sonnet-20241022-v2:0",
max_tokens=1024,
messages=[{"role": "user", "content": "Hello!"}]
)client = AnthropicBedrock(
aws_access_key="AKIA...",
aws_secret_key="wJal...",
aws_region="us-east-1"
)client = AnthropicBedrock(
aws_profile="production",
aws_region="us-west-2"
)with client.messages.stream(
model="anthropic.claude-3-5-sonnet-20241022-v2:0",
max_tokens=1024,
messages=[{"role": "user", "content": "Write a story"}]
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)regions = ["us-east-1", "us-west-2", "eu-west-1"]
for region in regions:
client = AnthropicBedrock(aws_region=region)
message = client.messages.create(...)
client.close()import asyncio
from anthropic import AsyncAnthropicBedrock
async def main():
client = AsyncAnthropicBedrock(aws_region="us-east-1")
message = await client.messages.create(...)
await client.close()
asyncio.run(main())AWS_ACCESS_KEY_ID - AWS access keyAWS_SECRET_ACCESS_KEY - AWS secret keyAWS_SESSION_TOKEN - AWS session tokenAWS_REGION - AWS region (default: us-east-1)