Skip to main content
Models generate responses one token at a time. Instead of waiting for the entire response, you can get each token as the model generates it. This streaming improves the response-time experience—especially for longer responses, where someone would otherwise have to wait with no feedback from the model. You can control whether and how Friendli Engine streams a response. Once you turn streaming on, you need to change only how you read and handle the response. You can also cancel a stream to stop the model from generating tokens.

Streaming Parameters

With streaming parameters, you can control whether and how Friendli Engine streams a response. You pass them as request body parameters. To learn more, see Control a Capability. You can control streaming with the following parameters:

stream

You can turn streaming on and off by setting the stream parameter to true or false, respectively. It controls whether Friendli Engine streams a response as the model generates it. By default, it’s off. If streaming is off, Friendli Engine waits for the model to generate the response before returning it. If streaming is on, Friendli Engine streams the response in chunks as the model generates each token, and sends each chunk as a server-sent event (SSE). To turn streaming on, you can use the following code:

stream_options

With the stream_options parameter, you can control how Friendli Engine streams a response by setting its fields. You can set the following fields:

include_usage

If you turn streaming on, you can include or exclude token usage by setting the include_usage field to true or false, respectively. It controls whether Friendli Engine includes token usage in the stream. By default, Friendli Engine excludes it. If you need token usage (for example, to track costs), include it. You get it in an additional chunk at the end of the stream. Note that your choice doesn’t change the number of tokens the model uses to generate a response. You pay the same either way. To include token usage, you can use the following code:

Read a Stream

Friendli Engine streams incremental chunks in an event stream. For example, you get the following stream from a reasoning model:
The first chunk in the stream sets role to assistant. To learn how to read the rest of the stream, see the following:
  • Reasoning chunks: If the model reasons, you get its chain of thought first. It comes in a stream of reasoning chunks, where each chunk usually contains one token in both reasoning and reasoning_content. To learn more, see Reasoning.
  • Content chunks: You get the model’s response next. It comes in a stream of content chunks, where each chunk usually contains one token in content.
  • Finish chunk: After the last token, you get a finish chunk. Its delta object is empty, and its finish_reason is stop, tool_calls (if the model calls a tool), or length (if the model reaches a token limit, such as max_tokens).
  • Usage chunk: If you include token usage, you get an additional chunk with token counts, such as prompt_tokens and completion_tokens. Its choices array is empty, and you should guard against it. To learn more, see include_usage.
  • Done message: The stream ends with data: [DONE].

Cancel a Stream

You can cancel a stream by closing its connection. When you close it, the model stops generating. If you pay per token, you pay only for the tokens you use, such as your prompt tokens and the completion tokens up to the point the model stops generating. If, for example, a user wants to stop the model, you can close the connection for them. The stream ends midstream with no finish chunk, usage chunk, or done message. To cancel a stream, you can use the following code:

API Reference

Look up operations and parameters:

Model APIs

Learn to use the API for a curated set of models available at usage-based pricing.

Dedicated Endpoints

Learn to use the API for any model you deploy on dedicated GPUs reserved for you.
Last modified on November 9, 2026