Claude API Python SDK Quick Start Guide
If you're looking to get the Claude API working in Python as fast as possible, here's the short version: install anthropic, set an API key as an environment variable, and call client.messages.create(). That's the entire mental model. Everything else — streaming, system prompts, tool use, multi-turn conversations — builds on that one method call.
This guide walks through the actual setup, the first working script, and the handful of things that trip people up the first time (token limits, message formatting, and key management). By the end you'll have a script that sends a prompt and gets a real response back.
Installing the SDK
Anthropic maintains an official Python SDK. Install it with pip:
pip install anthropic
That's the only dependency you need to talk to the API. No framework, no boilerplate client setup beyond a key.
Getting an API key
You need an API key before anything works. If you already have Claude access through a subscription rather than a direct API account, you have two options:
- Sign up for API billing directly with Anthropic and pay per token.
- Use a service like SubToAPI that converts your existing Claude subscription into a standard HTTPS API key (
sub_live_...), so you skip separate API billing entirely.
Either way, once you have a key, set it as an environment variable rather than hardcoding it:
export ANTHROPIC_API_KEY="sk-ant-..."
If you're using SubToAPI instead, the setup is identical in shape — you just point the SDK's base URL at SubToAPI and use your sub_live_... key. More on that below.
Your first request
Here's the minimal working script using the official SDK:
import anthropic
client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY from env
response = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
messages=[
{"role": "user", "content": "Explain what a quick start guide is, in one sentence."}
],
)
print(response.content[0].text)
Run it with python app.py and you'll get a text response printed to your terminal. A few things worth noting:
max_tokensis required — there's no default, and leaving it too low will truncate responses.messagesis a list of role/content dicts, same pattern as other chat-based LLM APIs.- The response object has a
contentlist (usually one text block, but tool use responses add more blocks), plususagewith input/output token counts.
Adding a system prompt
System prompts are passed as a separate top-level parameter, not as a message:
response = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
system="You are a terse technical writer. Answer in bullet points only.",
messages=[
{"role": "user", "content": "What's the difference between a list and a tuple in Python?"}
],
)
This is one of the more common early mistakes — putting the system instructions inside the messages array as if it were a chat turn. Claude's API treats system as its own field.
Multi-turn conversations
Claude's API is stateless — there's no server-side session. You maintain conversation history yourself by appending to the messages list:
messages = [
{"role": "user", "content": "What's the capital of Portugal?"}
]
response = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=512,
messages=messages,
)
messages.append({"role": "assistant", "content": response.content[0].text})
messages.append({"role": "user", "content": "What's its population?"})
response2 = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=512,
messages=messages,
)
Every request sends the full history. There's no hidden context window being tracked for you — this matters for both cost and for debugging why Claude "forgot" something you never actually sent back.
Streaming responses
For anything user-facing, streaming avoids making people stare at a blank screen while a long response generates:
with client.messages.stream(
model="claude-sonnet-4-20250514",
max_tokens=1024,
messages=[{"role": "user", "content": "Write a short poem about Python."}],
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
The SDK handles the server-sent events parsing for you — text_stream yields plain text chunks as they arrive.
Using the SDK against SubToAPI instead
If your Claude access comes through a subscription and you're using SubToAPI to expose it as an API, you don't need a different library. The official SDK supports a custom base_url:
import anthropic
client = anthropic.Anthropic(
api_key="sub_live_your_key_here",
base_url="https://api.subtoapi.app/v1",
)
response = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
messages=[{"role": "user", "content": "Hello from SubToAPI"}],
)
Same SDK, same method calls, same response shape — only the base URL and key change. This is useful if you're already paying for Claude through a subscription and don't want to set up separate token-based billing just to build an app or internal tool. SubToAPI also adds usage metadata per key and team seat management, which is handy once more than one person or project is hitting the API. See the quickstart docs and messages reference for the full parameter list, or the streaming guide for SSE details.
Common early mistakes
A few things worth checking if your first script doesn't behave as expected:
- Model name typos. Model identifiers are versioned strings (e.g.
claude-sonnet-4-20250514), and a wrong one returns a 404, not a helpful fallback. - Forgetting
max_tokens. It's a required field, not optional with a sane default. - Alternating roles. The
messagesarray generally expects alternatinguser/assistantturns — twousermessages in a row without an assistant reply in between can cause errors. - Key exposure. Never commit API keys to source control. Load them from environment variables or a secrets manager.
Wrapping up
The Python SDK's surface area is small by design: one client, one messages.create() call, optional streaming and tool use layered on top. Once you've got the basic request/response loop working, adding system prompts, conversation history, and streaming is incremental — not a rewrite. If you're building on top of a Claude subscription rather than direct API billing, pointing the same SDK at SubToAPI gets you identical code with subscription-based access instead of a separate billing account.
Questions
Do I need a paid Anthropic API account to use the Python SDK? The SDK itself is free and open source — you need a valid API key to make requests, either from Anthropic directly or from a provider like SubToAPI that exposes subscription-based Claude access as a standard API key.
Why does my script return a 400 error about max_tokens? max_tokens is a required parameter in messages.create(). Unlike some other APIs, Claude's API won't apply a default — you must specify it explicitly for every request.
Can I use the same SDK for streaming and non-streaming requests? Yes. Use client.messages.create() for a single complete response, or client.messages.stream() as a context manager to receive the response as it's generated, token by token.