Using a Language Model in Code › Your First Model Call

Michael BrenndoerferJune 27, 20252 min read
AI Agent HandbookAll chapters
Part 2 of 15Language Models - The Brain of the Agent
Chapter 5Using a Language Model in Code › Your First Model Call
Reading section 3 of 15 · Your First Model Call · 2 minSection 3 of 15 · 2 min

Book contents

Section 3 of 15 · 2 min read

Using a Language Model in Code › Your First Model CallOne section · Sections in this chapter3 / 15 ↓

We'll start with the simplest possible example: asking a language model a question and printing the answer.

Example (GPT-5)

In[3]:
Code
import os
from openai import OpenAI

## Initialize the client
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

## Ask a question
## Using GPT-5 for its standardized, reliable responses
response = client.chat.completions.create(
    model="gpt-5", messages=[{"role": "user", "content": "What is 2 + 2?"}]
)

## Print the answer
print(response.choices[0].message.content)
Out[3]:
OpenAIError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable
---------------------------------------------------------------------------
OpenAIError                               Traceback (most recent call last)
Cell In[3], line 5
      2 from openai import OpenAI
      4 ## Initialize the client
----> 5 client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
      7 ## Ask a question
      8 ## Using GPT-5 for its standardized, reliable responses
      9 response = client.chat.completions.create(
     10     model="gpt-5", messages=[{"role": "user", "content": "What is 2 + 2?"}]
     11 )

File /private/tmp/mb-ai-style-scanner/.venv/lib/python3.11/site-packages/openai/_client.py:139, in OpenAI.__init__(self, api_key, organization, project, webhook_secret, base_url, websocket_base_url, timeout, max_retries, default_headers, default_query, http_client, _strict_response_validation)
    137     api_key = os.environ.get("OPENAI_API_KEY")
    138 if api_key is None:
--> 139     raise OpenAIError(
    140         "The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable"
    141     )
    142 if callable(api_key):
    143     self.api_key = ""

OpenAIError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable

Let's unpack what's happening here:

API Key: The OPENAI_API_KEY is your authentication credential. You get this from OpenAI's website after creating an account. Store it as an environment variable (never hard-code it in your source code, as that's a security risk).

Client initialization: The OpenAI() client is your connection to the model. You create it once and reuse it for multiple requests.

Model selection: "gpt-5" specifies which language model to use. Different models have different capabilities, speeds, and costs. GPT-5 is OpenAI's latest model as of 2025 and is designed for improved reliability and standardized responses.

Messages format: The messages parameter is a list of conversation turns. Each message has a role (who's speaking) and content (what they said). Right now we're only sending one user message, but we'll expand this shortly.

Response structure: The model returns a complex object, but what we care about is response.choices[0].message.content, which contains the actual text the model generated.