Skip to main content
Let’s start with the lab files you’ll work through:
In this lesson you’ll learn how to make your first AI API calls with the OpenAI Python client. The goal is practical: verify your environment, connect to the API, make a chat completion request, extract the assistant’s reply, and inspect token usage and cost — all in progressive steps.
A tutorial screen titled "Mission: Your First AI API Calls" showing a "Welcome, Beginner!" message and a list of six progressive steps for making AI API calls. A dark sidebar on the right displays filenames for related Python tasks.

1 — Verify the environment

Before writing code, verify that your runtime is ready: activate the virtual environment, confirm Python is available, ensure the OpenAI package is installed, and verify your API keys are present. Run these commands in the lab VM:
If verification succeeds, the script prints readiness checks and exits. If something fails, re-check your virtual environment and that packages (like the OpenAI Python package) are installed. Environment variables commonly used in these examples:

What is OpenAI?

OpenAI builds ChatGPT and families of large language models (e.g., GPT-4, GPT-4.1 Mini, GPT-3.5). The OpenAI Python client is the bridge between your Python code and the API.
A dark-themed screenshot of a tutorial titled "What is OpenAI?" listing OpenAI models (GPT-4, GPT-4.1‑mini, GPT-3.5) and describing the OpenAI Python library. A file sidebar with example Python task filenames is visible on the right.

Task 1 — Import required libraries

Open task_1_import_setup.py. You need to import the OpenAI client library and the os module to read environment variables. The following file shows the required imports and writes a completion marker for the lab system.
Run it like this:

Authentication and client setup

To authenticate you need:
  • OPENAI_API_KEY — your secret API key
  • OPENAI_API_BASE — (optional) custom API base URL
Keep these values out of source control. Use environment variables, a secrets manager, or CI secrets.

Task 2 — Initialize the OpenAI client

Open task_2_client_initialization.py and initialize the OpenAI client using environment variables. This example creates a client object you can reuse across requests.
Example run:
If client initialization fails, confirm your environment variables are set and that the model you request is available for your account.

Chat completions — the basics

Chat completions implement conversational interactions. You send an ordered list of messages (with roles) and the model returns assistant messages. Minimal Python pattern:
Roles:
  • system — high-level instructions that define behavior
  • user — user input
  • assistant — model replies

Task 3 — Make an API call

Open task_3_api_call_explained.py. Configure the model and messages, then make a call where the AI introduces itself.
Example output:
If you receive PermissionDenied or “model not supported” errors, switch to a model available on your account or check API permissions.

Task 4 — Extract the AI’s response

Responses contain nested structures. The straightforward path to the assistant’s reply is:
Open task_4_extract_response.py to extract and print that text.
Example output:

Tokens and costs

Tokens are the billing and processing unit used by models. Every request consumes tokens from your account: Output tokens are often priced higher than input tokens, so being concise helps control costs.
Keep your API key secure. Never hard-code it in scripts or check it into version control. Use environment variables or a secrets manager.
A screenshot of a dark-themed code editor and documentation titled "Understanding Tokens & AI Economics," showing bullet points about token types, costs, and where to find usage. The right side shows a file list with Python scripts (e.g., task_4_extract_response.py).

Task 5 — Extract token usage and compute cost

Open task_5_tokens_and_costs.py. The response includes a usage object with three fields: prompt_tokens, completion_tokens, and total_tokens. Use these values to compute a simple cost estimate with your per-token pricing.
Sample output (varies per request and model):
Be careful with long model responses or high-frequency calls — costs can add up quickly. Use concise prompts, set max tokens when needed, and monitor usage.
A screenshot of a “Congratulations!” tutorial screen listing mastered topics (environment setup, chat completions, models/roles, extracting responses, token/costs) and a highlighted key takeaway path. A dark editor sidebar on the right shows Python task filenames for the lab.

Wrap-up

Congrats — by completing this lab you:
  • Verified your environment and runtime
  • Initialized the OpenAI Python client
  • Made chat completion requests
  • Extracted assistant replies via response.choices[0].message.content
  • Read token usage and estimated costs
Next steps: experiment with system messages to control behavior, try longer multi-turn conversations, and test different models to compare quality and cost. Relevant local files in this lab: You’re ready to build on this foundation and explore richer prompts, system instructions, and multi-turn dialogues. Good luck!

Watch Video

Practice Lab