Request carries model + role-tagged messages + sampling knobs; response carries assistant content + finish_reason + a usage block; streaming swaps the JSON body for SSE delta chunks.
Picture sending a structured letter to an assistant. The envelope says which assistant to deliver it to (the model name). Inside the envelope, every paragraph is labeled with who is speaking: system rules first, then a back and forth between user and assistant. You also include a sticky note with knobs like 'do not exceed 500 words' and 'be a little creative'. The assistant writes a reply with three things: the actual text, a stamp saying why she stopped writing, and a meter showing how many words you used in total. If you ticked the streaming box, instead of one finished letter you get a stack of postcards arriving one piece at a time.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
The chat completions endpoint is the lingua franca of hosted LLM serving in 2026. OpenAI's original shape, introduced in 2023, became the de facto industry standard because every client SDK and agent framework was written against it. Every serious provider (Anthropic, Google, Mistral, DeepSeek) now ships either a native OpenAI-compatible endpoint or a thin adapter, and every open-source serving stack (vLLM, TensorRT-LLM, SGLang) ships the front door so self-hosted deployments slot into existing client code with one base-URL change.
This deep dive walks through the request and response schemas in detail, the role of finish_reason as a dispatch contract, how streaming changes the wire format, and the production concerns that distinguish a working implementation from a correct one.
The request schema in detail
A chat completions request body has three components.
model, a string identifying which model to use. Hosted APIs accept names likegpt-5.5,claude-sonnet-4-6,gemini-3.1-pro. Self-hosted vLLM accepts the model path or alias configured at startup.messages, an array of message objects, each with aroleandcontent. The four roles aresystem(instructions and persona),user(input from the caller),assistant(prior model output, used for multi-turn context), andtool(results from function calls being fed back). Content is text by default; for multimodal endpoints it is an array of typed parts (text, image, audio).- Sampling and shaping parameters,
temperature(rescales logits),top_p(nucleus truncation),max_tokens(hard cap on output length),stop(sequences that terminate generation),presence_penaltyandfrequency_penalty(discourage repetition),seed(best-effort reproducibility), andtools/tool_choicefor function calling.
Multimodal content arrays
When content is an array, each entry has a type field (text, image_url, input_audio). GPT-5.5 and Claude Opus 4.7 both accept this shape. Image parts can carry a base64 data URL or a remote URL; vision models tokenize the image into a fixed number of vision tokens (often around 1024 for high-res images) which are billed in the usage block.
Situations where this technique stops working.
2–4 min · Everything important, quickly.
Real products, models, and research that use this idea.
- OpenAI's GPT-5.5 chat completions endpoint defines the canonical schema; every other provider adopts it or supplies an adapter.
- Anthropic's Messages API uses an OpenAI-compatible structure with role-tagged messages and adds explicit cache_creation / cache_read fields in the usage block.
What an interviewer would ask next. Try answering before peeking at the approach.
QHow does the chat completions contract handle multimodal inputs in 2026?
The content field per message becomes an array of typed parts: text parts, image parts (with base64 or URL), and increasingly audio parts. GPT-5.5, Claude Opus 4.7, and Gemini 3.1 Pro all use this shape. Tokenization for image parts is provider-specific and billed at separate rates.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Conflating the legacy completions endpoint with chat completions. Chat completions is structured around role-tagged messages, not a single prompt string, and finish_reason matters for tool-call dispatch.
60 second bullets to scan on the way to the call.
Required request fields: model, messages, optional sampling knobs
The four message roles (system, user, assistant, tool)
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.