# Streaming Source: https://docs.getworg.com/streaming With "stream": true the answer arrives as server-sent events while the model writes it. ## Request a stream ```bash curl -N https://api.getworg.com/v1/chat/completions \ -H "Authorization: Bearer $WORG_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "deepseek-v4.1-flash:cached", "messages": [{"role": "user", "content": "Count from 1 to 3."}], "max_tokens": 512, "stream": true, "stream_options": {"include_usage": true} }' ``` ```python import os from openai import OpenAI client = OpenAI( base_url="https://api.getworg.com/v1", api_key=os.environ["WORG_API_KEY"], ) stream = client.chat.completions.create( model="deepseek-v4.1-flash:cached", messages=[{"role": "user", "content": "Count from 1 to 3."}], max_tokens=512, stream=True, ) for chunk in stream: if chunk.choices and chunk.choices[0].delta.content: print(chunk.choices[0].delta.content, end="", flush=True) print() ``` ## Format Stream (ids shortened): ``` : keepalive data: {"id":"chatcmpl-req_Kzzo...","object":"chat.completion.chunk","created":1793729649,"model":"deepseek-v4.1-flash:cached","choices":[{"index":0,"delta":{"role":"assistant","content":""},"logprobs":null,"finish_reason":null}]} data: {"id":"chatcmpl-req_Kzzo...","object":"chat.completion.chunk","created":1793729649,"model":"deepseek-v4.1-flash:cached","choices":[{"index":0,"delta":{"content":"1"},"logprobs":null,"finish_reason":null}]} data: {"id":"chatcmpl-req_Kzzo...","object":"chat.completion.chunk","created":1793729649,"model":"deepseek-v4.1-flash:cached","choices":[{"index":0,"delta":{"content":","},"logprobs":null,"finish_reason":null}]} data: {"id":"chatcmpl-req_Kzzo...","object":"chat.completion.chunk","created":1793729649,"model":"deepseek-v4.1-flash:cached","choices":[{"index":0,"delta":{"content":" 2, 3."},"logprobs":null,"finish_reason":null}]} data: {"id":"chatcmpl-req_Kzzo...","object":"chat.completion.chunk","created":1793729649,"model":"deepseek-v4.1-flash:cached","choices":[{"index":0,"delta":{},"logprobs":null,"finish_reason":"stop"}]} data: {"id":"chatcmpl-req_Kzzo...","object":"chat.completion.chunk","created":1793729649,"model":"deepseek-v4.1-flash:cached","choices":[],"usage":{"prompt_tokens":12,"completion_tokens":9,"total_tokens":21,"prompt_tokens_details":{"cached_tokens":0}}} data: [DONE] ``` - Each event is one `data:` line with a JSON chunk, then a blank line. The stream ends with `data: [DONE]`. - Text arrives in `delta.content`, reasoning in `delta.reasoning_content`. The chunk with `finish_reason` has an empty `delta`. - Lines that start with a colon, such as `: keepalive`, are comments. SSE clients and the OpenAI SDKs skip them. - Some chunks carry `"usage": null`. Read usage only from the usage chunk below. ## The usage chunk With `"stream_options": {"include_usage": true}`, the last chunk before `[DONE]` has an empty `choices` array and the `usage` of the whole request. Without it there is no usage chunk. Worg bills the same either way. ## Tool calls in a stream A function call arrives in pieces in `delta.tool_calls`. The first piece for each call has its `id` and function `name`; later pieces add to `function.arguments`. Join the pieces by `index`, and parse the arguments after the chunk with `finish_reason: "tool_calls"`. Some text in `delta.content` can come before the call. ## Errors - Until the first chunk, nothing is sent: an error before then is a normal HTTP error with a JSON body, as on [Errors and retries](https://docs.getworg.com/errors). A combo can still move to its next lane. - After the first chunk the status is already 200. If the model stops with an error, a chunk carries an `error` object with code `upstream_error`, and the request stays on that lane. - A stream that ends without `data: [DONE]` was cut. Retry the whole request. ## When your client disconnects The model keeps writing after your client leaves. Worg keeps reading the stream for up to 2 minutes after you disconnect and bills the tokens the model wrote. In one test a client that left after 2 seconds was billed for all 458 output tokens. Set `max_tokens` to the most you want to pay for. Closing the connection does not stop the cost. Next: [Tool calling](https://docs.getworg.com/tool-calling) · [Reasoning](https://docs.getworg.com/reasoning) · [Errors and retries](https://docs.getworg.com/errors)