Reference · How-to · ~6 min
How to wire Anthropic Messages API
Last updated
Call Claude from Python with roles and a system prompt — first message in ~6 minutes.
Call Claude from Python with roles and a system prompt — first message in ~6 minutes.
Steps
1. **Install SDK** — `pip install anthropic`
2. **Set `ANTHROPIC_API_KEY`** — Server env only.
3. **Send roles** — `system` (optional) + alternating `user` / `assistant`.
4. **Pick model** — e.g. `claude-sonnet-4-20250514` (check docs for current IDs).
5. **Handle errors** — 429 rate limits → exponential backoff.
Copy-paste starter
from anthropic import Anthropic
client = Anthropic()
msg = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=512,
system="You are a concise tutor.",
messages=[{"role": "user", "content": "Explain RAG in one paragraph."}],
)
print(msg.content[0].text)Watch out
Anthropic message format differs from OpenAI — **don't mix SDKs** without mapping roles and tool blocks.
**See also:** `/reference/api/anthropic-messages` · snippet `anthropic-messages`