Skip to content
English

Streaming Output

Using streaming output on 1折AI: Anthropic's native SSE event sequence, OpenAI-compatible chunk format, and how to handle stream interruptions.

For long responses, use streaming output ("stream": true) so the first character appears faster and the request is less likely to be cut off by intermediate networks due to a single request taking too long. Both protocols are supported, with formats matching the official ones respectively.

Add "stream": true to the request; the response is Server-Sent Events. A normal stream follows this order (from Anthropic Streaming documentation ):

EventMeaning
message_startMessage starts, with model and initial usage
content_block_startA content block starts (text, thinking, tool_use)
content_block_deltaContent delta, text is in delta.text
content_block_stopThis content block ends
message_deltaMessage-level update, with stop_reason and final output token count
message_stopThe entire message ends

ping events may be interspersed in between and can be ignored; on error you will receive a error event.

bash
curl -N https://1zheai.com/v1/messages \
-H "x-api-key: $ONEZHE_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{"model":"claude-sonnet-5-5","max_tokens":128,"stream":true,
"messages":[{"role":"user","content":"从一数到五"}]}'

When using an SDK you don’t need to parse events yourself; see the streaming example in Anthropic SDK .

Add "stream": true to /v1/chat/completions; the response is chunks in the form of data: {...}, text is in choices[0].delta.content, and it ends with data: [DONE]. See OpenAI SDK for an SDK example.

The last chunk before usage contains [DONE], and choices is an empty array. It is also returned if stream_options.include_usage is not passed. Full stream tested on 2026-10-15:

text
data: {"id":"msg_011Cg3ERqM8nN58LxLt7TVuG","object":"chat.completion.chunk","created":1792031230,"model":"claude-sonnet-4-6","choices":[{"index":0,"delta":{"role":"assistant"},"finish_reason":null}]}
data: {"id":"msg_011Cg3ERqM8nN58LxLt7TVuG","object":"chat.completion.chunk","created":1792031230,"model":"claude-sonnet-4-6","choices":[{"index":0,"delta":{"content":"ok"},"finish_reason":null}]}
data: {"id":"msg_011Cg3ERqM8nN58LxLt7TVuG","object":"chat.completion.chunk","created":1792031230,"model":"claude-sonnet-4-6","choices":[{"index":0,"delta":{"content":""},"finish_reason":"stop"}]}
data: {"id":"msg_011Cg3ERqM8nN58LxLt7TVuG","object":"chat.completion.chunk","created":1792031230,"model":"claude-sonnet-4-6","choices":[],"usage":{"prompt_tokens":370,"completion_tokens":4,"total_tokens":374}}
data: [DONE]

When processing chunks, first check whether choices is empty; the example on the OpenAI SDK page above already does this.

  • First check if it’s a proxy: turn off the proxy and connect directly, then try again.
  • The client’s read timeout should be longer than the model’s generation time; increase the timeout setting for long outputs.
  • On receiving overloaded_error in a error event, wait a few seconds and retry; see Error Codes and Troubleshooting .

A request disconnected mid-stream is not entirely free; it is billed based on the output tokens generated before the disconnection, recorded in the call log. The billed output may be slightly more than what the client actually received. Tested on 2026-10-15: having the model count from 1 to 600, when the client disconnected at the 4th second it had received 188 lines (about 380 output tokens), while the call log recorded 750 output tokens. For long output you don’t need, rather than disconnecting mid-stream, it’s better to set max_tokens small from the start.