Skip to main content
On a model whose page lists streaming, stream tokens as they are generated so a user sees the answer appear instead of waiting for all of it. Any chat UI wants this. So does anything long enough that silence looks like a hang.

Minimal code

What your loop will see

Streaming timeline”stream”: truerequestHTTP 200, event stream opensfirst tokenwithin the 60s TTFT budgetdelta framesone complete frame at a timeusage frameonly if you opted in240s response budget, then 504How a stream endsdata: [DONE]the normal terminal frame after the last deltaerror framea stream that dies upstream ends with one final frame carrying the standardupstream_stream_errorenvelope, on the same HTTP 200. Handle it in your frame loop.Only tokens actually delivered to you are billed.
Streaming timeline”stream”: truerequestHTTP 200, event stream opensfirst tokenwithin the 60s TTFT budgetdelta framesone complete frame at a timeusage frameonly if you opted in240s response budget, then 504How a stream endsdata: [DONE]the normal terminal frame after the last deltaerror framea stream that dies upstream ends with one final frame carrying the standardupstream_stream_errorenvelope, on the same HTTP 200. Handle it in your frame loop.Only tokens actually delivered to you are billed.

What to tune

Common mistakes

  • Forgetting flush=True in Python. Without it stdout buffers and the tokens arrive in clumps.
  • Treating an error frame as a network failure. A stream that dies upstream ends with one final frame carrying the standard error envelope, on the same HTTP 200. Handle it inside your frame loop.
  • Breaking the loop too early. The final content frame carries finish_reason and an empty delta.content. Keep reading until the stream closes.
  • Streaming through a buffering proxy. Some edges buffer server-sent events. Stream direct, or disable buffering on that layer.
  • Mixing n > 1 with streaming. Allowed, but every delta carries its own choices[].index. Route by index or the outputs interleave.

Next steps

Stream reference

Exact frame shapes, and the usage frame.

Tool calling

Let the model act, then answer.

Structured output

Get JSON that matches your schema.