GPT API Basics Cheat Sheet

OpenAI API fundamentals — chat completions, streaming, function calling, token management, error handling, and rate limits for GPT-4, GPT-4o, and GPT-3.5 models.

Last Updated: May 1, 2025

Chat Completion Basics

POST https://api.openai.com/v1/chat/completions
Single 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.7
Creativity control — 0=deterministic, 1=creative, 2=max
max_tokens: 1024
Response length limit — count both input+output against model limit
stream: true
Server-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: result
Send function result back to model

Token Management

ItemDescription
tiktoken libraryCount 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 tokensCount against input limit — keep under 500 tokens for efficiency
Truncation strategySummarize old messages rather than dropping — preserve conversation context
Token limitsGPT-4o: 128K. GPT-4: 8K/32K/128K. GPT-3.5: 4K/16K.

Error Handling

ItemDescription
401 UnauthorizedInvalid API key — check key, billing, organization
429 Rate LimitToo many requests — implement exponential backoff with jitter
500 Server ErrorOpenAI side — retry with backoff, max 3 attempts
Context Length ExceededInput too long — trim messages or use model with larger context
Content FilterRequest 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.