Streaming
Stream Electron tokens over Server-Sent Events, same as OpenAI.
Set "stream": true and the response becomes Server-Sent Events (SSE). Each token (or small group of tokens) arrives as a data: {...} line. The stream ends with a data: [DONE] marker.
This is the same wire format OpenAI uses, so OpenAI client SDKs work without changes.
Request
Always send stream_options.include_usage: true if you bill or log per-token. With it, the server emits a final usage chunk with exact token counts. That chunk only arrives if the stream finishes, so if your client might disconnect mid-stream, also send continuous_usage_stats: true to get the counts so far on every chunk.
SSE format
Each delta chunk:
Field-by-field:
Final usage chunk (with include_usage: true)
After the final content delta and before [DONE], the server emits one extra chunk with empty choices and a usage object:
Running usage on every chunk
Add continuous_usage_stats: true next to include_usage: true to get the token counts so far on every chunk, not just the last one:
Use this when you need up-to-date counts during the stream. Without include_usage, it has no effect.
Consuming the stream
Errors mid-stream
An error before the stream starts comes back as a normal JSON error with an HTTP status. See Errors.
Once the stream has started (you’ve received 200 with text/event-stream), a failure arrives as an error event, followed by [DONE]:
The OpenAI SDKs raise an error when they read this event, so a cut-off reply is never returned as complete. If you parse the stream yourself, check each data: line for an error key before reading choices.
Client disconnect behavior
If the client closes the connection mid-stream, generation stops. You’re billed for the prompt and for the output tokens generated before the disconnect, not for the rest of the reply.
Latency profile
- Time to first token (TTFT): typically under 300 ms warm.
- Per-token interval after the first token: a few milliseconds; full sentences arrive in tens of milliseconds.
For voice-agent pipelines, start your TTS engine on the first delta.content chunk to mask end-to-end latency. See Best Practices and the Voice Agent cookbook.