API Setup Guide

Every lab in this course calls a large language model through an API. An API key is a secret string that identifies your account to the model provider and bills your usage to it. Each student issues their own key; keys are personal credentials and are never shared or committed to git.

Labs run in Google Colab — you need a Google account and one of the providers below. Option A is the course default.

Option Provider Default model Cost When to choose
A OpenAI openai:gpt-4o-mini prepaid, ≈ $5 covers the semester Course default — pick this if unsure
B Anthropic anthropic:claude-haiku-4-5 prepaid, ≈ $5 covers the semester You already have an Anthropic account, or prefer Claude
C Ollama (local machine only) ollama:llama3.2 free No card — but you must then run labs locally, not in Colab

All labs run identically on any option — the model is selected by a single DOCQA_MODEL string, and the lab code never changes.

Option A — OpenAI (default)

  1. Create an account at https://platform.openai.com/signup. This is the developer platform; a ChatGPT subscription is a separate product and does not include API credit.
  2. Add prepaid credit: Settings → Billing → Add payment details, then purchase the minimum credit (currently $5). API access requires a positive credit balance; without it, every call fails with insufficient_quota.
  3. Set a usage limit (recommended): Settings → Limits, set a monthly budget of $5. This caps the damage if a key ever leaks.
  4. Create a key: API keys → Create new secret key. Name it stml2026. The key (starts with sk-) is shown once — copy it immediately.
  5. Register the key in Colab (see Register your key in Colab below).

Option B — Anthropic

  1. Create an account at https://console.anthropic.com. As with OpenAI, this developer console is separate from a Claude.ai subscription.
  2. Add prepaid credit: Settings → Billing, purchase the minimum credit (currently $5).
  3. Create a key: Settings → API keys → Create key. Name it stml2026; the key (starts with sk-ant-) is shown once — copy it immediately.
  4. Register the key in Colab as ANTHROPIC_API_KEY, and add a second secret DOCQA_MODEL with the value anthropic:claude-haiku-4-5 (see the next section).

Option C — Ollama (local machine only, free)

Ollama runs an open-weights model on your own machine — no account, no key, no card. It does not work in Colab, so choosing this option means running the lab notebooks locally in Jupyter instead (see Alternative: running locally below). Quality is below the hosted models and answers in comparison exercises will differ from reference outputs, but every lab completes.

  1. Install Ollama from https://ollama.com (macOS/Windows/Linux).
  2. Download the course model (~2 GB, one time): ollama pull llama3.2
  3. Requirement: 8 GB RAM or more. If your machine cannot run it, use Option A.

Register your key in Colab

Colab stores keys in Secrets — encrypted, per-account, invisible to anyone you share a notebook with. Do this once; every course notebook then finds the key automatically.

  1. Open any notebook at https://colab.research.google.com.
  2. Click the key icon (Secrets) in the left sidebar.
  3. Add new secret — Name: OPENAI_API_KEY (Option A) or ANTHROPIC_API_KEY (Option B). Value: your key, pasted exactly.
  4. Option B only: add a second secret — Name: DOCQA_MODEL, Value: anthropic:claude-haiku-4-5.
  5. Toggle Notebook access on. Colab asks again per notebook the first time it reads the secret — grant it.

Never paste the key itself into a notebook cell. Secrets exist precisely so the key is not part of the notebook file you submit.

Verify

In a fresh Colab cell:

import subprocess, sys, os
subprocess.run([sys.executable, '-m', 'pip', 'install', '-q', 'aisuite[openai,anthropic]'], check=True)
from google.colab import userdata
for k in ('OPENAI_API_KEY', 'ANTHROPIC_API_KEY', 'DOCQA_MODEL'):
    try:
        v = userdata.get(k)
        if v: os.environ[k] = v
    except Exception:
        pass
import aisuite
model = os.environ.get('DOCQA_MODEL', 'openai:gpt-4o-mini')
r = aisuite.Client().chat.completions.create(model=model,
        messages=[{'role': 'user', 'content': 'Reply with exactly: ready'}])
print(r.choices[0].message.content)

Expected output: ready (or a close variant). If this prints, your setup is complete and Week 1’s notebook will run.

Alternative: running locally (Option C, or by preference)

With Python 3.10+ on your own machine: clone/download the course labs/ folder, then

cd labs
python -m venv .venv && source .venv/bin/activate   # Windows: .venv\Scripts\activate
pip install -r requirements.txt
cp .env.example .env    # then edit: set DOCQA_MODEL and your key (or ollama:llama3.2)

.env is gitignored, so the key stays on your machine. The notebooks detect a local run and use this configuration automatically.

Expected cost

The labs use small models on short prompts. A typical week is a few hundred calls of a few hundred tokens each; a full semester of lab work totals well under one million tokens. At current prices (gpt-4o-mini: $0.15 per million input tokens, $0.60 per million output; claude-haiku-4-5: $1 / $5), the $5 minimum credit covers the entire semester with a wide margin. The one exception is Week 3’s self-consistency lab, which multiplies calls by N=5 — still cents, not dollars. Check the provider’s live pricing page before topping up more than the minimum; there is no reason to.

Key safety rules

  1. Keys live only in Colab Secrets (or, for local runs, in the gitignored .env). Never commit a key to git.
  2. Never paste a key into a notebook cell, a screenshot, or a chat message. Notebooks are submitted and shared; a key in a cell output is a leaked key.
  3. Set a spending limit at the provider (Option A step 3) so a leaked key cannot spend more than your cap.
  4. If a key leaks, revoke it immediately on the provider’s API-keys page and issue a new one. Revocation is instant and free.

Troubleshooting

Symptom Cause Fix
AuthenticationError / HTTP 401 Key missing, mistyped, or .env not loaded Re-copy the key; confirm you edited labs/.env, not .env.example; restart the notebook kernel
insufficient_quota / HTTP 429 (quota) No prepaid credit on the account Complete the billing step (A-2 or B-2)
RateLimitError / HTTP 429 (rate) Too many calls per minute (new accounts have low limits) Wait a minute and rerun; avoid rerunning all cells in a tight loop
Connection refused on Ollama Ollama app not running Start Ollama, confirm with ollama list
Key shown once and lost Provider shows keys only at creation Delete the old key, create a new one