Streaming
Set "stream": true to receive the response as server-sent events. Each event carries a chunk with the next piece of text in choices[0].delta.content. The stream ends with data: [DONE].
Python
stream = client.chat.completions.create(
model="moonshotai/kimi-k3.1-pro",
messages=[{"role": "user", "content": "Tell me a short story."}],
stream=True,
)
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
TypeScript
const stream = await client.chat.completions.create({
model: "moonshotai/kimi-k3.1-pro",
messages: [{ role: "user", content: "Tell me a short story." }],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}
Token usage in streams
Usage is not included in streamed chunks by default. Add "stream_options": {"include_usage": true} and the last chunk before [DONE] contains a usage object with prompt, cached and completion token counts.
Errors during a stream
If an error happens after the stream starts, you get an event with an error object instead of a chunk, and the stream closes. The object has the same shape as other errors:
data: {"error": {"message": "The model is temporarily over capacity.", "type": "server_error", "code": "overloaded"}}
A stream that ends without [DONE] has failed. Retry it.