Streaming API for Grok
Updated:
Grok streaming lets an application display an answer as it is generated instead of waiting for the complete response. The Streaming API sends text and service events over Server-Sent Events (SSE).
If you need one complete response object, use the Messages API.
How streaming works
The HTTP connection stays open and sends ordered events until the response completes or the client cancels it.
"stream": trueTransport
SSE over one HTTP response; no WebSocket handshake is required.
Payload
Each event describes a lifecycle change or a small text delta.
Result
Append deltas for the UI, then use the completed event for final status and usage.
Start a streaming response
curl -N https://api.llm-gate.tech/v1/responses \
-H "Authorization: Bearer $GROK_API_DEV_KEY" \
-H "Content-Type: application/json" \
-H "Accept: text/event-stream" \
-d '{
"model": "grok-4.5",
"input": "Explain binary search in two sentences",
"stream": true
}'Event types
While listening to a stream, the client receives different event types. The current events and their purpose are listed below.
| Event | What to do |
|---|---|
| response.created | Store the response ID and mark the request as in progress. |
| response.output_item.added | A typed output item was added; it may be text, a tool call, or another item. |
| response.content_part.added | A new content part started inside an output message. |
| response.output_text.delta | Append delta to the visible answer in order. |
| response.output_text.done | The current text part has finished. |
| response.completed | Commit the final answer and read status and usage. |
| error | Stop rendering and surface a safe error state. |
Example event stream
response.created{"response":{"id":"resp_01abc","status":"in_progress"}}response.output_text.delta{"delta":"Binary"}response.output_text.delta{"delta":" search"}response.completed{"response":{"status":"completed","usage":{"input_tokens":18,"output_tokens":42,"total_tokens":60}}}Streaming code example
import json
import os
import httpx
with httpx.stream(
"POST",
"https://api.llm-gate.tech/v1/responses",
headers={
"Authorization": f"Bearer {os.environ['GROK_API_DEV_KEY']}",
"Accept": "text/event-stream",
},
json={
"model": "grok-4.5",
"input": "Explain binary search in two sentences",
"stream": True,
},
timeout=3600.0,
) as response:
response.raise_for_status()
for line in response.iter_lines():
if not line.startswith("data:"):
continue
data = line.removeprefix("data:").strip()
if not data or data == "[DONE]":
continue
event = json.loads(data)
if event.get("type") == "response.output_text.delta":
print(event["delta"], end="", flush=True)Error handling
HTTP error before SSE
If the response status is not 2xx, parse the normal JSON error body.
Error event after connection
The stream can stop after an event of type error. Keep the partial output, mark it incomplete, and log the response ID.
Connection closed early
If the connection closes before response.completed, treat the answer as incomplete. A retry creates a new answer, so do not automatically append it to the text already received.
429 or temporary 5xx
Use capped exponential backoff with jitter only when no useful stream was committed.