Using the API
Streaming
Receive the article token by token with server-sent events, and get usage in the last chunk.
Set stream to true and the API sends the article as server-sent events while it is being written. The format matches OpenAI, so SDK stream helpers work unchanged.
Event format
Each event is a line that starts with data: followed by a chat.completion.chunk object. The first chunk carries the assistant role, the following chunks carry text in delta.content, and the stream ends with data: [DONE].
Usage in the final chunk
Add stream_options: {"include_usage": true} to receive token counts. They arrive in one extra chunk just before [DONE], with an empty choices array. Check for an empty array before reading choices[0].
Cold starts and keepalives
When a model has been idle, the first request can wait a few minutes for a GPU. During that time the API sends comment lines that start with a colon to keep the connection open. SSE clients ignore them. Set your HTTP client timeout to at least 10 minutes.
Errors that happen after the stream has started arrive as a data: event that contains an error object, followed by [DONE]. Handle both in your stream loop.