AI

OpenAI API Basics

Chat Completions, responses API, tools, embeddings, and billing gotchas.

Interview tip Know message roles (system/user/assistant), token limits, structured outputs, and function calling flow.

① What you must know (30 sec)

The OpenAI API provides programmatic access to GPT models via Chat Completions (and newer Responses API), plus Embeddings, Images, Audio, and Batch endpoints. Core concepts: message roles (system, user, assistant, tool), token limits, function/tool calling, and structured outputs for reliable JSON.
Analogy: The API is a vending machine for intelligence — you insert tokens (paid), select the model SKU, and get formatted output if you specify the slot correctly.

② How it works

Function calling flow
User messageModeltool_calls JSONYour functionTool result → final reply
Authentication — Bearer API key from platform.openai.com
Chat Completions — POST /v1/chat/completions with model + messages
Streaming — stream: true yields SSE token deltas for UX
Tools — pass tools[] schema; model may return tool_calls to execute
Embeddings — separate endpoint for vector generation; Batch API for async 50% off
gpt-4o and gpt-4o-mini are default choices in 2025 — verify current model IDs in OpenAI docs before hardcoding.

③ Step-by-step (hands-on)

Step 1 — Get API key and set env

export OPENAI_API_KEY=sk-... — use secrets manager in production. Set usage limits and alerts on dashboard.

Step 2 — First chat completion

messages=[{role:"system",...},{role:"user",...}]. Parse choices[0].message.content.

Step 3 — Add streaming

stream=True; iterate chunks for choices[0].delta.content. Handle [DONE] and connection drops.

Step 4 — Implement tool calling

Define tools array; if message.tool_calls, run functions, append role:"tool" messages, call API again.

Step 5 — Structured outputs

Use response_format json_schema for guaranteed schema compliance on supported models.

Step 6 — Monitor usage

Log response.usage.prompt_tokens and completion_tokens. Aggregate daily cost per feature.

④ Code / config patterns

EndpointUse caseCost note
Chat CompletionsInteractive chat, agentsPer input + output token
EmbeddingsRAG indexingCheaper per token than chat
Batch APIOffline eval, bulk jobs~50% discount, 24h window
Images / AudioMultimodal productsSeparate pricing table
Fine-tuningCustom model weightsTraining + inference premium
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": "You are helpful."},
        {"role": "user", "content": user_input}
    ],
    tools=TOOLS,  # optional
    response_format={"type": "json_schema", "json_schema": SCHEMA},
    max_tokens=1024,
    temperature=0.2
)
text = response.choices[0].message.content
usage = response.usage

⑤ Production & pitfalls

PitfallWhy it hurtsFix
Exposed API keyAccount drained by botsBackend only; rotate key if leaked
No max_tokens capRunaway completion costSet max_tokens per request type
Ignoring 429 errorsCascading failuresExponential backoff; request queue
Wrong message role orderConfused multi-turn contextAlternate user/assistant; tool after assistant tool_calls
Parsing JSON from free textFragile pipelinesUse structured outputs / json_schema mode
Not counting tokens pre-callContext overflow errorstiktoken estimate; trim or summarize history
Production tips:
  • Organization-level API keys per environment
  • Idempotency keys for payment-critical flows if supported
  • Fallback model (4o-mini) when flagship model overloaded
  • Redact PII before sending to API

⑥ Interview / on-the-job Q&A

QuestionAnswer
Message roles?system (instructions), user, assistant (model), tool (function results).
What is max_tokens?Cap on tokens the model generates in the response — controls cost and length.
Function calling flow?Model returns tool_calls → you execute → send tool message → model final answer.
Batch API benefit?~50% cheaper for non-urgent jobs completed within 24 hours.
Structured outputs?JSON schema enforcement so output always matches your schema.
How estimate cost?prompt_tokens + completion_tokens × price per 1M tokens for model tier.

⑦ Tools & ecosystem

  • SDK: openai Python/Node official library
  • Token counting: tiktoken
  • Proxies: LiteLLM, Portkey
  • Dashboard: platform.openai.com usage & limits
openaiapifunction-callingtokensstructured-output

⑧ Revision checklist

  • API key in env var, not source code
  • Usage limits and billing alerts set
  • max_tokens configured per endpoint
  • Streaming implemented for user-facing chat
  • 429/5xx retry with backoff
  • Token usage logged per request
  • Structured outputs for JSON pipelines
  • Tool calling loop tested end-to-end
  • Model IDs verified against current docs