Skip to main content
Set stream: true to receive the answer as server-sent events while it is generated, instead of one JSON body at the end.

Event format

Each event is a data: line with one chat.completion.chunk, and the stream ends with data: [DONE]:

Token usage

To get the token counts of a stream, set stream_options.include_usage. The last chunk before data: [DONE] then carries usage and an empty choices list:
Your usage is counted either way; this option only decides whether you see the counts.

Keep-alive comments

When a stream has nothing to send for a few seconds, such as while a long prompt is read, fastrun sends a comment line so that proxies and clients don’t close the connection:
Lines that start with : are comments in the server-sent events format. OpenAI SDKs and other SSE clients skip them; if you parse the stream yourself, skip them too.

Errors during a stream

If no GPU server has answered within 2 seconds, fastrun starts the response right away with status 200 and keeps it open with keep-alive comments until one does. If the request then fails, the status line has already been sent, so the error arrives as an event instead:
OpenAI SDKs raise this as an API error. If you parse the stream yourself, check each event for an error field. Treat it like an HTTP error with the same code: see Errors and retries.

Stopping early

Closing the connection stops generation. Use it when the user cancels or you have what you need.