Reference · How-to · ~10 min

How to call OpenAI API with streaming

Streaming shows tokens as they arrive — better UX for long replies.

Streaming shows tokens as they arrive — better UX for long replies.

Steps

1. Set `stream=True` on chat completions

2. Iterate over chunks; append `delta.content` to your UI buffer

3. Handle `[DONE]` / stream end

4. Keep API key on the **server**, not in browser code

Python example

from openai import OpenAI
client = OpenAI()
stream = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Explain RAG in 3 bullets."}],
    stream=True,
)
for chunk in stream:
    part = chunk.choices[0].delta.content or ""
    print(part, end="", flush=True)

Node example

const stream = await openai.chat.completions.create({
  model: "gpt-4o-mini",
  messages: [{ role: "user", content: "Hello" }],
  stream: true,
});
for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}

**Try the lesson:** `llm-api-hello` in Lane D