Streaming
Server-Sent Events for incremental responses.
Streaming
Set "stream": true in the request body. Streaming works in both inference formats and does not change pricing.
Anthropic emits events such as message_start, content_block_delta, and message_stop. OpenAI emits data: {...} chunks and finishes with data: [DONE].
from openai import OpenAI
client = OpenAI(api_key="mg_live_...", base_url="https://api.model-gate.com/v1")
stream = client.chat.completions.create(model="claude-opus-4.8", messages=[{"role":"user","content":"Hello"}], stream=True)
for chunk in stream:
text = chunk.choices[0].delta.content
if text:
print(text, end="", flush=True)
Keep the connection open and disable reverse-proxy buffering. Closing the client connection cancels upstream generation; already generated usage remains billable. See Errors for retry guidance.