API Beginner easy · 3 min

OpenAI() client initialization

What you will learn
Create an authenticated OpenAI API client object that handles all requests to OpenAI's models.

Why this matters

Every interaction with OpenAI's API (chat, embeddings, images) starts with initializing a client. Getting this right determines whether your code can authenticate, handle errors gracefully, and respect rate limits.

Skip if: Use the REST API directly with curl or requests library if you need minimal dependencies or are building a non-Python service. Use the official OpenAI CLI for one-off testing instead of writing Python scripts.

Explanation

The OpenAI() constructor creates a synchronous client object that manages authentication, request formatting, and API communication. It reads your API key from the environment variable OPENAI_API_KEY by default, but you can pass it explicitly as a parameter.

Under the hood, the client stores your credentials, sets up HTTP session pooling, configures timeout values, and establishes default headers for every request. When you call methods like client.chat.completions.create(), the client automatically signs the request, sends it to OpenAI's servers, and deserializes the JSON response into Python objects.

Initialize the client once in your application startup (not inside loops or functions called repeatedly) to reuse the same HTTP connection pool. This saves memory and network overhead.

Request code

python
import os
from openai import OpenAI

os.environ['OPENAI_API_KEY'] = 'sk-your-api-key-here'

client = OpenAI()

response = client.chat.completions.create(
    model='gpt-4.1',
    messages=[
        {'role': 'user', 'content': 'What is 2 + 2?'}
    ]
)

print(response.choices[0].message.content)

Authentication

Set your API key as an environment variable before running Python: export OPENAI_API_KEY='sk-...' on macOS/Linux or set OPENAI_API_KEY=sk-... on Windows. The OpenAI() constructor automatically reads this. Alternatively, pass it explicitly: OpenAI(api_key='sk-...'): though hardcoding is unsafe in production.

Response shape

FieldDescription
id chatcmpl-9abc123def456
object chat.completion
created 1704067200
model gpt-4.1-20250101
choices [object Object]
usage [object Object]

Field guide

id

Unique identifier for this completion: useful for logging and debugging specific API calls

created

Unix timestamp of when the response was generated: helps track API latency across regions

usage.total_tokens

Sum of prompt and completion tokens: critical for calculating API costs before processing responses

finish_reason

Why the model stopped: 'stop' for natural end, 'length' if max_tokens was hit, 'content_filter' if safety policy triggered: tells you if output was truncated

Setup trap

Setting os.environ['OPENAI_API_KEY'] AFTER importing OpenAI but BEFORE calling OpenAI() works fine: the constructor reads the environment at instantiation time. However, if you set the env var in a different process or thread after the client is already created, the client won't see the update. The client captures the key at construction, not on every request.

Cost

Client initialization itself costs nothing: you only pay per API call (not per client object). A single chat completion with gpt-4.1 costs roughly $0.00001 per prompt token and $0.00003 per completion token as of April 2026. Always check the usage.total_tokens field in responses to estimate spend.

Rate limits

Rate limits apply per API key, not per client object. Creating multiple client instances with the same key doesn't increase your quota. Standard API keys have rate limits (requests per minute vary by plan). The SDK will raise RateLimitError after hitting limits: implement exponential backoff retry logic in production.

Common gotcha

The most common mistake is initializing the client inside a request handler (like a Flask route or FastAPI endpoint) called thousands of times. Each initialization creates a new HTTP connection pool, wasting memory and connections. Initialize once at application startup and reuse it globally.

Error recovery

AuthenticationError
Your API key is missing, invalid, or revoked. Check: export OPENAI_API_KEY='sk-...' is set correctly, the key hasn't been deleted in OpenAI dashboard, and there are no leading/trailing spaces in the environment variable.
APIConnectionError
Network is unreachable or OpenAI servers are down. Implement retry logic with exponential backoff: wait 1s, then 2s, then 4s before retrying. Check your internet connection first.
RateLimitError
You've exceeded your API quota for the minute/day. Implement a queue with delayed retries. Upgrade your OpenAI plan if limits are consistently hit.

Experienced dev note

Store the client as a module-level singleton or in dependency injection (FastAPI Depends, Django settings). This prevents connection pool waste and makes mocking easier in tests. In FastAPI, instantiate once in a startup event and pass via context: don't recreate per request. For async code, use AsyncOpenAI instead, which uses asyncio internally.

Check your understanding

Why would initializing OpenAI() inside a Flask view function that handles 1000 requests per minute be problematic, and how would you verify this is actually happening in production?

Show answer hint

Each OpenAI() constructor creates a new HTTP connection pool. Doing this 1000 times per minute leaks connections and memory. Look for connection pool growth in monitoring (increasing socket count, memory creep) or set HTTPX_LOG_LEVEL=debug to see connection churn.

VERSION openai 1.x is the current stable version (April 2026). Do not use openai 0.x patterns like openai.ChatCompletion.create() or openai.api_key = 'key': these are deprecated. The 1.x SDK requires explicit client instantiation with OpenAI().

Community Notes

No notes yetBe the first to share a version-specific fix or tip.