Every later lesson assumes you can open a terminal, run a Python script, and call a model. This lesson sets that up once, cleanly, so you never fight your tools again. We'll move in small, verifiable steps — if a step works, you'll know immediately.
You'll need an API key from a model provider, which usually requires adding a payment method. The examples cost fractions of a cent — running everything in this course is typically under a dollar. We'll cover cost control properly in Section 6, and we'll show a fully free local-model option in Section 11.
The shape of your workspace
Before the commands, picture what you're building: an isolated project folder with its own Python environment, a secrets file the model reads from, a notebook or script to run, and a Git repo to save it all.
.env and calls the provider's API. The key and the environment never get committed.Step by step
-
Install Python 3.11+
Download it from python.org (or your package manager). Confirm it works:
python --version # Python 3.11.x (or newer) -
Create a project and a virtual environment
A virtual environment keeps this course's packages isolated from the rest of your system.
When it's active, your prompt showsmkdir sage && cd sage python -m venv .venv # activate it: # macOS / Linux: source .venv/bin/activate # Windows (PowerShell): .venv\Scripts\Activate.ps1(.venv). -
Install the packages
We start with the OpenAI SDK and a helper to load secrets. (We'll add more per section.)
pip install openai python-dotenv -
Get an API key and store it safely
Create a key in your provider's dashboard, then put it in a file named
.env— never in your code:# .env (this file stays on your machine only) OPENAI_API_KEY=sk-your-key-here -
Make your first model call
Create
hello.py:
Run it:import os from dotenv import load_dotenv from openai import OpenAI load_dotenv() # read .env client = OpenAI() # uses OPENAI_API_KEY automatically resp = client.chat.completions.create( model="gpt-4o", messages=[{"role": "user", "content": "In one sentence, say hello as Sage, a friendly study assistant."}], ) print(resp.choices[0].message.content)
You should see a friendly one-liner. That's Sage's first breath. 🎉python hello.py -
Initialise Git — and protect your secrets
Thegit init printf ".venv/\n.env\n__pycache__/\n" > .gitignore git add . git commit -m "Section 1: hello, Sage".gitignoreguarantees your key and environment never get committed.
Why the key goes in .env, not your code
It's tempting to paste your key straight into the script. Don't. Keys in source code get pushed to GitHub, copied into screenshots, and shared in chats — and a leaked key can run up real charges on your account. Keeping it in a git-ignored .env file means the secret lives only on your machine, while your code (which is safe to share) just reads it by name.
- Hard-coding the key as a string in
hello.py(then committing it). - Forgetting to add
.envto.gitignorebefore the first commit. - Pasting a real key into a question on a forum or into a screenshot.
Scripts or notebooks?
Both work, and you'll use both. VS Code with the Python extension is great for building real files like hello.py. Jupyter notebooks are perfect for experimenting cell by cell — handy when you're poking at model output. Install whichever you prefer now; the course's code runs identically in either.
Prefer the faster, modern installer uv if you know it: uv venv and uv pip install openai python-dotenv do the same thing in a fraction of the time. Plain pip is perfectly fine for this course — use what you're comfortable with.
Get to a green checkmark before moving on:
- Run
hello.pyand confirm you get a sentence back. - Change the prompt to ask Sage for three study tips, and run it again.
- Run
git statusand confirm that.envdoes not appear in the list of tracked files.
What success looks like
Step 1 prints a friendly line. Step 2 prints three tips — proof you're really steering a live model. Step 3 shows .env nowhere in git status (it's ignored). If .env does show up, your .gitignore isn't taking effect — fix it before you ever push.
- Isolate the project with a virtual environment (
python -m venv .venv). - Install the SDK:
pip install openai python-dotenv. - Protect your key in a git-ignored
.env— never in code. - Verify with a "hello model" call, then commit with a safe
.gitignore.