API
Streaming responses
Render output as it arrives and handle stream errors safely.
Enable streaming
Set stream to true. Ask for the final usage frame when your client supports it.
curl -N "$MAXLABS_BASE_URL/chat/completions" \
-H "Authorization: Bearer $MAXLABS_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "worker",
"messages": [{"role": "user", "content": "Write a release checklist."}],
"stream": true,
"stream_options": {"include_usage": true}
}'
The response uses Server-Sent Events. Each event begins with data: and contains a JSON object. The stream ends with this marker:
data: [DONE]
Join choices[0].delta.content values in order. Tool call arguments can also arrive across several frames, so concatenate them before parsing the final JSON arguments.
Handle errors inside the stream
A streaming connection can start with HTTP 200 and later emit an error object. Check every JSON frame for error; do not treat [DONE] alone as proof of success. If the connection closes early, record the request ID and apply the retry guidance.
Do not assume that cancelling the client stops account usage immediately. A request that has already started may continue long enough to settle its actual usage.
