From typing once to running a thousand times — while you make coffee.
AI for Dummies Module 6
An API is how your code talks to AI instead of you. The model is the same — the door is programmable.
By the end of this lesson you’ll have run a real API call, understood what a token is, and know how to keep costs under control.
You type. You wait. You read the reply. You type again. One exchange at a time.
To summarise 1,000 customer reviews you’d need to paste them one by one, every day, forever.
Your program sends prompts. It collects replies. You’re not even at the keyboard.
The same 1,000 reviews: a small Python loop, a coffee break, and a spreadsheet of results waiting for you.
Rule of thumb: if you’d paste the same prompt more than a handful of times, the API is the right door. Learn it once; use it everywhere.
Same restaurant. Same chef. Same food. Just a different, programmable doorway.
Every language — curl, Python, JavaScript — wraps this same HTTP round-trip. The shape never changes.
⚠️ Leaked keys get used by strangers. Your account gets the bill. Keys slip onto GitHub every day — people paste them into code they then commit. That’s how cloud bills appear out of nowhere.
os.environ or the SDK auto-reads itexport ANTHROPIC_API_KEY=sk-ant-abc123 — add it to ~/.zshrc to make it permanent. Windows: System Properties → Environment Variables.
No library to install. curl is on every Mac and Linux machine already.
curl https://api.anthropic.com/v1/messages \ # ← the endpoint
-H "x-api-key: $ANTHROPIC_API_KEY" \ # ← key from env var, never in plain text
-H "anthropic-version: 2023-06-01" \ # ← API version header (required)
-H "content-type: application/json" \
-d '{
"model": "claude-sonnet-4-6", # ← which model to use
"max_tokens": 1024, # ← ceiling on reply length
"messages": [
{"role": "user", "content": "Explain what an API is in one sentence."}
]
}'
Install once: pip install anthropic. Then this is a complete working program.
import anthropic
client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY from environment automatically
message = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
messages=[
{"role": "user", "content": "Write a friendly 2-line welcome email."}
],
)
print(message.content[0].text) # ← the model's reply, as a plain string
create() call and you can process a spreadsheet of inputs while you sleep.You send: model, max_tokens, your messages array — as JSON over HTTPS.
You receive: message.content[0].text — the reply text. Plus usage: how many tokens were used (for billing).
Total round-trip for a short reply: typically 1–3 seconds. For streaming responses you can receive tokens as they're generated — but that’s a later lesson.
☁️ Cloud (Claude via Anthropic)
import anthropic
client = anthropic.Anthropic()
msg = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=256,
messages=[{"role":"user",
"content":"Hello!"}],
)
print(msg.content[0].text)
🏠 Local (Ollama — free, offline)
from openai import OpenAI
client = OpenAI(
base_url="http://localhost:11434/v1",
api_key="ollama", # any string
)
resp = client.chat.completions.create(
model="llama3.2",
messages=[{"role":"user",
"content":"Hello!"}],
)
print(resp.choices[0].message.content)
Notice: the structure is almost identical. Change the base_url, the model name, and the SDK — the rest of your code stays the same. Learn the shape once.
Runs in the background (terminal). Default port:
base_url="http://localhost:11434/v1"
model="llama3.2" # or gemma3, mistral…
GUI app — start the local server from the sidebar. Default port:
base_url="http://localhost:1234/v1"
model="lmstudio-community/Meta-Llama-3…"
Covered in depth in Lesson 04. Revisiting it here because it unlocks free, private API calls for any volume.
Cloud APIs don’t charge per message or per word — they charge per token. Here’s what that means in practice:
Rule of thumb: 1 token ≈ ¾ of a word ≈ 4 characters
| Text | Tokens | Why |
|---|---|---|
| hello | 1 | Common short word — single token |
| antidisestablishmentarianism | 6 | Long rare word split into sub-word pieces |
| 😊 (emoji) | 1–3 | Emojis are multi-byte UTF-8 sequences |
| The cat sat on a mat | 6 | 6 words ≈ 6 tokens (short common words) |
You pay for tokens both ways: what you send (input) and what the model generates (output). A typical short reply uses a few hundred tokens — a fraction of a cent. The cost is real when you loop over thousands of items.
Prices are per million tokens. Same idea, very different scale.
Input / output rates. Check provider docs for current prices.
💰 The $10 lesson: a loop with a small bug once ran 50,000 API calls in 20 minutes. The monthly spending limit stopped it at $10 instead of $500. It takes 30 seconds to set. Set it now.
How to set it in the Anthropic Console:
Also limit at the code level:
# Always set max_tokens explicitly!
# Without it, the model can return
# thousands of tokens per call.
message = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=256, # ← hard ceiling
messages=[...]
)
max_tokens in code (limits each individual call’s output length).
🏠 Running Ollama or LM Studio?
The token count is irrelevant. Cost = $0.
Smart path: prototype with the cloud (no setup), switch to local once cost or privacy is the bottleneck. Because both use the same API format, the switch is one URL change.
This is the most common beginner use-case. The loop is the whole idea.
import anthropic, csv
client = anthropic.Anthropic()
results = []
with open("reviews.csv") as f:
for row in csv.DictReader(f): # ← iterate each row
msg = client.messages.create(
model="claude-haiku-4-5", # ← cheapest model for bulk work
max_tokens=20, # ← short reply: just "positive" / "negative"
messages=[{"role": "user", "content":
f"Classify as positive/negative: {row['review_text']}"
}],
)
results.append({
"id": row["id"],
"label": msg.content[0].text.strip(),
})
print(f"Done. {len(results)} rows classified.")
time.sleep(0.1) between calls if you hit rate limits. Or use the Batch API for large jobs — that’s a later lesson.pip install anthropicmax_tokens in every call — cap each reply’s length🎯 Week 3 milestone: touch the API. Run the Python example. Process a small batch of something real. That’s the goal.