Advertisement

How to Use the Claude API with Python

The complete beginner guide. Get your API key, understand what things actually cost, and run your first AI script in 10 minutes. No prior AI experience needed.

What you'll need: A computer, Python 3.8+ installed, and a credit card for the API account (you won't be charged until you exceed $5 of usage, and most scripts cost fractions of a cent).

What is the Claude API?

Claude is an AI model made by Anthropic. The API lets you send text to Claude and get a response back - from your own code, on your own machine, without going through any middleman SaaS tool.

Every "AI-powered" SaaS tool you've seen that charges $20-99/month is doing exactly this - calling the Claude API (or OpenAI's API) behind the scenes, marking it up 50-1000x, and charging you a subscription. When you use the API directly, you cut out the middleman and pay only the raw cost: around $0.001 per 1,000 words.

Step 1 - Get your free API key

  1. Go to console.anthropic.com and create an account
  2. Add a payment method (required, but you won't be charged until you use $5+ worth)
  3. Go to API Keys in the left sidebar
  4. Click Create Key, give it a name, copy it
  5. Your key starts with sk-ant-api03-... - treat it like a password

New accounts receive free credits. After that it's strictly pay-as-you-go - no monthly fee, no minimum spend. If you run one script a week, your bill might be $0.02/month.

Step 2 - Install Python and the SDK

If you don't have Python yet, download it from python.org (get 3.10 or newer). Then install the Anthropic SDK:

pip install anthropic

That's the only dependency you need to call Claude. Individual tools may require extra packages (like pdfplumber for the PDF tool) but the core SDK is just one install.

Step 3 - Run your first script

Create a file called hello.py and paste this:

import anthropic

client = anthropic.Anthropic(api_key="sk-ant-YOUR_KEY_HERE")

message = client.messages.create(
    model="claude-haiku-4-5-20251001",
    max_tokens=200,
    messages=[
        {"role": "user", "content": "What is the Claude API, in 2 sentences?"}
    ]
)

print(message.content[0].text)

Run it:

python hello.py

You'll get a response in 1-2 seconds. That API call cost approximately $0.00003 - three thousandths of a cent.

Step 4 - Keep your API key safe

Never paste your key directly into a script you share or commit to GitHub. Use an environment variable instead:

# On Mac/Linux - add to ~/.bashrc or ~/.zshrc
export ANTHROPIC_API_KEY="sk-ant-..."

# On Windows (Command Prompt)
set ANTHROPIC_API_KEY=sk-ant-...

# On Windows (PowerShell)
$env:ANTHROPIC_API_KEY="sk-ant-..."

Then in your Python code:

import os
import anthropic

client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

All tools on this site accept --api-key as a command line argument so your key never touches the source code:

python scraper.py --api-key sk-ant-...

Which model should I use?

Anthropic offers several Claude models at different price points. For most everyday tasks - summarization, classification, extraction, writing - claude-haiku is the right choice. It's dramatically cheaper and still extremely capable.

Model Speed Cost per 1M tokens Best for
claude-haiku-4-5-20251001 Fastest ~$0.80 in / $4 out Bulk tasks, summaries, classification, extraction
claude-sonnet-4-6 Fast ~$3 in / $15 out Complex writing, nuanced analysis, coding
claude-opus-4-6 Slower ~$15 in / $75 out Hardest tasks, deep reasoning, research

A "token" is roughly 0.75 words. A typical 500-word article is about 700 tokens. At haiku prices, summarizing it costs around $0.003 - less than a third of a cent.

Real cost examples

Here's what common tasks actually cost when you call the API directly:

Compare that to a SaaS tool doing the same thing for $29/month. The math is brutal: you'd need to run the equivalent of thousands of operations per month before it made financial sense to pay for a subscription tool.

How to use it with any tool on this site

Every tool on getaitools.dev accepts --api-key. Clone the repo, install requirements, run the script:

# Reddit Scraper with AI summaries
git clone https://github.com/Get-Ai-Tools/reddit-scraper
cd reddit-scraper
pip install -r requirements.txt
python scraper.py --subreddit python --summarize --api-key sk-ant-...

# PDF Summarizer
git clone https://github.com/Get-Ai-Tools/pdf-summarizer
cd pdf-summarizer
pip install -r requirements.txt
python summarizer.py --pdf report.pdf --api-key sk-ant-...

# Cold Email Writer
git clone https://github.com/Get-Ai-Tools/cold-email-writer
cd cold-email-writer
pip install -r requirements.txt
python writer.py --leads leads.csv --api-key sk-ant-...

Common errors and fixes

AuthenticationError: invalid x-api-key

Your API key is wrong or has extra spaces. Copy it fresh from console.anthropic.com and make sure there's no whitespace.

RateLimitError

You're sending requests too fast. New accounts have lower rate limits. Add time.sleep(0.5) between requests or upgrade your usage tier in the Anthropic console.

ModuleNotFoundError: No module named 'anthropic'

Run pip install anthropic in the same environment where you're running the script. If you use multiple Python versions, try pip3 install anthropic.

My script ran but I got charged nothing?

Normal - small scripts cost so little they round to $0.00 in the billing dashboard. Check the Usage tab in console.anthropic.com for actual token counts.

Next steps