ApiX Docs
Docs / Streaming
·View as Markdown

Streaming

Set stream: true and read Server-Sent Events: data: {...} chunks and a final data: [DONE].

python
import json, os, requests

res = requests.post(
    os.environ["APIX_BASE"] + "/v1/chat/completions",
    headers={"Authorization": "Bearer " + os.environ["APIX_KEY"]},
    json={
        "model": "4/glm-5.3-flash",
        "stream": True,
        "stream_options": {"include_usage": True},
        "messages": [{"role": "user", "content": "Count to 20"}],
    },
    stream=True,
)
for line in res.iter_lines(decode_unicode=True):
    if not line or not line.startswith("data: "):
        continue
    payload = line[6:]
    if payload == "[DONE]":
        break
    chunk = json.loads(payload)
    print(chunk["choices"][0]["delta"].get("content", ""), end="")

Details#

  • /v1/messages streams Anthropic events; /v1/responses streams Responses events.
  • With stream_options.include_usage the last chat chunk carries usage.
  • The stream ends with a comment line : apix {...} with usage and spend. SSE clients ignore comments; parse it if you want the cost.
  • ApiX sends X-Accel-Buffering: no so nginx does not hold the chunks. If you run your own proxy, disable response buffering there too.