Documentation
Streaming
The A4F API allows streaming responses from any model. This is useful for building chat interfaces or other applications where the UI should update as the model generates the response.
To enable streaming, you set the stream
parameter to true
in your request to the /v1/chat/completions endpoint. The model will then stream the response to the client in chunks, rather than returning the entire response at once.
Here is an example of how to stream a response and process it:
Additional Information
For Server-Sent Events (SSE) streams, A4F passes through the data format provided by the underlying model provider, which is generally compatible with the OpenAI SSE format. Some providers might occasionally send comments (lines starting with :
) as keep-alive pings to prevent connection timeouts. These can usually be ignored by SSE clients.
Standard SSE client libraries and parsers should handle these streams correctly:
- eventsource-parser (JavaScript/TypeScript)
- Official OpenAI SDKs (Python, Node.js) handle streaming internally when
stream=True
is set. - @microsoft/fetch-event-source for robust SSE handling in the browser.
Each data chunk in the stream typically follows the format data: {...json...}
, followed by two newlines. The stream is terminated by data: [DONE]
.
Stream Cancellation
Streaming requests can be cancelled by aborting the underlying HTTP connection. For supported providers, A4F will attempt to propagate this cancellation to stop model processing and billing.
Provider Support for Cancellation
Effective stream cancellation (i.e., stopping backend processing and further billing) depends on the capabilities of the underlying model provider. A4F endeavors to forward cancellation signals when feasible.
If a provider does not support immediate cancellation or if the signal is not propagated in time, the model might continue processing the request fully, and you could be billed accordingly, even if your client has stopped receiving chunks.
To implement stream cancellation in your client:
Cancellation and Billing
Was this page helpful?