Using a Language Model in Code › Your First Model Call
AI Agent HandbookAll chapters 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)
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)[31m---------------------------------------------------------------------------[39m
[31mOpenAIError[39m Traceback (most recent call last)
[36mCell[39m[36m [39m[32mIn[3][39m[32m, line 5[39m
[32m 2[39m [38;5;28;01mfrom[39;00m[38;5;250m [39m[34;01mopenai[39;00m[38;5;250m [39m[38;5;28;01mimport[39;00m OpenAI
[32m 4[39m [38;5;66;03m## Initialize the client[39;00m
[32m----> [39m[32m5[39m client = [43mOpenAI[49m[43m([49m[43mapi_key[49m[43m=[49m[43mos[49m[43m.[49m[43mgetenv[49m[43m([49m[33;43m"[39;49m[33;43mOPENAI_API_KEY[39;49m[33;43m"[39;49m[43m)[49m[43m)[49m
[32m 7[39m [38;5;66;03m## Ask a question[39;00m
[32m 8[39m [38;5;66;03m## Using GPT-5 for its standardized, reliable responses[39;00m
[32m 9[39m response = client.chat.completions.create(
[32m 10[39m model=[33m"[39m[33mgpt-5[39m[33m"[39m, messages=[{[33m"[39m[33mrole[39m[33m"[39m: [33m"[39m[33muser[39m[33m"[39m, [33m"[39m[33mcontent[39m[33m"[39m: [33m"[39m[33mWhat is 2 + 2?[39m[33m"[39m}]
[32m 11[39m )
[36mFile [39m[32m/private/tmp/mb-ai-style-scanner/.venv/lib/python3.11/site-packages/openai/_client.py:139[39m, in [36mOpenAI.__init__[39m[34m(self, api_key, organization, project, webhook_secret, base_url, websocket_base_url, timeout, max_retries, default_headers, default_query, http_client, _strict_response_validation)[39m
[32m 137[39m api_key = os.environ.get([33m"[39m[33mOPENAI_API_KEY[39m[33m"[39m)
[32m 138[39m [38;5;28;01mif[39;00m api_key [38;5;129;01mis[39;00m [38;5;28;01mNone[39;00m:
[32m--> [39m[32m139[39m [38;5;28;01mraise[39;00m OpenAIError(
[32m 140[39m [33m"[39m[33mThe api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable[39m[33m"[39m
[32m 141[39m )
[32m 142[39m [38;5;28;01mif[39;00m [38;5;28mcallable[39m(api_key):
[32m 143[39m [38;5;28mself[39m.api_key = [33m"[39m[33m"[39m
[31mOpenAIError[39m: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variableLet'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.