Streaming
Set "stream": true to receive the reply as the model writes it. Your users see the first words in about a second, instead of waiting for the whole answer.
Python
python
from openai import OpenAI
client = OpenAI(base_url="https://api.continuumlabs.ai/v1", api_key="cl-your-key")
stream = client.chat.completions.create(
model="glm-5.2",
messages=[{"role": "user", "content": "Write a short welcome note for a new employee in Brisbane."}],
stream=True,
)
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)TypeScript
typescript
import OpenAI from 'openai'
const client = new OpenAI({ baseURL: 'https://api.continuumlabs.ai/v1', apiKey: 'cl-your-key' })
const stream = await client.chat.completions.create({
model: 'glm-5.2',
messages: [{ role: 'user', content: 'Write a short welcome note for a new employee in Brisbane.' }],
stream: true,
})
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? '')
}What arrives
The reply arrives as server-sent events. Each line starts with data: and holds a JSON chunk with the next piece of text in choices[0].delta.content. The stream ends with data: [DONE].
text
data: {"choices":[{"delta":{"content":"Welcome"}}],"model":"glm-5.2"}
data: {"choices":[{"delta":{"content":" to the team"}}],"model":"glm-5.2"}
data: {"choices":[],"usage":{"prompt_tokens":21,"completion_tokens":64}}
data: [DONE]Token usage
The last chunk before [DONE] carries usage, the tokens charged for the request. We always ask the model to include it, so you don't need to set stream_options yourself.
To stop a long reply early, close the connection. Set
max_tokens if you want a hard limit on the length of every reply.