Claude Python SDK Installation Guide
The Anthropic Python SDK is the fastest way to start building with Claude. This guide covers installation, platform-specific extras, and your first API call.
Quick Fix
Install the SDK and make your first call in under 2 minutes:
pip install anthropic
export ANTHROPIC_API_KEY="sk-ant-your-key-here"
import anthropic
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
messages=[{"role": "user", "content": "Hello, Claude"}]
)
print(message.content[0].text)
What You Need
- Python 3.9 or higher
- An Anthropic API key from the Console
- pip (included with Python)
Full Solution
Install the Base SDK
pip install anthropic
Install Platform-Specific Extras
If you are using Claude through AWS Bedrock, Google Vertex AI, or need better async performance:
# Amazon Bedrock support
pip install anthropic[bedrock]
# Google Vertex AI support
pip install anthropic[vertex]
# Improved async performance with aiohttp
pip install anthropic[aiohttp]
# Multiple extras at once
pip install anthropic[bedrock,vertex,aiohttp]
Set Your API Key
The SDK reads the ANTHROPIC_API_KEY environment variable automatically:
# Linux / macOS (add to ~/.bashrc or ~/.zshrc for persistence)
export ANTHROPIC_API_KEY="sk-ant-your-key-here"
# Windows PowerShell
$env:ANTHROPIC_API_KEY = "sk-ant-your-key-here"
Or pass it explicitly (not recommended for production):
import anthropic
client = anthropic.Anthropic(api_key="sk-ant-your-key-here")
Synchronous Usage
import anthropic
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
messages=[{"role": "user", "content": "Explain quantum computing in 3 sentences."}]
)
print(message.content[0].text)
print(f"Tokens: {message.usage.input_tokens} in, {message.usage.output_tokens} out")
Async Usage
from anthropic import AsyncAnthropic
client = AsyncAnthropic()
async def main():
message = await client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
messages=[{"role": "user", "content": "Hello"}]
)
print(message.content[0].text)
import asyncio
asyncio.run(main())
Async with aiohttp (Better Performance)
from anthropic import AsyncAnthropic, DefaultAioHttpClient
async def main():
async with AsyncAnthropic(http_client=DefaultAioHttpClient()) as client:
message = await client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
messages=[{"role": "user", "content": "Hello"}]
)
print(message.content[0].text)
import asyncio
asyncio.run(main())
Platform Integrations
Use Claude through cloud providers with their respective clients:
import anthropic
# Amazon Bedrock
bedrock_client = anthropic.AnthropicBedrock()
# Google Vertex AI
vertex_client = anthropic.AnthropicVertex(
project_id="your-gcp-project",
region="us-east5"
)
# Microsoft Foundry
foundry_client = anthropic.AnthropicFoundry()
Enable Debug Logging
ANTHROPIC_LOG=debug python your_script.py
Configure Retries and Timeouts
import anthropic
import httpx
# Custom retries (default: 2)
client = anthropic.Anthropic(max_retries=5)
# Custom timeout (default: 10 minutes)
client = anthropic.Anthropic(timeout=20.0)
# Fine-grained timeout control
client = anthropic.Anthropic(
timeout=httpx.Timeout(60.0, read=5.0, write=10.0, connect=2.0)
)
Prevention
- Pin your SDK version: Use
pip install anthropic==X.Y.Zin production to avoid breaking changes. - Use environment variables: Never hardcode API keys in source code.
- Use a virtual environment:
python -m venv .venv && source .venv/bin/activatebefore installing. - Check Python version: The SDK requires Python 3.9+. Run
python --versionto verify.
Related Guides
- Claude Python SDK Getting Started – your first project with error handling and streaming.
- How to Set ANTHROPIC_API_KEY for Claude – detailed API key setup across all platforms.
- Claude TypeScript SDK Installation Guide – the TypeScript equivalent of this guide.
- Claude API Error 401 authentication_error Fix – troubleshoot API key issues after installation.
- Claude SDK Timeout Configuration – tune retry and timeout settings for production.