Streaming
Receive responses incrementally and read final usage.
Read the response incrementally
With stream: true, responses use Server-Sent Events (SSE). SDKs parse events for you; when reading HTTP directly, process by event boundaries — never assume each network chunk is complete JSON.
from openai import OpenAI
client = OpenAI(
base_url="https://189.24.66.33/v1",
api_key="sk-relay-...",
)
stream = client.chat.completions.create(
model="glm-5.3-flash-xploded",
messages=[{"role": "user", "content": "Hello"}],
stream=True,
stream_options={"include_usage": True},
)
for chunk in stream:
if chunk.choices:
print(chunk.choices[0].delta.content or "", end="", flush=True)
if chunk.usage:
print("\nUsage:", chunk.usage)Usage and the final event
OpenAI streaming asks the upstream for usage. The final chunk containing usage may have no choices — check them separately. The Anthropic API uses its own message event format.
After a disconnect, settlement uses the usage actually received; token costs are never estimated when usage is missing. Check line-item results in usage records.