CODE · CLAUDE API · PYTHON

Claude API
in 5 minutes

5 lines of code that connect you to the Claude API. The basic snippet + a memory version + a model table — which model for which job.

5LINES
3MODELS
$0TO START

The full snippet

Start with pip install. Then an API key from the console. Then 5 lines of Python. Then decide if you want memory.

INSTALL

Steps 1 + 2 — install and set the key

Where do you get a key? console.anthropic.com → API Keys → Create Key. Never put the key in your code — always in an env variable.
# Step 1 — install (once)
pip install anthropic


# Step 2 — API key in an env variable

# Mac / Linux
export ANTHROPIC_API_KEY="sk-ant-api03-..."

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

# To make it permanent on Mac, add this to ~/.zshrc:
# echo 'export ANTHROPIC_API_KEY="sk-ant-..."' >> ~/.zshrc
CODE

Step 3 — the 5 basic lines

Save this as hello_claude.py and run with python hello_claude.py. If the key is set right — you'll get a reply in 2-3 seconds.
import anthropic

client = anthropic.Anthropic()  # picks up ANTHROPIC_API_KEY automatically

message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Hello, tell me a joke about programmers"}
    ]
)

print(message.content[0].text)
ADVANCED

Multi-turn conversation with memory

Three additions that change everything: a system parameter for persistent character, a conversation list that holds history, and pushing to that list after every reply. Without these three — every call is a fresh chat.
import anthropic

client = anthropic.Anthropic()
conversation = []

def chat(user_message):
    conversation.append({"role": "user", "content": user_message})
    response = client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=1024,
        system="You are a technical assistant. Always answer concisely and clearly.",
        messages=conversation
    )
    reply = response.content[0].text
    conversation.append({"role": "assistant", "content": reply})
    return reply

# Example conversation:
print(chat("What is an API?"))
print(chat("Now explain again, but shorter"))  # Claude remembers context
MODELS

Model table — which to pick

Rule of thumb: Sonnet for 90% of jobs. Haiku only when the task is simple and you have lots of calls. Opus only for complex code or deep analysis that has to be perfect.
| Model                          | When to use                                | Relative cost |
|--------------------------------|--------------------------------------------|---------------|
| claude-haiku-4-5-20251001      | Classification, short summaries, light jobs| × 1 (cheap)   |
| claude-sonnet-4-6              | Default for most cases                     | × 12          |
| claude-opus-4-6                | Complex code, deep analysis, important text| × 60          |

Three things that affect cost more than the model itself:

1. max_tokens — set the minimum that's enough, don't default to 4096
2. The length of the conversation history — it grows with every turn; refresh or trim
3. The size of the system prompt — sent on every call; cut it down to what's essential

How to use — step by step

Set up once. Then it runs by itself.

1

Install the SDK

pip install anthropic. Requires Python 3.8+. That's the entire setup.

2

Get an API key

console.anthropic.com → API Keys → Create Key. Don't share it, never put it in git. Always in an env variable.

3

Run the first snippet

The 5-line snippet above. Save as hello_claude.py. python hello_claude.py. You'll get a reply in 3 seconds.

4

Add memory to the chat

When you want a real multi-turn conversation — switch to the version with the conversation list and a system prompt. The difference between a tool and a coworker.

5

Pick a model per task

Sonnet by default. Haiku if the task is cheap. Opus when the job demands maximum quality. Full docs at docs.anthropic.com.

Want to stay in the loop?

Once a week — a new episode and a prompt or template like this one. No spam, unsubscribe anytime.

Your details are never shared.