Skip to content
Veil

Streaming

Event formats, the usage chunk and what a broken stream looks like.

Send "stream": true on either endpoint. The reply is text/event-stream.

OpenAI format

curl -N https://veil.st/openai/v1/chat/completions \
  -H "Authorization: Bearer ak_..." \
  -H "Content-Type: application/json" \
  -d '{
    "model": "deepseek-v4.1-flash-1017-abliterated",
    "messages": [{"role": "user", "content": "Count to five."}],
    "max_tokens": 200,
    "stream": true
  }'

Each event is a chat.completion.chunk. After the last content chunk comes one chunk with an empty choices array and the usage for the request, then data: [DONE]. You do not need to send stream_options to get it. Skip chunks with no choices when you read deltas.

data: {"id":"chatcmpl-...","object":"chat.completion.chunk","model":"deepseek-v4.1-flash-1017-abliterated","choices":[{"index":0,"delta":{"role":"assistant","content":"One"},"finish_reason":null}]}

data: {"id":"chatcmpl-...","object":"chat.completion.chunk","model":"deepseek-v4.1-flash-1017-abliterated","choices":[{"index":0,"delta":{"content":", two"},"finish_reason":null}]}

data: {"id":"chatcmpl-...","object":"chat.completion.chunk","model":"deepseek-v4.1-flash-1017-abliterated","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}

data: {"id":"chatcmpl-...","object":"chat.completion.chunk","model":"deepseek-v4.1-flash-1017-abliterated","choices":[],"usage":{"prompt_tokens":12,"completion_tokens":9,"total_tokens":21}}

data: [DONE]

When the thinking model streams its reasoning, that text arrives in delta.reasoning_content or delta.reasoning, separate from delta.content.

stream = client.chat.completions.create(
    model="deepseek-v4.1-flash-1017-abliterated",
    messages=[{"role": "user", "content": "Count to five."}],
    max_tokens=200,
    stream=True,
)
for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")
    if chunk.usage:
        print("\n", chunk.usage)

Anthropic format

The standard event sequence, ending with message_stop. Usage is in message_start and message_delta.

event: message_start
data: {"type":"message_start","message":{"id":"msg_...","type":"message","role":"assistant","model":"deepseek-v4.1-flash-1017-abliterated","content":[],"usage":{"input_tokens":12,"output_tokens":0}}}

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"}}

event: content_block_stop
data: {"type":"content_block_stop","index":0}

event: message_delta
data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":9}}

event: message_stop
data: {"type":"message_stop"}

When a stream breaks

If the model stops partway, or the stream ends without its normal final event, one last error event is sent and the stream closes. The HTTP status is still 200, because it was sent with the first byte. You are charged only for what was delivered.

OpenAI format:

data: {"error":{"message":"The model stopped responding. You were charged only for what was delivered.","type":"upstream_error","param":null,"code":"upstream_error"}}

Anthropic format:

event: error
data: {"type":"error","error":{"type":"overloaded_error","message":"The model stopped responding. You were charged only for what was delivered."}}

Treat a stream that ends without [DONE] or message_stop as incomplete. The text received so far is valid, and you can retry or continue from it.

Cancelling

Closing the connection stops the model and settles the bill for the tokens produced up to that point. Nothing more is charged after you disconnect.

Errors before the stream starts

Authentication, validation, balance and rate limit errors arrive as a normal JSON response with a 4xx or 5xx status, not as an event. Check the status before reading the body as a stream.

Streaming | Docs | Veil