Enable Streaming Output
Setstream=True in the request to enable streaming. The SDK then returns an iterable — loop over it to read data chunks one by one. Each chunk has a structure similar to a completion, except the message field is replaced by a delta field. If you need token usage in a streaming response, also pass stream_options: {"include_usage": true} (write {"include_usage": True} in the Python SDK):
The examples on this page use the latest model
kimi-k3 by default. K3 configures reasoning effort with the top-level reasoning_effort request field (supports "low" / "high" / "max", default "max"). To use another model such as kimi-k2.6, just replace the model field — parameter configurations differ across models. See the Model Parameter Reference.- python
- node.js
Parse the SSE Response Body
With streaming enabled, the API no longer returns a JSON response (Content-Type: application/json); it returns Content-Type: text/event-stream (SSE) instead, which lets the server continuously push Tokens to the client. An SSE response body looks like this:
data: prefix, followed by a valid JSON object, and ends with two newline characters \n\n. Once all chunks are transmitted, the server sends data: [DONE] to mark completion, at which point you can close the connection.
Note: always use data: [DONE] to determine whether the data has been fully transmitted, not finish_reason or any other means. If you have not received data: [DONE], do not consider the transmission complete even if finish_reason=stop was received; in other words, until data: [DONE] arrives, the message should be considered incomplete.
During streaming, the content field is delivered chunk by chunk; role is not repeated in every chunk and appears only in the first one. When you pass stream_options: {"include_usage": true}, the server sends a final statistics chunk before [DONE]. This chunk has an empty choices array, and the total usage for the request is in the top-level usage field.
Count Token Usage
There are two ways to count tokens. We recommend passingstream_options: {"include_usage": true}, waiting until all chunks have been transmitted, and reading the top-level usage field of the final statistics chunk to see the request’s prompt_tokens/completion_tokens/total_tokens:
The final statistics chunk does not contain model output, so its
choices array is empty. When parsing a stream, do not assume that every chunk has choices[0]; read the total usage from chunk.usage on the final statistics chunk.- python
- node.js
Stop Streaming Output
To terminate the output early, simply close the HTTP connection or discard subsequent chunks — for example,break out of the loop:
Handle SSE Without an SDK
In a language without an SDK, or when the SDK cannot accommodate your business logic, you can interface with the HTTP API directly to handle streaming output. The following examples show how to read and parse the SSE response body line by line; see the code comments for details:- python
- node.js
- Send an HTTP request with the
streamparameter set totruein the request body; - Check the
Content-Typein the responseHeaders—text/event-streammeans the response is a streaming output; - Read the response line by line and parse the data chunks (in JSON format), locating chunk boundaries via the
data:prefix and newline characters\n; - A chunk whose content is
[DONE]marks the end of the transmission.
Multiple Responses (n Parameter)
Current models (
kimi-k3, kimi-k2.7-code, kimi-k2.6) fix n at 1 and do not support returning multiple responses in a single request. Passing an n greater than 1 returns a 400 error (invalid n: only 1 is allowed for this model) for both streaming and non-streaming requests. See the Model Parameter Reference for per-model parameter constraints.