sagelearnai

Environments, Dependencies & API Keys

Most "it works on my machine" bugs come from three things: the wrong Python environment, unpinned package versions, and mishandled API keys. Get these right once and save yourself hours.

Virtual environments

A virtual environment is an isolated folder with its own Python interpreter and packages, so project A's openai==1.x doesn't fight project B's.

bash
python3 -m venv .venv
source .venv/bin/activate        # Windows: .venv\Scripts\activate
python -m pip install --upgrade pip
pip install openai anthropic python-dotenv pydantic numpy

Modern alternative: uv, a very fast drop-in for pip/venv workflows:

bash
uv venv && source .venv/bin/activate
uv pip install openai pydantic

Conda is common in data science, especially when you need non-Python binaries such as CUDA libraries.

Pinning dependencies

AI libraries change fast, and breaking API changes are common. Pin versions so builds are reproducible:

bash
pip freeze > requirements.txt          # exact versions of everything installed
pip install -r requirements.txt        # recreate elsewhere

Better: declare direct dependencies in pyproject.toml and use a lock file (uv, Poetry, pip-tools). Rule of thumb: pin exact versions for apps, use ranges for libraries.

Secrets: API keys

An API key is a password that spends money. Leaked keys are scraped from public GitHub repos within minutes.

.env (local only)OPENAI_API_KEY=sk-...listed in .gitignore Environment varsos.environ["…"]read at startup Prod secret storeVercel/AWS/GCP secrets,Vault, Doppler
bash
# .env  (NEVER commit this file)
OPENAI_API_KEY=sk-...
ANTHROPIC_API_KEY=sk-ant-...
bash
# .gitignore
.env
.venv/
__pycache__/
python
import os
from dotenv import load_dotenv
load_dotenv()                                   # reads .env into environment variables
api_key = os.environ["OPENAI_API_KEY"]          # fails loudly if missing (good)

Official SDKs read standard variables (OPENAI_API_KEY, ANTHROPIC_API_KEY) automatically, so you rarely pass the key in code.

If you leak a key

Revoke it immediately in the provider dashboard and create a new one. Deleting the commit isn't enough: git history and forks keep it. Also set spending limits and usage alerts on every provider account.

Secrets in other places

  • Browsers and mobile apps: never embed keys. Call your own backend, which holds the key.
  • CI: use the CI's encrypted secrets feature, never plain YAML.
  • Logs: redact Authorization headers and keys; don't log full request objects blindly.
  • Notebooks: don't paste keys into cells you'll share. Use getpass or environment variables.

A clean project layout

text
my-ai-app/
├── .env               # secrets (git-ignored)
├── .env.example       # variable names with fake values (committed)
├── .gitignore
├── pyproject.toml     # dependencies
├── src/app/
│   ├── config.py      # reads env vars, one place
│   ├── llm.py         # model calls, retries
│   └── prompts/       # prompt templates as files, versioned
├── evals/             # golden datasets and eval scripts
└── tests/
python
# src/app/config.py: centralize configuration
from pydantic_settings import BaseSettings, SettingsConfigDict   # pip install pydantic-settings

class Settings(BaseSettings):
    model_config = SettingsConfigDict(env_file=".env")
    openai_api_key: str
    model: str = "gpt-4o-mini"
    request_timeout_s: float = 30.0

settings = Settings()

Last reviewed 2026-11-05.