LLM Application Development
Beginner
4.5

Call an LLM API in Python

Make your first programmatic call to a language model and handle the response.

0h 20m
1 lesson
1.2K students

What You'll Learn

Learning objectives will be added soon.

Tutorial Content

From chat box to code

You've used an AI assistant in a browser; calling it from Python is the same idea, just programmatic — and it's the foundation of every AI app you'll build. Once you can send a message and read the reply in code, you can wrap a model in a script, a web app, or an automation. Here's the whole thing, start to finish.

Install and authenticate

Install the client library and set your API key as an environment variable (never paste it into your code):

pip install openai
export OPENAI_API_KEY="sk-..."

The library automatically reads the key from the environment, so you don't pass it explicitly.

Your first call

from openai import OpenAI
client = OpenAI()

resp = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": "You are concise."},
        {"role": "user", "content": "Explain embeddings in one sentence."},
    ],
)
print(resp.choices[0].message.content)

That's a complete program: create a client, send a list of messages, read the reply out of the response object.

Understanding the pieces

  • Messages are a list of role/content dicts. The system message sets behavior and persona; user messages carry the actual request; assistant messages (when you add them) represent the model's past replies.
  • Model picks the engine — smaller models like gpt-4o-mini are cheap and fast; larger ones are smarter and pricier.
  • The reply lives at resp.choices[0].message.content.

Do it safely

A few habits separate a toy script from real code:

  • Never hard-code keys. Read them from environment variables so they never end up in your git history.
  • Handle failures. Wrap calls in try/except; network errors and rate limits happen.
  • Back off on rate limits. If you hit a 429, wait a moment and retry with increasing delays rather than hammering the API.

The takeaway

Every AI application — chatbots, RAG systems, agents — is built on this one move: send messages, get a reply. Master this call and the rest is just arranging it cleverly.

Try it now: Run the snippet above, then change the system message to "You are a pirate" and rerun. Seeing the same code produce a totally different voice makes the role system click.

Your Progress

Sign in to track your progress

Tags

Python
API
LLM