# Grok Streaming API

Stream Grok responses with Server-Sent Events (SSE) to display the answer as it is generated.

For one complete response object, use the [Messages API](https://grok-api.dev/en/docs/messages).

## How streaming works

The HTTP connection stays open and sends ordered events until the response completes. Set `stream: true` in the request body.

```bash
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

The client receives lifecycle events while listening to the stream.

| event | handling |
| --- | --- |
| response.created | Save the response ID. |
| response.output_item.added | Handle a new typed output item. |
| response.content_part.added | Handle a new content part. |
| response.output_text.delta | Append the text delta in order. |
| response.output_text.done | Finish the current text part. |
| response.completed | Read the final response, status, and usage. |
| error | Stop rendering and handle the error. |

## JavaScript example

```javascript
const stream = await client.responses.create({
  model: "grok-4.5",
  input: "Explain binary search in two sentences.",
  stream: true,
});

for await (const event of stream) {
  if (event.type === "response.output_text.delta") {
    process.stdout.write(event.delta);
  }
  if (event.type === "response.completed") {
    console.log(event.response.usage);
  }
}
```

## Error handling

| case | handling |
| --- | --- |
| HTTP error before SSE | Parse the regular JSON error body when the status is not 2xx. |
| error event | Keep partial output, mark it incomplete, and save the response ID. |
| Early disconnect | Treat the answer as incomplete if response.completed was not received. |
| 429 or temporary 5xx | Use limited exponential backoff with jitter only when no useful result was committed. |
