Skip to main content
You’ve reviewed the code — now run it yourself against a live model. This short hands-on lesson walks through executing a minimal Python program that calls an LLM via the OpenAI SDK using a real API key from your terminal. No simulations: your code, your credentials, and a real model responding.
A retro-style black banner with neon text that reads "YOUR FIRST API CALL" and the subtitle "Run it yourself — for real." A small green button below says "FOR REAL" and there's a "HANDS-ON LAB" label above.
Environment and prerequisites
  • Python 3.11 is already installed and your project’s virtual environment is active.
  • Install the official SDK:
It’s safer to keep your API key out of source files. Use an environment variable such as OPENAI_API_KEY and load it from os.environ instead of pasting the key in code.
Create and run the example
  1. Create a file named hello_llm.py.
  2. Paste the short program below, save the file, and run it with python3 hello_llm.py.
This example demonstrates the minimal flow:
  • Create an SDK client
  • Send a chat completion request
  • Print the returned message
Explanation of key steps
  • client creation: OpenAI(api_key="...") — creates an authenticated client instance.
  • model: selects which model to use (here "gpt-4o").
  • temperature: controls randomness (0.0 = deterministic, higher = more diverse).
  • messages: the chat-style conversation (system -> assistant behavior, user -> request).
  • response.choices[0].message.content: the assistant’s returned text.
Run it from your terminal:
Parameters reference Change the prompt and experiment with temperature
  1. Modify the user message to:
    • "Write a one-sentence story about a robot."
  2. Set temperature to 0.0 and run the script twice. With 0.0 the model is deterministic (the same prompt returns the same output):
  1. Set temperature to 1.0 and run multiple times to see more diverse outputs:
Summary: temperature controls randomness — 0.0 = deterministic (repeatable), higher values = more varied creativity. Security reminder
Do not commit API keys to source control. Use environment variables (for example, OPENAI_API_KEY) or a secrets manager and never paste keys into public repositories.
Use an environment variable (recommended) If you prefer to load the API key from the environment instead of hardcoding it, update your client creation like this:
Set the environment variable in your shell before running:
Further reading and references You’ve now written and executed your first LLM program locally. Concepts demonstrated here — tokens, prompts, temperature, and context windows — are the foundations you’ll build on for more advanced use cases.

Watch Video