Claude Python SDK Installation Guide (2026)
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.
Get started → Generate your project setup with our Project Starter.
Related Guides
Configure permissions → Build your settings with our Permission Configurator.
Try it: Paste your error into our Error Diagnostic for an instant fix.
- 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.
Related Articles
- Claude Code Pulumi Python Infrastructure Guide
- Claude Code Python SDK Package Guide
- How to Use Python Developers
Frequently Asked Questions
What is the minimum setup required?
You need Claude Code installed (Node.js 18+), a project with a CLAUDE.md file, and the relevant toolchain for your project type (e.g., npm for JavaScript, pip for Python). The CLAUDE.md file should describe your project structure, conventions, and common commands so Claude Code can work effectively.
How long does the initial setup take?
For a typical project, initial setup takes 10-20 minutes. This includes creating the CLAUDE.md file, configuring .claude/settings.json for permissions, and running a test task to verify everything works. Subsequent sessions start immediately because the configuration persists.
Can I use this with a team?
Yes. Commit your .claude/ directory and CLAUDE.md to version control so the entire team uses the same configuration. Each developer can add personal preferences in ~/.claude/settings.json (user-level) without affecting the project configuration. Review CLAUDE.md changes in pull requests like any other configuration file.
What if Claude Code produces incorrect output?
First check that your CLAUDE.md accurately describes your project conventions. Incorrect or outdated context is the most common cause of wrong output. If the output is still wrong, provide feedback in the same session — Claude Code learns from corrections within a conversation. For persistent issues, add explicit rules to CLAUDE.md (e.g., “Always use single quotes” or “Never modify files in the config/ directory”).