Add
"stream": true to your request body and consume the event-stream as it arrives.
Example – Python
Python
Example – TypeScript / browser fetch
TypeScript
About the SSE payload
- Each line that begins with
data:contains a JSON chunk. - Lines beginning with
:are comments and can be safely ignored. - A final line
data: [DONE]signals the end of the stream.
Cancelling streams
Simply abort or close the underlying HTTP connection (e.g. viaAbortController in the browser). The request will stop; however, you may be billed for the tokens of the full stream.