Streaming
Receive answers token by token as server-sent events in both API styles.
Set "stream": true to receive the answer as server-sent events while it is generated. With curl, add -N to print events as they arrive.
Messages API
curl -N "$KEYDAN_API_BASE/v1/messages" \
-H "x-api-key: $KEYDAN_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model": "kd-balanced", "max_tokens": 256, "stream": true,
"messages": [{"role": "user", "content": "Count to three."}]}'The stream is a sequence of named events: message_start, then for each content block content_block_start, content_block_delta events and content_block_stop, then message_delta (stop reason and final output tokens) and message_stop. ping events may appear at any time; an error event ends a failed stream.
event: message_start
data: {"type":"message_start","message":{"id":"msg_0a1b2c3d","type":"message","role":"assistant","model":"kd-balanced","content":[],"stop_reason":null,"usage":{"input_tokens":12,"output_tokens":1}}}
event: content_block_start
data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"One, two, three."}}
event: content_block_stop
data: {"type":"content_block_stop","index":0}
event: message_delta
data: {"type":"message_delta","delta":{"stop_reason":"end_turn","stop_sequence":null},"usage":{"output_tokens":7}}
event: message_stop
data: {"type":"message_stop"}Chat Completions API
curl -N "$KEYDAN_API_BASE/v1/chat/completions" \
-H "Authorization: Bearer $KEYDAN_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model": "kd-balanced", "stream": true,
"stream_options": {"include_usage": true},
"messages": [{"role": "user", "content": "Count to three."}]}'Each event is a chat.completion.chunk with a delta. The last chunk with a choice carries finish_reason. With stream_options.include_usage, one more chunk with an empty choices list carries the usage (and cost). The stream ends with data: [DONE].
data: {"id":"chatcmpl-8f2c1d","object":"chat.completion.chunk","created":1763553600,"model":"kd-balanced","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]}
data: {"id":"chatcmpl-8f2c1d","object":"chat.completion.chunk","created":1763553600,"model":"kd-balanced","choices":[{"index":0,"delta":{"content":"One, two, three."},"finish_reason":null}]}
data: {"id":"chatcmpl-8f2c1d","object":"chat.completion.chunk","created":1763553600,"model":"kd-balanced","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
data: {"id":"chatcmpl-8f2c1d","object":"chat.completion.chunk","created":1763553600,"model":"kd-balanced","choices":[],"usage":{"prompt_tokens":12,"completion_tokens":7,"total_tokens":19,"cost":0.000169}}
data: [DONE]Notes
- Errors found before the answer starts come back as a normal JSON error with its status code.
- A stream that breaks after it started is never ended silently: in the Messages API it ends with an
errorevent ("type": "overloaded_error"); in the Chat Completions API withdata: {"error": {"type": "server_error", "code": "stream_error", ...}}and no[DONE]. - Keep-alive events (
ping) and comment lines (: ping) may appear; skip them. - Usage and cost are those of the whole answer, also when you stop reading early.
- A streamed answer is never stored for idempotent replay.