Last Updated: May 1, 2025
Chat Completion Basics
POST https://api.openai.com/v1/chat/completionsSingle endpoint for all chat models
model: 'gpt-4o'Best overall: fast + capable + vision + function calling
model: 'gpt-4-turbo'Larger context (128K), slightly slower, high quality
messages: [{role:'system',content:'...'},{role:'user',content:'...'}]Message array — system contextualizes, user asks
temperature: 0.7Creativity control — 0=deterministic, 1=creative, 2=max
max_tokens: 1024Response length limit — count both input+output against model limit
stream: trueServer-sent events — response arrives token-by-token
Function Calling
tools: [{type:'function',function:{name,description,parameters}}]Define available functions
tool_choice: 'auto'Model decides when to call a function (default)
tool_choice: {type:'function',function:{name:'get_weather'}}Force a specific function call
finish_reason: 'tool_calls'Model wants to call a function — extract arguments, execute, send back
role: 'tool', tool_call_id, content: resultSend function result back to model
Token Management
| Item | Description |
|---|---|
tiktoken library | Count tokens before sending — avoid exceeding model limits |
Pricing (per 1M tokens) | GPT-4o: $5 input / $15 output. GPT-4: $30/$60. GPT-3.5: $0.50/$1.50. |
System prompt tokens | Count against input limit — keep under 500 tokens for efficiency |
Truncation strategy | Summarize old messages rather than dropping — preserve conversation context |
Token limits | GPT-4o: 128K. GPT-4: 8K/32K/128K. GPT-3.5: 4K/16K. |
Error Handling
| Item | Description |
|---|---|
401 Unauthorized | Invalid API key — check key, billing, organization |
429 Rate Limit | Too many requests — implement exponential backoff with jitter |
500 Server Error | OpenAI side — retry with backoff, max 3 attempts |
Context Length Exceeded | Input too long — trim messages or use model with larger context |
Content Filter | Request flagged by safety system — rephrase or use moderation endpoint first |
Pro Tip: Always use streaming for user-facing applications — it cuts perceived latency from 5+ seconds to near-instant. The first token appears in ~200ms with streaming.