What You Will Learn
BEGINNER- Install Python, pip, and the OpenAI SDK in 5 minutes
- Secure API key management (so you never leak a key)
- Your first working "hello LLM" script — and what's actually happening
Step 1: Install Python & pip
If you don't have Python yet, install Python 3.11 or newer from python.org. On macOS, use brew install python. On Linux, your distro package manager (apt install python3). Verify it works:
Step 2: Get an OpenAI API Key
Go to platform.openai.com/api-keys, sign in, click "Create new secret key", copy it. Treat this key like a credit card — anyone with it can spend your money. The key looks like sk-proj-abc123... (about 100 characters).
Step 3: Secure API Key Management
The right way: store your key in an environment variable. Create a file called .env in your project folder:
# .env file (NEVER commit this to git!)
OPENAI_API_KEY=sk-proj-your-actual-key-habcdefg1234567890
# Add .env to your .gitignore:
echo ".env" >> .gitignore
Then in Python, use the python-dotenv library to load it:
# Install: pip install openai python-dotenv
from dotenv import load_dotenv
import os
load_dotenv() # loads variables from .env file
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
raise SystemExit("Set OPENAI_API_KEY in your .env file!")
Step 4: Your First LLM Call
Now the magic moment. With your key loaded, here's a complete working script:
from dotenv import load_dotenv
from openai import OpenAI
import os
load_dotenv()
client = OpenAI() # auto-reads OPENAI_API_KEY from env
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "You are a helpful teacher."},
{"role": "user", "content": "Explain what an API is, in one sentence."},
],
)
print(response.choices[0].message.content)What's Actually in the Response?
The response is a Python object with several useful fields. Let's print them all:
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Say hi"}],
)
# The main text
print("Answer:", response.choices[0].message.content)
# The reason the model stopped (usually "stop")
print("Finish reason:", response.choices[0].finish_reason)
# Token usage (for cost tracking)
print("Tokens used:", response.usage)
# Which model was actually used
print("Model:", response.model)response.usage.total_tokens per request. Multiply by the model's per-token price to compute cost. Tools like Helicone and LangSmith (Lesson 71) do this automatically — but for a quick script, logging usage yourself takes 2 lines of code and saves surprises on your bill.Step 5: Set Up a Project Folder
Best practice for any LLM project: one folder per project, with a clean structure:
my-llm-app/
├── .env # API keys (gitignored)
├── .gitignore # tells git to ignore .env
├── requirements.txt # pip dependencies
├── main.py # entry point
├── prompts/ # your prompt templates
│ └── system.txt
└── tests/ # unit tests
└── test_api.py
Create requirements.txt with your pinned dependencies:
# requirements.txt
openai>=1.50.0
python-dotenv>=1.0.1
tiktoken>=0.7.0
pydantic>=2.0
Then anyone can reproduce your environment with pip install -r requirements.txt.
python --version.env with your key, add .env to .gitignorepip install openai python-dotenvresponse.usage to see how many tokens your call used (multiply by $0.15 / 1M to see cost)🎯 Key Takeaways
- Install Python 3.11+, the openai SDK, and python-dotenv — that's all you need to start.
- NEVER hardcode API keys. Always load from
.envviaos.getenv(). - Your first call is just:
client.chat.completions.create(model, messages). - Always log
response.usage.total_tokensto track costs. - Use a clean project structure: .env, requirements.txt, main.py, prompts/.
🧠 Quick Check
response.usage.total_tokens tell you?client.chat.completions.create(model='gpt-4o-mini', messages=[{'role':'user','content':'hi'}]) — model + messages is enough.Today: Today: you set up Python, secured your API key, made your first LLM call, and inspected the response.
Next: Next: Lesson 10 deep-dives every parameter of the Chat Completions API — temperature, top_p, max_tokens, response_format, and more.
Comments
Comments
Post a Comment