Chat Completions API Cheat Sheet

Endpoints, parameters, streaming and error codes at a glance

The parameters that actually change behaviour, a working request in three languages, the error codes you will hit in production, and what to do about each one.

Category: AI APIs Difficulty: Intermediate Version: 1.0 Updated: July 31, 2026 Author: Sabir

Core parameters

The ones that meaningfully change output. Everything else is situational.

Parameter What it does Sensible default
model Which model answers the request The newest one you can afford
messages The conversation so far, oldest first system + user
temperature Randomness. 0 = repeatable, 1 = creative 0 for extraction, 0.7 for writing
max_tokens Ceiling on the REPLY length only Set it — an unbounded reply is a runaway bill
stream Send tokens as they are produced true for anything user-facing
response_format Force valid JSON {"type":"json_object"} for pipelines
tools Functions the model may call Only what this request could need
seed Best-effort determinism Set when you need reproducibility

Token budgeting

The context window covers INPUT + OUTPUT together. A 128k window with a 120k prompt leaves you 8k of answer. Budget backwards from the reply length you need, and remember every prior turn you resend is billed again.

Code examples

Minimal request (Python) Python

The smallest thing that works, with streaming on.

from openai import OpenAI

client = OpenAI(api_key="YOUR_KEY")

stream = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "system", "content": "You are concise."},
        {"role": "user", "content": "Explain vector embeddings in two sentences."},
    ],
    temperature=0.3,
    max_tokens=300,
    stream=True,
)

for chunk in stream:
    delta = chunk.choices[0].delta.content
    if delta:
        print(delta, end="", flush=True)

Minimal request (JavaScript) JavaScript

import OpenAI from 'openai';

const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });

const stream = await client.chat.completions.create({
  model: 'gpt-4o',
  messages: [
    { role: 'system', content: 'You are concise.' },
    { role: 'user', content: 'Explain vector embeddings in two sentences.' },
  ],
  temperature: 0.3,
  max_tokens: 300,
  stream: true,
});

for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content ?? '');
}

Retry with backoff (Python) Python

Rate limits are normal, not exceptional. Handle them from day one.

import time, random

def with_backoff(fn, attempts=5):
    for i in range(attempts):
        try:
            return fn()
        except Exception as e:
            status = getattr(e, 'status_code', None)
            if status not in (429, 500, 502, 503, 529) or i == attempts - 1:
                raise
            # full jitter: avoids a thundering herd on recovery
            time.sleep(random.uniform(0, 2 ** i))

API reference

POST https://api.openai.com/v1/chat/completions

Sends a conversation and returns the next assistant message.

Auth: Bearer token in the Authorization header

ParameterTypeRequiredDescription
modelstringyesModel id, e.g. gpt-4o
messagesarrayyesOrdered list of {role, content}
temperaturenumberno0–2. Lower is more deterministic
max_tokensintegernoCeiling on the reply
streambooleannoServer-sent events
curl https://api.openai.com/v1/chat/completions \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o",
    "messages": [{"role": "user", "content": "Hello"}]
  }'
{
  "id": "chatcmpl-123",
  "object": "chat.completion",
  "model": "gpt-4o",
  "choices": [{
    "index": 0,
    "message": { "role": "assistant", "content": "Hello! How can I help?" },
    "finish_reason": "stop"
  }],
  "usage": { "prompt_tokens": 9, "completion_tokens": 8, "total_tokens": 17 }
}
CodeMeaningWhat to do
401Bad or missing API keyCheck the header name and that the key is not expired
400Malformed requestUsually a bad messages array or an unknown parameter
429Rate or quota limitExponential backoff with jitter; check your usage tier
500 / 503Upstream failureRetry idempotently; do not retry non-idempotent side effects
context_length_exceededPrompt + reply exceeds the windowTrim history or summarise older turns
Rate limits: Per-model RPM and TPM, tier-dependent. Always read the x-ratelimit-* response headers rather than hard-coding a number.

FAQs

Why is my reply cut off mid-sentence?

You hit max_tokens. Check finish_reason — if it is "length", raise the ceiling or ask for a shorter answer.

Does temperature 0 guarantee identical output?

No. It makes sampling greedy but hardware and model updates still introduce variance. Use seed for best-effort reproducibility.

Resources