Skip to main content

How to Call the Claude API from Python with the Anthropic SDK

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

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​

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>"}],
)
PartWhat it means
anthropic.Anthropic()The client. With no arguments, it reads your key from the ANTHROPIC_API_KEY environment variable
modelWhich Claude model answers, such as claude-opus-5-5
max_tokensThe most tokens the reply may use, thinking included. Claude stops there if it hasn't finished
messagesThe conversation so far: a list of {"role": ..., "content": ...} turns, starting with the user
message.contentThe 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​

Terminal
uv init --bare --python 3.15 claude-api
cd claude-api
uv add anthropic
Output (excerpt)
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 =:

.env
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​

ask_claude.py
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 ask_claude.py script in a macOS-style code window: an Anthropic client sends one user message to claude-opus-5-5 with max_tokens 1024, and the loop prints every text block of the reply.

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):

Terminal
uv run --env-file .env ask_claude.py
Output
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.

The output in a terminal: Claude's one-sentence answer describing a carabiner as a metal loop with a spring-loaded gate used to connect ropes, harnesses and anchors.

Claude's answer will be worded differently each time you run it.

Common Mistakes​

MistakeWhat happensDo this instead
Running without the key in the environmentThe client can't authenticateRun with uv run --env-file .env, or set ANTHROPIC_API_KEY
Printing message.content[0].textThe first block can be a thinking block, which has no textPrint the blocks whose type is "text"
A max_tokens too small for the answerThe reply is cut offGive it room; it's a limit, not a target

Troubleshooting​

SymptomLikely causeFix
AuthenticationError with status 401The key is wrong, revoked or not loadedCheck the .env line and that you ran with --env-file .env
NotFoundError with status 404The model ID is misspelledUse 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​