Anthropic Claude API Guide 2025: Setup, Models & Pricing
A practical guide to the Anthropic API for developers — getting your API key, understanding the Claude model lineup, sending your first message, streaming, tool use, vision, and choosing between Claude and OpenAI.
Getting Your API Key
- Go to console.anthropic.com and sign in (or create an account)
- Navigate to API Keys in the left sidebar
- Click Create Key — give it a descriptive name
- Copy the key immediately — it begins with
sk-ant-api03-...and is shown only once - Go to Billing and add a payment method to access the API beyond the free trial credit
Security tip: Never hardcode your API key. Use environment variables: export ANTHROPIC_API_KEY=sk-ant-api03-... or a .env file excluded from git.
Claude Models (2025)
| Model | Context | Best for | Price (input / output per 1M tokens) |
|---|---|---|---|
| claude-haiku-4-5 | 200k | Fast tasks, classification, summarization | $0.80 / $4 |
| claude-sonnet-4-6 | 200k | Coding, analysis, best quality/price ratio | $3 / $15 |
| claude-opus-4-8 | 200k | Complex reasoning, research, agentic tasks | $15 / $75 |
| claude-fable-5 | 200k | Next-gen (preview, check availability) | — |
All Claude models share a 200k token context window — roughly 150,000 words or a full novel. For most production use cases, claude-sonnet-4-6 offers the best balance of quality and cost.
Messages API — First Request
# curl
curl https://api.anthropic.com/v1/messages \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-4-6",
"max_tokens": 1024,
"messages": [
{"role": "user", "content": "Explain REST APIs in 2 sentences."}
]
}' # Python (anthropic SDK)
pip install anthropic
import anthropic
client = anthropic.Anthropic() # uses ANTHROPIC_API_KEY env var
message = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
system="You are a helpful assistant.",
messages=[
{"role": "user", "content": "Explain REST APIs in 2 sentences."}
]
)
print(message.content[0].text)
print(f"Input tokens: {message.usage.input_tokens}")
print(f"Output tokens: {message.usage.output_tokens}") System Prompts & Multi-turn Conversations
Claude uses a separate system parameter (not a system message in the messages array). For multi-turn conversations, append both user and assistant turns to the messages array:
message = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
system="You are a senior Python developer. Be concise.",
messages=[
{"role": "user", "content": "What is a decorator?"},
{"role": "assistant", "content": "A decorator is a function that wraps another function."},
{"role": "user", "content": "Give me a simple example."}
]
) Streaming Responses
Streaming returns tokens as they generate — critical for chat UIs. Use stream=True or the async streaming context manager:
with client.messages.stream(
model="claude-sonnet-4-6",
max_tokens=1024,
messages=[{"role": "user", "content": "Tell me a joke."}]
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
# After streaming, access final message:
final_message = stream.get_final_message()
print(f"\nTotal tokens: {final_message.usage.input_tokens + final_message.usage.output_tokens}") Tool Use (Function Calling)
Define tools with JSON schema. Claude decides when to call them and outputs structured arguments:
tools = [{
"name": "get_weather",
"description": "Get current weather for a city",
"input_schema": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "City name"}
},
"required": ["city"]
}
}]
response = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
tools=tools,
messages=[{"role": "user", "content": "What's the weather in Paris?"}]
)
# Check if Claude wants to call a tool
if response.stop_reason == "tool_use":
tool_call = next(b for b in response.content if b.type == "tool_use")
print(f"Tool: {tool_call.name}, Args: {tool_call.input}") Vision (Image Input)
All Claude models support images. Pass them as base64-encoded data or via URL:
import base64
with open("screenshot.png", "rb") as f:
image_data = base64.b64encode(f.read()).decode()
response = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
messages=[{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/png",
"data": image_data
}
},
{"type": "text", "content": "What does this screenshot show?"}
]
}]
)
# Supported formats: JPEG, PNG, GIF, WebP — max 5MB per image, 20 images per request Rate Limits & Tiers
| Tier | Requirement | Sonnet RPM | Sonnet TPM |
|---|---|---|---|
| Free / Build | New account | 5 | 40k |
| Tier 1 | $5 paid | 50 | 50k |
| Tier 2 | $40 paid | 1,000 | 160k |
| Tier 3 | $200 paid | 2,000 | 320k |
| Tier 4 | $400 paid | 4,000 | 400k |
Limits apply per model per minute. There is also a concurrent request limit. Use exponential backoff on 429 errors and retry with a 60-second window on 529 overloaded errors.
Error Codes
| Code | Meaning | Fix |
|---|---|---|
| 401 | Invalid API key | Check key starts with sk-ant-api03- |
| 429 | Your rate limit exceeded | Exponential backoff; upgrade tier |
| 529 | API overloaded (global) | Retry with backoff; check Anthropic status |
| 500 | Anthropic server error | Retry after 30s |
| 400 | Bad request (e.g. prompt too long) | Check max_tokens and prompt length vs 200k limit |
For the 529 overloaded error specifically, see our detailed guide: Claude not working — 529 fix.
Claude API vs OpenAI API
| Factor | Anthropic Claude | OpenAI GPT |
|---|---|---|
| Context window | 200k (all models) | 128k (GPT-4o), 200k (o3) |
| Coding (SWE-bench) | Best-in-class (Sonnet 4.6) | Excellent (o3, o4-mini) |
| Prompt caching | Explicit — 80-90% savings | Automatic (smaller discount) |
| Image generation | ❌ Not available | ✅ DALL-E 3 |
| Audio/voice | ❌ Not available | ✅ Whisper + TTS |
| Tool use | ✅ Robust, explicit | ✅ Function calling |
| Best Sonnet-tier price | $3/$15 per 1M | $2.50/$10 per 1M (GPT-4o) |
For a deeper comparison, see OpenAI vs Anthropic API. For Anthropic's prompt caching details, see the Claude Code guide.
Monitor Anthropic API Status
The Anthropic API has degradations and 529 overload events that affect production apps. Prismix tracks real-time status, historical uptime, and sends instant alerts when the API becomes unavailable.
Monitor Anthropic Status Free →