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.
Start with pip install. Then an API key from the console. Then 5 lines of Python. Then decide if you want memory.
# 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
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)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| 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
Set up once. Then it runs by itself.
pip install anthropic. Requires Python 3.8+. That's the entire setup.
console.anthropic.com → API Keys → Create Key. Don't share it, never put it in git. Always in an env variable.
The 5-line snippet above. Save as hello_claude.py. python hello_claude.py. You'll get a reply in 3 seconds.
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.
Sonnet by default. Haiku if the task is cheap. Opus when the job demands maximum quality. Full docs at docs.anthropic.com.
Once a week — a new episode and a prompt or template like this one. No spam, unsubscribe anytime.
Your details are never shared.