Code beginner · 3 min read

How to create OpenAI client in python

Direct answer
Create an OpenAI client in Python by importing OpenAI from openai and initializing it with your API key from os.environ like client = OpenAI(api_key=os.environ["OPENAI_API_KEY"]).

Setup

Install
bash
pip install openai
Env vars
OPENAI_API_KEY
Imports
python
import os
from openai import OpenAI

Examples

inCreate client and send a simple chat message 'Hello'
outclient = OpenAI(api_key=os.environ["OPENAI_API_KEY"]) response = client.chat.completions.create(model="gpt-4o", messages=[{"role": "user", "content": "Hello"}]) print(response.choices[0].message.content)
inInitialize client and request a chat completion with model 'gpt-4o-mini'
outclient = OpenAI(api_key=os.environ["OPENAI_API_KEY"]) response = client.chat.completions.create(model="gpt-4o-mini", messages=[{"role": "user", "content": "What is AI?"}]) print(response.choices[0].message.content)
inCreate client and handle empty message list error
outclient = OpenAI(api_key=os.environ["OPENAI_API_KEY"]) try: response = client.chat.completions.create(model="gpt-4o", messages=[]) except Exception as e: print(f"Error: {e}")

Integration steps

  1. Install the OpenAI Python SDK with pip.
  2. Set your OpenAI API key in the environment variable OPENAI_API_KEY.
  3. Import OpenAI from the openai package and os.
  4. Initialize the client using OpenAI(api_key=os.environ["OPENAI_API_KEY"]).
  5. Use the client to call API methods such as chat.completions.create with the desired model and messages.
  6. Extract the response text from response.choices[0].message.content.

Full code

python
import os
from openai import OpenAI

# Initialize OpenAI client with API key from environment
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

# Create a chat completion request
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello, how do I create an OpenAI client in Python?"}]
)

# Print the assistant's reply
print("Response:", response.choices[0].message.content)
output
Response: To create an OpenAI client in Python, import OpenAI from the openai package and initialize it with your API key from the environment variable OPENAI_API_KEY.

API trace

Request
json
{"model": "gpt-4o", "messages": [{"role": "user", "content": "Hello, how do I create an OpenAI client in Python?"}]}
Response
json
{"choices": [{"message": {"content": "To create an OpenAI client in Python, import OpenAI from the openai package and initialize it with your API key from the environment variable OPENAI_API_KEY."}}], "usage": {"total_tokens": 30}}
Extractresponse.choices[0].message.content

Variants

Streaming chat completion

Use streaming to display partial results immediately for long or interactive responses.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

# Stream response for better UX on long outputs
response_stream = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Explain streaming in OpenAI Python SDK."}],
    stream=True
)

for chunk in response_stream:
    print(chunk.choices[0].delta.get('content', ''), end='')
print()
Async client usage

Use async when integrating OpenAI calls into asynchronous Python applications for concurrency.

python
import os
import asyncio
from openai import OpenAI

async def main():
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
    response = await client.chat.completions.acreate(
        model="gpt-4o",
        messages=[{"role": "user", "content": "Async client example."}]
    )
    print(response.choices[0].message.content)

asyncio.run(main())
Use smaller model for cost efficiency

Use smaller models like gpt-4o-mini to reduce cost and latency for simpler tasks.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "What is the weather today?"}]
)
print(response.choices[0].message.content)

Performance

Latency~800ms for gpt-4o non-streaming calls
Cost~$0.002 per 500 tokens for gpt-4o
Rate limitsTier 1: 500 requests per minute / 30,000 tokens per minute
  • Keep messages concise to reduce token usage.
  • Use smaller models like gpt-4o-mini for cheaper calls.
  • Cache frequent responses to avoid repeated calls.
ApproachLatencyCost/callBest for
Standard client~800ms~$0.002 per 500 tokensGeneral purpose chat completions
Streaming clientStarts immediately, total ~800msSame as standardLong responses with better UX
Async client~800ms~$0.002 per 500 tokensConcurrent calls in async apps
Smaller model (gpt-4o-mini)~400ms~$0.0005 per 500 tokensCost-sensitive or simple tasks

Quick tip

Always load your API key from environment variables to keep credentials secure and avoid accidental leaks.

Common mistake

Beginners often forget to use the latest SDK v1+ pattern and try deprecated methods like openai.ChatCompletion.create(), which no longer work.

Verified 2026-04 · gpt-4o, gpt-4o-mini
Verify ↗