From zero to your first real API response in under 30 minutes. We'll cover authentication, the SDK, and the core Messages API pattern.
API keys authenticate your requests and link usage to your billing account. Never commit them to version control.
.env file at your project root# .env (never commit this file — add it to .gitignore)
ANTHROPIC_API_KEY=sk-ant-api03-...
# .env.example (safe to commit — shows structure without secrets)
ANTHROPIC_API_KEY=your_api_key_here
Security rule: add .env to your .gitignore immediately. Exposed keys should be rotated on the console within minutes.
pip install anthropic python-dotenv tiktoken
my-project/
├── .env # secrets (gitignored)
├── .env.example # template (committed)
├── .gitignore
├── requirements.txt
├── demo/
│ └── first_call.py
└── notebooks/
└── explore.pdf
import anthropic
from dotenv import load_dotenv
load_dotenv() # loads ANTHROPIC_API_KEY from .env
client = anthropic.Anthropic()
# The SDK auto-reads ANTHROPIC_API_KEY from the environment
All calls go through client.messages.create(). Here is a minimal but complete request:
message = client.messages.create(
model="claude-haiku-4-5",
max_tokens=1024,
messages=[
{"role": "user", "content": "Explain recursion in one paragraph."}
]
)
print(message.content[0].text)
| Field | Type | Description |
|---|---|---|
content | list[ContentBlock] | The model's reply — access text via .content[0].text |
usage.input_tokens | int | Tokens in your prompt (billed) |
usage.output_tokens | int | Tokens in the response (billed) |
stop_reason | str | "end_turn", "max_tokens", or "tool_use" |
model | str | Exact model version used |
Anthropic offers a tiered family. Choose based on your latency, cost, and capability requirements.
| Model | Speed | Cost tier | Best use case |
|---|---|---|---|
| claude-haiku-4-5 | Fastest | $ | Classification, triage, simple Q&A, high-volume pipelines |
| claude-sonnet-4-5 | Fast | $$ | Drafting, analysis, coding tasks, most production workloads |
| claude-opus-4-5 | Slower | $$$$ | Complex reasoning, nuanced writing, tasks needing maximum accuracy |
Tip: Start prototyping with Haiku to keep costs low, then benchmark against Sonnet/Opus only where quality matters. Many tasks never need Opus.
For long outputs or real-time UX, streaming lets you display tokens as they arrive rather than waiting for the full response.
with client.messages.stream(
model="claude-haiku-4-5",
max_tokens=512,
messages=[{"role": "user", "content": "Write a haiku about APIs."}]
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
# After the loop, access usage:
final_message = stream.get_final_message()
print(f"\nInput tokens: {final_message.usage.input_tokens}")
print(f"Output tokens: {final_message.usage.output_tokens}")
Live walkthrough using demo/first_call.py.
.env is present — check with print(os.environ.get("ANTHROPIC_API_KEY", "MISSING")[:8])usage.input_tokens and usage.output_tokensmax_tokens to 10 — observe stop_reason="max_tokens"cost_estimator(prompt: str, model: str) -> float function that uses tiktoken to estimate the cost of a call before making it.system parameter of messages.create. Write a system prompt that makes the model always respond in bullet points, then test it with 3 different user messages.