How to Call the Claude API from Python with the Anthropic SDK
The quickest way to use Claude from your own Python code is Anthropic's official SDK: one client, one call, and Claude's answer comes back as Python objects. This guide installs it, sends one question to Claude Opus 5.5 and prints the answer, with real output from anthropic 2.14.0.
Syntax
import anthropic
client = anthropic.Anthropic()
message = client.messages.create(
model="<model ID>",
max_tokens=<most tokens Claude may produce>,
messages=[{"role": "user", "content": "<your question>"}],
)
| Part | What it means |
|---|---|
anthropic.Anthropic() | The client. With no arguments, it reads your key from the ANTHROPIC_API_KEY environment variable |
model | Which Claude model answers, such as claude-opus-5-5 |
max_tokens | The most tokens the reply may use, thinking included. Claude stops there if it hasn't finished |
messages | The conversation so far: a list of {"role": ..., "content": ...} turns, starting with the user |
message.content | The reply: a list of content blocks. The answer is in the text blocks |
What It Costs
The Claude API is paid and has no free tier. Claude Opus 5.5 costs $4 per million input tokens and $20 per million output tokens (Anthropic's pricing). A one-sentence question and answer like this one uses a few hundred tokens or fewer, so each run costs a fraction of a cent.
Prerequisites
- A Claude API key. It comes from the Claude Console and is billed to your account.
- uv and Python 3.10 or newer. This guide uses uv 0.13.10 with Python 3.15.2.
Step 1: Create a Project and Install the SDK
uv init --bare --python 3.15 claude-api
cd claude-api
uv add anthropic
Initialized project `claude-api` at `/Users/you/source/claude-api`
Using CPython 3.15.2 interpreter at: /Library/Frameworks/Python.framework/Versions/3.15/bin/python3
Creating virtual environment at: .venv
Resolved 18 packages in 825ms
Installed 16 packages in 127ms
+ anthropic==2.14.0
uv init --bare makes a minimal project, a pyproject.toml and nothing else, and uv add anthropic installs the SDK with the 15 packages it needs.
Step 2: Put Your API Key in a .env File
Create .env in the project folder, with your own key after the =:
ANTHROPIC_API_KEY=your-api-key
If you don't have a key yet, How to Get a Claude API Key walks through the Console. Never put the key in your code, and add .env to .gitignore so it never reaches Git.
Step 3: Write the Script
import anthropic
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-opus-5-5",
max_tokens=1024,
messages=[{"role": "user", "content": "In one sentence, what is a carabiner used for?"}],
)
for block in message.content:
if block.type == "text":
print(block.text)

The loop prints only text blocks. Claude Opus 5.5 always thinks before it answers, and its reply can start with a thinking block whose text is empty by default, so printing message.content[0].text would fail on such a reply.
Step 4: Run It
uv run --env-file .env loads the key from the file for this one run (How to Load a .env File with uv run --env-file):
uv run --env-file .env ask_claude.py
A carabiner is a metal loop with a spring-loaded gate used to quickly and securely connect components, most commonly ropes, harnesses, and anchors in climbing and other safety-critical activities.

Claude's answer will be worded differently each time you run it.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Running without the key in the environment | The client can't authenticate | Run with uv run --env-file .env, or set ANTHROPIC_API_KEY |
Printing message.content[0].text | The first block can be a thinking block, which has no text | Print the blocks whose type is "text" |
A max_tokens too small for the answer | The reply is cut off | Give it room; it's a limit, not a target |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
AuthenticationError with status 401 | The key is wrong, revoked or not loaded | Check the .env line and that you ran with --env-file .env |
NotFoundError with status 404 | The model ID is misspelled | Use the exact ID, such as claude-opus-5-5 |
FAQ
Which model should I use?
Claude Opus 5.5 (claude-opus-5-5) is Anthropic's current Opus model. The models overview lists every model with its ID and price.
Can I pass the key in code instead?
anthropic.Anthropic(api_key=...) accepts one, but read it from the environment or a settings file rather than writing it into the code.
Conclusion
client.messages.create(model="claude-opus-5-5", ...) sent one question to Claude, and printing the text blocks of message.content showed its one-sentence answer.
Additional Resources
- Create a Message — every parameter and response field.
- Claude models overview — model IDs and prices.
- Anthropic Python SDK — installation, the client and its options.
