Skip to content

Streaming

Set "stream": true to receive tokens as they are generated, as server-sent events in the same format the provider uses.

Chat Completions

Add stream_options.include_usage to get a final chunk with token counts. Without it the stream ends without usage, although you are still billed on the real usage.

curl
curl -N https://api.kyvalt.com/v1/chat/completions \
  -H "Authorization: Bearer $KYVALT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "deepseek-v4-pro",
    "stream": true,
    "stream_options": {"include_usage": true},
    "max_tokens": 200,
    "messages": [{"role": "user", "content": "Count to five."}]
  }'
stream
data: {"object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"One"}}]}

data: {"object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}

data: {"object":"chat.completion.chunk","choices":[],"usage":{"prompt_tokens":12,"completion_tokens":9,"total_tokens":21}}

data: [DONE]

Python

python
stream = client.chat.completions.create(
    model="deepseek-v4-pro",
    stream=True,
    stream_options={"include_usage": True},
    messages=[{"role": "user", "content": "Count to five."}],
)
for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)

Messages

Streams use the Anthropic event types (message_start, content_block_delta, message_delta, message_stop). Usage is part of message_start and message_delta, so no extra option is needed. The Anthropic SDK's client.messages.stream(...) works unchanged.

Errors during a stream

An error found before the first token is a normal HTTP error response. If the seller fails after tokens have been sent, the stream ends with an error event that has the same error object as other errors. Interrupted requests are not charged.