
You’ve likely used Claude Code as an assistant by now, but if you were to integrate Anthropic’s suite of models into your code directly, you can’t do this with Claude Code.
However, Anthropic does provide a SDK (software development kit) for engineers. They provide it in many different languages, including Python (official documentation).
Installing Anthropic SDK
Just like every other package, you can install it using pip or uv:
pip install anthropic
uv add anthropicAuthentication
Once you sign up for your account on their website, you’ll be able to authenticate by passing in your API key to the client via the ANTHROPIC_API_KEY environment variable.
It’s very important to remember to never check this API key into source code. Leverage a .env file and add it to your .gitignore file! Never hardcode your API key.
Creating an API call
The core of all of your calls to Anthropic lie within the client.messages.create() method:
import os
from anthropic import Anthropic
client = Anthropic() # reads ANTHROPIC_API_KEY from env
message = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
messages=[
{
"role": "user",
"content": "Explain what a decorator is in Python."
}
]
)
print(message.content[0].text)There’s a few key parameters to mention:
model- This is the model that you want to use. Good defaults areclaude-sonnet-4-6for something that’s generally well-balanced and of good capability. You can see which models are available on their models overview page.max_tokens- A required parameter that caps the length of the response.messages- This is one of the more important arguments; I go into detail below as to what this is and how you go about structuring it.system- A top-level string that set’s Claude for the whole conversation. This is a great spot to define things like personas, output formats, or constraints.temperature- Controls the “randomness” in the responses, ranging from 0.0 to 1.0. You’re going to want to set a value closer to 0.0 for analytical or multiple choice tasks, and closer to 1.0 for creative/generative tasks.stream- Set this toTrueif you want to receive responses as they’re generated rather than waiting for the full response.
The messages parameter
At it’s core, this parameter is a list of dictionaries, where each dictionary represents one turn in a conversation.
There are 2 keys in each dictionary: role and content. There are 2 roles: user (you/your app) and assistant (Claude):
messages=[
{"role": "user", "content": "Hi Claude!"},
{"role": "assistant", "content": "Hello! How can I help?"},
{"role": "user", "content": "What is a list comprehension?"},
]It’s very important to understand that the Messages API is stateless, meaning that you must send the full conversation history with every request.
If you’re used to Claude Code (or the Claude desktop app), then this might take some slight adjustment, as those interfaces essentially “remember” the conversation as you interact with it.
Example of using the API
Let’s say that we’re working with a super messy CSV file that can’t be done with Pandas or Polars. We could feed this into a LLM to help us re-structure it so that it’s Polars/Pandas friendly:
import os
from anthropic import Anthropic
client = Anthropic() # reads ANTHROPIC_API_KEY from env
SYSTEM_PROMPT = """"You are a data cleaning assistant.
Your only job is to fix the given CSV so it can be read
by Pandas or Polars. Return ONLY the cleaned CSV data -
no explanation, no markdown, no code fences.
"""
with open("unstructured_data.csv", "r") as f:
csv_file = f.read()
CONTENT = f"Here is the CSV file data. Please fix this. {csv_file}"
message = client.messages.create(
model = "claude-sonnet-4-6",
max_tokens = 1000,
system = SYSTEM_PROMPT,
messages = [
{
"role": "user",
"content": CONTENT
}
]
)
cleaned = message.content[0].text
with open("cleaned_data.csv", "w") as f:
f.write(cleaned_csv)Happy coding!
📧 Join the Python Snacks Newsletter! 🐍
Want even more Python-related content that’s useful? Here’s 3 reasons why you should subscribe the Python Snacks newsletter:
Get Ahead in Python with bite-sized Python tips and tricks delivered straight to your inbox, like the one above.
Exclusive Subscriber Perks: Receive a curated selection of up to 6 high-impact Python resources, tips, and exclusive insights with each email.
Get Smarter with Python in under 5 minutes. Your next Python breakthrough could just an email away.
You can unsubscribe at any time.
Interested in starting a newsletter or a blog?
Do you have a wealth of knowledge and insights to share with the world? Starting your own newsletter or blog is an excellent way to establish yourself as an authority in your field, connect with a like-minded community, and open up new opportunities.
If TikTok, Twitter, Facebook, or other social media platforms were to get banned, you’d lose all your followers. This is why you should start a newsletter: you own your audience.
This article may contain affiliate links. Affiliate links come at no cost to you and support the costs of this blog. Should you purchase a product/service from an affiliate link, it will come at no additional cost to you.

