Grok API

Streaming API for Grok

View as Markdown

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.

Enable streaming in the request body"stream": true
01

Transport

SSE over one HTTP response; no WebSocket handshake is required.

02

Payload

Each event describes a lifecycle change or a small text delta.

03

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.

EventWhat to do
response.createdStore the response ID and mark the request as in progress.
response.output_item.addedA typed output item was added; it may be text, a tool call, or another item.
response.content_part.addedA new content part started inside an output message.
response.output_text.deltaAppend delta to the visible answer in order.
response.output_text.doneThe current text part has finished.
response.completedCommit the final answer and read status and usage.
errorStop rendering and surface a safe error state.

Example event stream

1
response.created
{"response":{"id":"resp_01abc","status":"in_progress"}}
2
response.output_text.delta
{"delta":"Binary"}
3
response.output_text.delta
{"delta":" search"}
4
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.