Integrating Anthropic Claude API with Amazon Bedrock: A Complete Developer Guide
You can integrate Anthropic Claude with Amazon Bedrock by using the AnthropicBedrock client from the official Python SDK, which automatically handles AWS SigV4 signing and exposes the same Messages API used in direct Anthropic API calls.
The anthropics/prompt-eng-interactive-tutorial repository provides a complete Bedrock edition that demonstrates integrating Anthropic Claude API with Amazon Bedrock through hands-on Jupyter notebooks. This implementation allows developers to leverage Claude's advanced reasoning capabilities while utilizing AWS infrastructure for enterprise-grade security and scalability.
How the AnthropicBedrock Client Works
The integration centers on the AnthropicBedrock class, a thin wrapper around AWS Bedrock's InvokeModel API. As implemented in the tutorial notebooks under AmazonBedrock/anthropic/, this client automatically handles SigV4 request signing and payload formatting required by AWS.
When you instantiate AnthropicBedrock(aws_region=AWS_REGION), the SDK discovers AWS credentials from your runtime environment—whether through environment variables, AWS config files, or a SageMaker execution role. The client then builds Bedrock request payloads matching Claude's Messages API schema, allowing seamless migration between direct Anthropic API calls and Bedrock deployments.
Environment Setup and Configuration
Installing the Required SDK
The repository pins the exact SDK version in AmazonBedrock/requirements.txt to ensure compatibility with the latest Bedrock APIs:
anthropic==0.21.3
Install this dependency in your virtual environment before running any notebooks:
pip install -r AmazonBedrock/requirements.txt
Configuring AWS Credentials
The AnthropicBedrock client supports standard AWS credential chains. You can authenticate via:
- Environment variables:
AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY, and optionallyAWS_SESSION_TOKEN - AWS config files: Credentials stored in
~/.aws/credentialsand~/.aws/config - SageMaker execution role: When using the provided CloudFormation template, the notebook instance assumes a role with
AmazonBedrockFullAccess
Automated Deployment with CloudFormation
For production-ready environments, the repository includes AmazonBedrock/cloudformation/workshop-v1-final-cfn.yml. This template provisions a SageMaker notebook instance pre-configured with:
- IAM role granting
AmazonBedrockFullAccess - Encrypted storage via KMS
- Pre-installed dependencies from
requirements.txt
Implementing the Bedrock Messages API
The tutorial implements a reusable helper function get_completion in AmazonBedrock/anthropic/01_Basic_Prompt_Structure.ipynb (lines 42-52). This function encapsulates the client.messages.create call with Bedrock-specific parameters.
Here is the complete implementation pattern:
import os
from anthropic import AnthropicBedrock
# Configure region and model identifier
AWS_REGION = os.getenv("AWS_REGION", "us-east-1")
MODEL_NAME = "anthropic.claude-3-haiku-20240307-v1:0"
# Initialize the Bedrock client
client = AnthropicBedrock(aws_region=AWS_REGION)
def get_completion(user_prompt: str, system: str = "") -> str:
"""
Send a prompt to Claude via Bedrock and return the text response.
"""
response = client.messages.create(
model=MODEL_NAME,
max_tokens=1024,
temperature=0.0,
messages=[{"role": "user", "content": user_prompt}],
system=system,
)
# response.content is a list of text blocks; join them for convenience
return "".join(block.text for block in response.content if hasattr(block, "text"))
# Example usage
if __name__ == "__main__":
system_prompt = "You are a concise technical assistant."
user_query = "Explain the difference between precision and recall."
answer = get_completion(user_query, system=system_prompt)
print(answer)
Critical implementation details:
- Model identifiers for Bedrock use the full ARN suffix format (e.g.,
anthropic.claude-3-haiku-20240307-v1:0), not the short names used in the direct Anthropic API. - The
systemparameter accepts a string that sets Claude's behavior across the conversation, implemented as a top-level parameter in the Messages API. - Response parsing requires iterating over
response.contentblocks, as Bedrock returns content as a list of text blocks rather than a single string.
Key Repository Files for Bedrock Integration
The anthropics/prompt-eng-interactive-tutorial repository organizes Bedrock-specific resources under the AmazonBedrock/ directory:
AmazonBedrock/requirements.txt– Pinsanthropic==0.21.3to ensure theAnthropicBedrockclient is available.AmazonBedrock/anthropic/01_Basic_Prompt_Structure.ipynb– Contains the foundationalget_completionhelper and demonstrates client initialization.AmazonBedrock/cloudformation/workshop-v1-final-cfn.yml– Infrastructure-as-code for deploying a SageMaker environment with pre-configured Bedrock permissions.AmazonBedrock/utils/hints.py– Supporting utilities for tutorial exercises, packaged with the Bedrock edition.
Summary
Integrating Anthropic Claude API with Amazon Bedrock requires understanding the AnthropicBedrock client wrapper and the Bedrock-specific Messages API implementation:
- Use
AnthropicBedrockfrom theanthropicPython SDK to handle AWS SigV4 signing and endpoint management automatically. - Reference the correct model identifiers (e.g.,
anthropic.claude-3-haiku-20240307-v1:0) when callingclient.messages.create. - Leverage the
get_completionpattern established inAmazonBedrock/anthropic/01_Basic_Prompt_Structure.ipynbto standardize API calls across your application. - Deploy via CloudFormation to provision SageMaker instances with the necessary
AmazonBedrockFullAccessIAM permissions and encrypted storage.
Frequently Asked Questions
What is the difference between AnthropicBedrock and the standard Anthropic client?
The AnthropicBedrock client is a specialized wrapper that routes requests through Amazon Bedrock's InvokeModel API rather than directly to Anthropic's API endpoints. It automatically handles AWS SigV4 request signing, uses Bedrock-specific model identifiers (ARN suffixes), and discovers credentials through standard AWS credential chains. The standard Anthropic client connects directly to api.anthropic.com and requires an Anthropic API key.
Which Claude models are available through Amazon Bedrock?
Amazon Bedrock supports multiple Claude model versions, including Claude 3 Haiku, Claude 3 Sonnet, and Claude 3 Opus, as well as Claude 2 and Claude 2.1. When using the AnthropicBedrock client, you must specify the full model identifier string, such as anthropic.claude-3-haiku-20240307-v1:0 or anthropic.claude-3-sonnet-20240229-v1:0. These identifiers correspond to the ARN suffixes used in the Bedrock console and API.
How do I handle authentication when running Claude on Bedrock in a SageMaker notebook?
When deploying via the provided CloudFormation template (AmazonBedrock/cloudformation/workshop-v1-final-cfn.yml), the SageMaker notebook instance assumes an IAM execution role that includes the AmazonBedrockFullAccess policy. The AnthropicBedrock client automatically discovers these credentials through the AWS SDK's default credential provider chain. If running locally, you must configure credentials via environment variables (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) or the ~/.aws/credentials file with appropriate Bedrock invocation permissions.
Can I use the same prompt engineering techniques from the direct API when calling Claude through Bedrock?
Yes, the prompt engineering techniques demonstrated in the anthropics/prompt-eng-interactive-tutorial repository apply identically whether using the direct Anthropic API or Amazon Bedrock. The get_completion helper function in AmazonBedrock/anthropic/01_Basic_Prompt_Structure.ipynb accepts the same parameters: user_prompt, system prompts, max_tokens, and temperature. The Bedrock integration uses the same Messages API schema, ensuring consistent behavior for chain-of-thought prompting, few-shot examples, and structured output techniques across both deployment options.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →