Chapter 1 · Lesson 3

Setting Up Your Environment

Nothing kills momentum like a broken setup. Twenty minutes now buys you a frictionless course — and by the end of this lesson, Sage will say its first words.

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.

⚠️ Heads Up

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.

A local development environment layout A project folder contains a virtual environment, a dot-env secrets file, a Python script, and a Git repository; the script reads the key from dot-env and calls the model provider's API. 📁 sage/ (your project) .venv .env(your secret key) hello.py .git .env and .venv are git-ignored Model provider API OpenAI · Claude · Gemini
Your script reads a secret key from .env and calls the provider's API. The key and the environment never get committed.

Step by step

  1. Install Python 3.11+ Download it from python.org (or your package manager). Confirm it works:
    python --version
    # Python 3.11.x  (or newer)
  2. Create a project and a virtual environment A virtual environment keeps this course's packages isolated from the rest of your system.
    mkdir sage && cd sage
    python -m venv .venv
    # activate it:
    # macOS / Linux:
    source .venv/bin/activate
    # Windows (PowerShell):
    .venv\Scripts\Activate.ps1
    When it's active, your prompt shows (.venv).
  3. 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
  4. 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
  5. Make your first model call Create hello.py:
    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)
    Run it:
    python hello.py
    You should see a friendly one-liner. That's Sage's first breath. 🎉
  6. Initialise Git — and protect your secrets
    git init
    printf ".venv/\n.env\n__pycache__/\n" > .gitignore
    git add .
    git commit -m "Section 1: hello, Sage"
    The .gitignore guarantees 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.

🚫 Common Mistakes
  • Hard-coding the key as a string in hello.py (then committing it).
  • Forgetting to add .env to .gitignore before 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.

💡 Expert Tip

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.

🛠️ Hands-on Exercise

Get to a green checkmark before moving on:

  1. Run hello.py and confirm you get a sentence back.
  2. Change the prompt to ask Sage for three study tips, and run it again.
  3. Run git status and confirm that .env does 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.

Lesson Summary
  • 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.