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.
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
Code examples
Minimal request (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)
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)
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
https://api.openai.com/v1/chat/completions
Sends a conversation and returns the next assistant message.
Bearer token in the Authorization header
| Name | Type | Required | Description |
|---|---|---|---|
model |
string | yes | Model id, e.g. gpt-4o |
messages |
array | yes | Ordered list of {role, content} |
temperature |
number | no | 0–2. Lower is more deterministic |
max_tokens |
integer | no | Ceiling on the reply |
stream |
boolean | no | Server-sent events |
| Header | Value | Notes |
|---|---|---|
Authorization |
Bearer $OPENAI_API_KEY |
Required |
Content-Type |
application/json |
Required |
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 }
}
| Code | Meaning | What to do |
|---|---|---|
401 |
Bad or missing API key | Check the header name and that the key is not expired |
400 |
Malformed request | Usually a bad messages array or an unknown parameter |
429 |
Rate or quota limit | Exponential backoff with jitter; check your usage tier |
500 / 503 |
Upstream failure | Retry idempotently; do not retry non-idempotent side effects |
context_length_exceeded |
Prompt + reply exceeds the window | Trim history or summarise older turns |
Frequently asked questions
Why is my reply cut off mid-sentence?
Does temperature 0 guarantee identical output?
Resources
Was this cheat sheet useful?
Comments
No comments yet — be the first.