Skip to main content
Menu
Build / Streaming
Build

Streaming

Receive a chat completion as an event stream, read usage and the receipt id, and keep the bytes you need to verify it.

6 minute readUpdated 13 November 2026curl · Python · TypeScript

Turn on streaming#

Send stream: true in a chat completion request. The response is text/event-stream in the OpenAI chunk format, so the stream helpers of the OpenAI SDKs work unchanged.

curl -N https://api.panward.ai/v1/chat/completions \
  -H "Authorization: Bearer $PANWARD_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "z-ai/glm-5.3",
    "messages": [{"role": "user", "content": "Summarise this discharge note."}],
    "stream": true,
    "stream_options": {"include_usage": true}
  }'

-N turns off curl's output buffering so the chunks show as they arrive. Disable response buffering in any reverse proxy of your own as well.

Event format#

Each frame is data: followed by one JSON-encoded chat.completion.chunk and a blank line. The text arrives in choices[0].delta.content, tool calls in delta.tool_calls. The stream ends with data: [DONE].

The event stream as written to the connection
data: {"id":"chatcmpl-01JC7Q5B1V","object":"chat.completion.chunk","created":1790000100,"model":"z-ai/glm-5.3","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]}

data: {"id":"chatcmpl-01JC7Q5B1V","object":"chat.completion.chunk","created":1790000100,"model":"z-ai/glm-5.3","choices":[{"index":0,"delta":{"content":"Hello"},"finish_reason":null}]}

data: {"id":"chatcmpl-01JC7Q5B1V","object":"chat.completion.chunk","created":1790000100,"model":"z-ai/glm-5.3","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}

data: {"id":"chatcmpl-01JC7Q5B1V","object":"chat.completion.chunk","created":1790000100,"model":"z-ai/glm-5.3","choices":[],"usage":{"prompt_tokens":9,"completion_tokens":2,"total_tokens":11}}

data: [DONE]

Usage in a stream#

Usage is sent only when stream_options.include_usage is true, in one extra final chunk whose choices is empty. stream_options is valid only with stream: true.

The receipt of a stream#

The receipt id is in the panward-receipt-id response header, so you have it as soon as the headers arrive, before the first chunk. Nothing is added to the stream itself. The receipt's response.emitted hash covers every byte of the event stream the gateway wrote, including data: [DONE], and its bytes and complete fields say how much was written and whether the stream reached its end.

Verify a stream#

Step 4 of verification compares the hash of your copy of the stream with the receipt. Keep the raw bytes as they arrived, before any event-stream parsing: a parser, a proxy that rewrites the stream or a re-encoded body changes the hash.

Keep the raw stream, then verify it
cat > request.json <<'EOF'
{"model": "z-ai/glm-5.3", "messages": [{"role": "user", "content": "Summarise this discharge note."}], "stream": true}
EOF

curl -N https://api.panward.ai/v1/chat/completions \
  -D headers.txt -o stream.bin \
  -H "Authorization: Bearer $PANWARD_API_KEY" \
  -H "Content-Type: application/json" \
  --data-binary @request.json

# The receipt id is the panward-receipt-id line of headers.txt.
warden verify rcpt_01JC7Q4N9W3XK2H8ZP5T6V7B8M --request request.json --response stream.bin

The verification guide explains each step and what it does not establish.

Verification level of a stream#

Streams on Sealed models reach the level sealed_attested_path. If your organization's policy requires a model-enclave signature, the gateway obtains the response unstreamed and streams it to you afterwards, which raises the time to the first token. panward-hold-until-verified is for non-streaming requests only; combined with stream: true it is refused with 400 unsupported_parameter.

A stream is labelled Sealed only once its receipt is final at a Sealed level, usually within a second of the end of the stream. Until then it is "Verification pending".

Errors during a stream#

An error before the first byte is an ordinary JSON error response with its HTTP status. After the first byte the status is already 200, so a failure is sent as an event: error frame holding the error object, and the stream ends without [DONE]. There is no retry after the first byte. Tokens already produced are billed and the receipt's level is failed.

A failure after the first byte: an error frame, and no [DONE]
data: {"id":"chatcmpl-01JC7Q5B1V","object":"chat.completion.chunk","created":1790000100,"model":"z-ai/glm-5.3","choices":[{"index":0,"delta":{"role":"assistant","content":"Hel"},"finish_reason":null}]}

event: error
data: {"error":{"type":"upstream_unavailable","message":"The response was interrupted. Tokens already produced are billed.","param":null,"request_id":"req_01JC7Q5B0Z7N3K8M2T4V6X9Y1A","doc_url":"https://panward.ai/docs/errors#upstream_unavailable"}}

The errors page lists the error types.

Stopping a stream#

Closing the connection stops delivery. You are billed for the tokens actually produced, including those of a stream you cut, and the receipt is still made final. Its response.emitted event records how many bytes were written and that the stream did not reach its end.

Was this page helpful?Suggest an edit