Skip to main content

Overview

Text generation endpoints support Server-Sent Events (SSE) streaming. Set stream: 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 a data: {json} SSE event:
When the stream ends, you’ll receive 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.cost is the call’s billed quota (an integer), not US dollars (USD); 150 means 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.usage contains this data
  • If the connection drops or no usage frame arrives, do not treat the missing cost as zero; check the console usage records