C.W.K.
Stream
Lesson 01 of 08 · published

The Chat Completions API

~22 min · chat-completions, messages, roles

Level 0Tokenizer
0 XP0/54 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

The Chat Completions API is the foundational interface for communicating with OpenAI's language models. Every interaction follows a simple request-response pattern: you send a list of messages with assigned roles, and the API returns a completion.

The message array is the heart of every request. Each message has a role and content. The available roles are:

  • system / developer — High-level instructions shaping the model's behavior. Newer GPT-5.x models prefer developer.
  • user — Messages from the human end-user.
  • assistant — Previous model responses (used for multi-turn context).
  • tool — Results from tool/function calls, referencing a tool_call_id.

Request / Response Lifecycle

The basic flow is straightforward:

For streaming responses, you set stream: true and receive Server-Sent Events:

Minimal Python Example

The response includes a choices array (usually one element), each with a message object and a finish_reason indicating why generation stopped (stop, length, tool_calls, or content_filter). The usage object tells you exactly how many tokens were consumed.

Code

Request shape (HTTP)·text
Client → POST /v1/chat/completions (messages[], model, params)
       ← HTTP 200 { id, object, created, model, choices[], usage }
Streaming SSE shape·text
Client → POST /v1/chat/completions (stream: true)
       ← HTTP 200 text/event-stream
         data: {"id":"...","choices":[{"delta":{"content":"..."}}]}
         data: [DONE]
Minimal Python example·python
from openai import OpenAI

client = OpenAI()  # reads OPENAI_API_KEY from env

completion = client.chat.completions.create(
    model="gpt-5.4",
    messages=[
        {"role": "developer", "content": "You are a concise assistant."},
        {"role": "user", "content": "What is the speed of light?"},
    ],
    temperature=0.7,
    max_completion_tokens=200,
)

print(completion.choices[0].message.content)
print(f"Tokens used: {completion.usage.total_tokens}")
print(f"Finish reason: {completion.choices[0].finish_reason}")

External links

Exercise

Send a single chat completion that uses both a developer message and a user message, then print the assistant text, the finish_reason, and total tokens used. Then change max_completion_tokens to a tiny number (e.g. 5) and run it again — confirm finish_reason becomes 'length'.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.