Set stream: true to receive server-sent events. The transport is plain SSE over HTTP/2 and works through any standard OpenAI client.
stream = client.chat.completions.create(
model="gpt-5-mini",
messages=[{"role": "user", "content": "Count to five."}],
stream=True,
)
for chunk in stream:
delta = chunk.choices[0].delta.content
if delta:
print(delta, end="", flush=True)
Usage on streams
We always request stream_options.include_usage from upstream, so the final event before [DONE] carries a usage object. If the upstream provider omits it, we fall back to an estimated count and flag the request in your usage log.
data: {"choices":[{"delta":{"content":"One"}}]}
data: {"choices":[{"delta":{"content":", two"}}]}
data: {"choices":[],"usage":{"prompt_tokens":12,"completion_tokens":7}}
data: [DONE]
Billing is settled after the stream flushes, not per chunk. A client that disconnects early is still charged for tokens already generated upstream.
Dokumentasi teknikal diselenggara dalam bahasa Inggeris.