Set stream: true and read Server-Sent Events: data: {...} chunks and a final data: [DONE].
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/messagesstreams Anthropic events;/v1/responsesstreams Responses events.- With
stream_options.include_usagethe last chat chunk carriesusage. - 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: noso nginx does not hold the chunks. If you run your own proxy, disable response buffering there too.