OpenAI API — GPT-4o for Production Applications
The OpenAI API is the fastest path to production AI applications. Learn the full API surface: chat completions, streaming, function calling (tool use), vision, embeddings, and cost management. These patterns are nearly identical across Anthropic Claude, Google Gemini, and compatible providers.
OpenAI API — Complete Patterns
from openai import OpenAI
import json
client = OpenAI() # reads OPENAI_API_KEY from env
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# 1. BASIC CHAT COMPLETION
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
response = client.chat.completions.create(
model="gpt-4o", # gpt-4o, gpt-4o-mini, o1, o1-mini
messages=[
{"role": "system", "content": "You are a Python expert."},
{"role": "user", "content": "What is a generator in Python?"},
],
temperature=0.7, # 0=deterministic, 1=creative, 2=chaotic
max_tokens=500,
top_p=0.95,
frequency_penalty=0.0, # reduce repetition
presence_penalty=0.0, # encourage topic diversity
)
print(response.choices[0].message.content)
input_tokens = response.usage.prompt_tokens
output_tokens = response.usage.completion_tokens
cost = input_tokens * 5e-6 + output_tokens * 15e-6 # gpt-4o pricing ($ per token)
print(f"Cost: ${cost:.5f}")
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# 2. STREAMING — real-time token output (like ChatGPT)
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
stream = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Write a haiku about AI."}],
stream=True, # tokens arrive incrementally
)
for chunk in stream:
delta = chunk.choices[0].delta.content
if delta:
print(delta, end="", flush=True)
print() # newline
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# 3. FUNCTION CALLING (Tool Use) — structured actions
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# Model decides WHEN to call a function and with what arguments
# Your code executes the function — model never runs actual code
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current weather for a city",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "City name"},
"units": {"type": "string", "enum": ["celsius", "fahrenheit"], "default": "celsius"},
},
"required": ["city"],
},
}
}
]
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "What's the weather like in Tokyo right now?"}],
tools=tools,
tool_choice="auto", # auto: model decides | "required": must call | "none": no tools
)
if response.choices[0].message.tool_calls:
tool_call = response.choices[0].message.tool_calls[0]
fn_name = tool_call.function.name # "get_weather"
fn_args = json.loads(tool_call.function.arguments) # {"city": "Tokyo"}
print(f"Tool call: {fn_name}({fn_args})")
# Execute function (your code)
weather_data = {"temp": 22, "condition": "sunny", "humidity": 55}
# Return result to model
follow_up = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "user", "content": "What's the weather in Tokyo?"},
response.choices[0].message, # assistant's tool call message
{"role": "tool", "tool_call_id": tool_call.id, "content": json.dumps(weather_data)},
],
)
print(follow_up.choices[0].message.content)
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# 4. VISION — analyze images with GPT-4o
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
vision_response = client.chat.completions.create(
model="gpt-4o",
messages=[{
"role": "user",
"content": [
{"type": "text", "text": "Describe this image and identify any text visible."},
{"type": "image_url", "image_url": {"url": "https://upload.wikimedia.org/wikipedia/commons/a/a7/Camponotus_flavomarginatus_ant.jpg"}},
]
}],
max_tokens=200,
)
print(vision_response.choices[0].message.content)Tip
Tip
Practice OpenAI API GPT4o for Production Applications in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Technical diagram.
Practice Task
Note
Practice Task — (1) Write a working example of OpenAI API GPT4o for Production Applications from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with OpenAI API GPT4o for Production Applications is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready ai code.