OpenAI offers multiple APIs for language models, embeddings, audio, and images. This guide covers what most developers need to build a chat feature, smart search, or structured data extraction, with attention to what matters in production.
1. Setup
pip install openai
export OPENAI_API_KEY="sk-..."
export OPENAI_MODEL="<model-id-from-docs>"2. Your first request with the Responses API
The Responses API is the newest interface and the one recommended for new projects. It combines text and tools in a single API.
import os
from openai import OpenAI
client = OpenAI() # reads OPENAI_API_KEY
response = client.responses.create(
model=os.environ["OPENAI_MODEL"],
instructions="You are a support assistant for Mit AI. Answer in English and keep it brief.",
input="What services do you offer?",
)
print(response.output_text)3. Chat Completions: the classic interface
It is still widely used, and it is the format adopted by most OpenAI-compatible servers (Ollama, vLLM, and others), so it remains useful if you want to switch providers by changing only base_url.
completion = client.chat.completions.create(
model=os.environ["OPENAI_MODEL"],
messages=[
{"role": "system", "content": "You are the Mit assistant."},
{"role": "user", "content": "Hello"},
],
)
print(completion.choices[0].message.content)4. Streaming
stream = client.chat.completions.create(
model=os.environ["OPENAI_MODEL"],
messages=[{"role": "user", "content": "Explain RAG simply"}],
stream=True,
)
for chunk in stream:
delta = chunk.choices[0].delta.content
if delta:
print(delta, end="", flush=True)5. Structured outputs (JSON) without guesswork
When you need data your code will read (extracting a name, a number, and a date from a message), do not rely on "ask the model to return JSON". Use schema-bound structured outputs and validate the result with a library such as Pydantic.
from pydantic import BaseModel
class Lead(BaseModel):
name: str
email: str | None
interest: str
completion = client.chat.completions.parse(
model=os.environ["OPENAI_MODEL"],
messages=[
{"role": "system", "content": "Extract the prospect's details."},
{"role": "user", "content": "I'm Sara, interested in a chatbot for my store. sara@example.com"},
],
response_format=Lead,
)
lead = completion.choices[0].message.parsed6. Function calling
As with Claude, you describe your functions with a JSON schema, the model decides when to request a call, and you execute it and return the result. The principle is the same across providers, which makes it easier to build a unified gateway.
tools = [{
"type": "function",
"function": {
"name": "get_order_status",
"description": "Get an order's status by its number",
"parameters": {
"type": "object",
"properties": {"order_id": {"type": "string"}},
"required": ["order_id"],
},
},
}]
completion = client.chat.completions.create(
model=os.environ["OPENAI_MODEL"], messages=messages, tools=tools
)
calls = completion.choices[0].message.tool_calls or []7. Embeddings for semantic search and RAG
emb = client.embeddings.create(
model="text-embedding-3-small",
input=["How do I book a consultation?", "Refund policy"],
)
vectors = [item.embedding for item in emb.data]
# Store them in pgvector, Qdrant, or Pinecone, then search by nearest similarityThe architectural details of this pattern are in How to improve your website search with AI and on the Enterprise RAG page.
8. Cost and reliability
- Token-based pricing: log
usagefor every request and set a monthly cap in the dashboard. - Rate limits (429): retry with exponential backoff plus jitter. The official SDK automatically retries certain errors.
- Timeouts: set a reasonable
timeout, and consider streaming for long responses. - Privacy: do not send unnecessary personal data, and review the data retention policies on your account.
- Moderation: if your app is open to the public, use content moderation tools and input filtering.
FAQ
What is the difference between the Responses API and Chat Completions?
The Responses API is the newer interface that unifies text, tools, and state, while Chat Completions is the classic interface that is widely supported by OpenAI-compatible servers.
How do I protect my OpenAI API key?
Store it in environment variables on the server only, never put it in frontend code, set a spending cap, and rotate it regularly.
Want an AI assistant in your business without this complexity?
We handle integration, setup, and security end to end, so your assistant starts serving your customers within weeks. Book a free consultation and we will recommend the best fit.