Overview
Text generation endpoints support Server-Sent Events (SSE) streaming. Setstream: true to receive output token by token as it’s generated.
Streaming-Enabled Endpoints
/v1/chat/completions— OpenAI compatible/v1/messages— Claude Messages/v1/responses— OpenAI Responses API
Example
The examples,choices, [DONE], and final-frame usage.cost below apply to /v1/chat/completions. Claude Messages and OpenAI Responses use different event structures; follow their endpoint documentation. Replace YOUR_MODEL with a text model available to your account.
SSE Data Format
Each Chat Completions chunk is adata: {json} SSE event:
data: [DONE].
Final Frame Usage & Billing (cost)
When usage output is enabled for Chat Completions, the final usage frame before[DONE] carries the call’s usage and billed quota. Its choices array may be empty, so check the array before accessing choices[0]:
usage.costis the call’s billed quota (an integer), not US dollars (USD);150means 150 quota- It is sent by default; explicitly passing
stream_options: {"include_usage": false}disables it - With the OpenAI SDK, the final chunk’s
chunk.usagecontains this data - If the connection drops or no usage frame arrives, do not treat the missing
costas zero; check the console usage records