OpenAI API
Integrate GPT-4o, Whisper, DALL-E, and embeddings via the OpenAI API - covering chat completions, function calling, streaming, vision, and cost management.
skill: OpenAI API category: AI/ML tags: [OpenAI, GPT-4, API, Function Calling, Streaming] readingTime: 6
The OpenAI API is the default starting point for AI applications - broad model selection, best tooling, and widest community. For privacy/on-premise requirements, consider running open-source models. For lower cost, consider Anthropic Claude or Google Gemini Flash. For embeddings specifically, text-embedding-3-small is among the best price/performance ratios.
Workflow
- Set up the OpenAI clientThe official SDK handles auth, retries, and streaming - use it instead of raw HTTP calls.
- Use function calling for structured outputFunction calling forces the model to output valid JSON matching a schema - reliable way to extract structured data from unstructured text.
- Stream responses for real-time UXWithout streaming, users wait 5-10 seconds staring at a blank screen. Streaming shows the first token in ~200ms.
- Analyze images with GPT-4o VisionGPT-4o accepts image URLs or base64 images - useful for document parsing, screenshot analysis, and visual QA.
- Count tokens and manage costGPT-4o costs ~$5/M input tokens and $15/M output. Without tracking, a single bad prompt loop can cost hundreds of dollars.
Output Format
OpenAI API integration with structured output, streaming, vision, and token tracking.
client = OpenAI()
response = client.chat.completions.create(
model='gpt-4o-mini', # cheaper for prototyping
messages=[system_msg, user_msg],
max_tokens=1000,
temperature=0.7,
)
result = response.choices[0].message.contentExamples
Input
Output
Edge Cases & Gotchas
- Temperature 0 isn't deterministic - Temperature 0 is near-deterministic but not guaranteed due to floating-point non-determinism on GPUs. Don't rely on 100% reproducibility.
- System prompt leakage - Users can extract system prompts with prompt injection. Don't put sensitive business logic or secrets in system prompts - they're not secure.
- Context window vs. cost - Using GPT-4o with 100K token contexts costs 100x more than 1K contexts. Summarize long histories or use retrieval instead of expanding context.
- JSON mode vs. function calling - response_format: {type: 'json_object'} forces JSON output but doesn't validate schema. Function calling validates against a specific schema - use it for structured extraction.
When to Break These Rules
- Use Claude API (Anthropic) when tasks require very long documents (200K context) or the task benefits from Claude's stronger instruction following.
- Use local models (Ollama, vLLM) when data privacy prevents sending content to external APIs.
Reference Files
https://platform.openai.com/docs/api-referencehttps://platform.openai.com/docs/guides/function-calling