Lab: M4: ship your first AI app
You'll need: your laptop with Python + a virtual environment (from M3), a terminal, and a few minutes to make an account. Time: ~60 min • Work in your breakout pair.
Heads up: you'll handle a real secret key today. Treat it like a password, keep it in
.env, never paste it into chat or your code. If it ever leaks, you delete it in the Console and make a new one; no lasting harm. Errors here are normal and safe.
This lab has two parts: - Part A: get a key, store it safely, and confirm it with one tiny call. - Part B: build and personalize your chatbot.
flowchart LR
You["Your Python<br/>(messages + your key)"] -->|request| API["Claude API<br/>(the model)"]
API -->|response| You
Env[".env<br/>(secret key, never committed)"] -.loaded by.-> You
Part A: connect to the model
Full per-step detail (with screenshots of where buttons live) is in the API-key guide. The steps below are the short path.
Step 1: Get the starter files into a project folder
Make a folder, put check_setup.py, chatbot_starter.py, and .env.example (from this
module's starters/ and solution/) into it, and activate your
virtual environment (you should see (.venv); see python-venv.md).
You should now see: (.venv) in your prompt, and ls (Windows dir) lists the files above.
Step 2: Create your account and a little credit
At https://console.anthropic.com, sign up, then in Billing add a small amount of credit and set a spend limit.
You should now see: a credit balance in the Console. (A few dollars covers the whole course.)
Step 3: Create your API key
In API keys → Create Key, name it ai-course, and copy the key (it starts with sk-ant-).
You should now see: your key shown once. Copy it now, you can't view it again later.
Step 4: Put the key in .env
Copy the example to a real .env and paste your key in:
cp .env.example .env # Windows PowerShell: Copy-Item .env.example .env
.env, paste your key after ANTHROPIC_API_KEY=, and save.
You should now see: a .env file containing ANTHROPIC_API_KEY=sk-ant-... (your real key).
Never commit this file: the course .gitignore already ignores it.
Step 5: Install the libraries
pip install anthropic python-dotenv
Successfully installed anthropic-… python-dotenv-….
Step 6: Confirm with one tiny call
python check_setup.py
It works! Claude says: followed by a short friendly sentence written by
the model. You just called an AI from your own code. (If you get an error, the
troubleshooting box names the fix, usually a key typo or a misnamed .env.)
Part B: build your chatbot
Step 7: Look at the shape, then run the starter
Open chatbot_starter.py. Read it top to bottom, you've met all of it: a system personality, a
messages list, and a loop that asks Claude each turn. Run it:
python chatbot_starter.py
You should now see: the bot replies, but on the second message it forgets your name.
That's the missing piece you'll fix next. Type quit to leave.
Step 8: Make the bot remember (finish TODO 2)
The bot forgets because we never store its replies. Under the print("Bot:", ...) line, add:
messages.append({"role": "assistant", "content": reply})
You should now see: now it remembers, "Your name is Sam." Sending the whole conversation each turn is how a stateless API "remembers". That's the heart of a chatbot.
Step 9: Give it a personality (finish TODO 1)
Change the SYSTEM line to anything you like, e.g.:
SYSTEM = "You are a cheerful pirate who answers in nautical slang but stays genuinely helpful."
You should now see: the bot's whole voice changes to match, same code, new personality. The system prompt is how you steer a model (you'll go deep on this in M5).
Step 10: Make it yours
Pick a real job for your bot (study buddy, recipe helper, one-city travel guide) and write a
SYSTEM prompt for it. Chat with it for a few turns.
You should now see: a bot that behaves like the job you described, remembering the conversation as you go. You built and shaped a real AI app.
Stuck? The finished, commented version is
../solution/chatbot.py. Peek only after you've tried.
Your win
You built an AI chatbot from scratch, keyed in safely, gave it a personality, made it remember the conversation, and you can read every line.
Post it to the chat wins board: one line of your bot's reply, plus its job, e.g. "My pirate code tutor: 'Arrr, that loop be shipshape! ', I built an AI app and I can read every line "
Take-home (optional)
Try the cheaper model: change model="claude-opus-4-8" to model="claude-haiku-4-5" in your
chatbot and chat again. Notice it's faster and far cheaper, a one-string change. Which felt better
for your bot's job? Bring a thought to next session (we dig into model/parameter choices in M6).