Stream responses token-by-token over server-sent events for a faster time-to-first-token.
Streaming lets you display output as it arrives rather than waiting for the full completion. The OpenDunes API implements the standard SSE (server-sent events) format, so any library that handles OpenAI streaming works out of the box.
Add "stream": true to your request body. The response switches from application/json to text/event-stream and the connection stays open until the model finishes.
Each event arrives as a line starting with data: followed by a JSON object, then a blank line. The chunks carry delta content — only the new token(s) in choices[0].delta.content, not the full text so far.
The final data chunk (before [DONE]) carries the usage object with token counts for the full request. Accumulate your own buffer client-side by concatenating delta.content from each chunk.
Close the connection at any point to cancel the stream. The server stops generating as soon as it detects the disconnect.
In Python, call stream.close(). In Node, call stream.controller.abort(). With raw fetch, call controller.abort() on the AbortController you passed to the request.
Billing settles on the tokens already sent — you are not charged for tokens that were never transmitted.
If the request is rejected before the stream opens — 401 invalid_api_key, or 402 insufficient_credits caught by the balance check — the server returns a normal non-streaming error response with the appropriate HTTP status code and JSON body. Your SSE loop never starts.
Once the response has been sent as 200 text/event-stream, the HTTP status can no longer change. If something fails after that point — the upstream provider erroring, or a balance reservation failing right as the stream opens — the server emits an error event followed by [DONE]:
A reservation failure surfaces the same way with "error": "insufficient_credits". Detect either by checking whether the final data chunk (before [DONE]) has an error field instead of choices. Handle it by surfacing the message to the user and offering a retry.
Billing settles on the tokens received before the error. See Errors for the full list of error codes.