> ## Documentation Index
> Fetch the complete documentation index at: https://docs.reygrid.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Streaming

> Stream ReyGrid chat replies in TypeScript with streamChat and the SSE helpers.

`streamChat()` is an async generator that sends the chat request with `stream: true` and yields one `ChatDeltaEvent` per SSE frame.

```typescript theme={"system"}
for await (const frame of client.streamChat("{AgentId}", "What are your hours?", {
  userId: "user-42",
})) {
  process.stdout.write(frame.delta?.reply?.content ?? "");
}
```

## Signature

```typescript theme={"system"}
streamChat(
  agentId: string,
  message: string,
  opts?: Omit<ChatRequest, "message" | "stream">,
): AsyncGenerator<ChatDeltaEvent>
```

`opts` accepts `conversationId`, `userId`, `tools`, and `results`.

## `ChatDeltaEvent`

| Field | Type | Description |
| - | - | - |
| `delta.reply.content` | `string?` | Text fragment |
| `delta.finish_reason` | `string?` | Set on the final frame |
| `delta.tool_calls` | `MessageToolCall[]?` | Tool-call deltas |
| `success`, `conversationId`, `messageId` | optional | Present on some frames |
| `usage` | `{ credits: number }?` | Present on the final frame |

## Collect the full text

```typescript theme={"system"}
let text = "";
for await (const frame of client.streamChat("{AgentId}", "Hello!")) {
  text += frame.delta?.reply?.content ?? "";
}
```

## Callback style

`consumeStream(stream, onEvent, onDone?, onError?)` wraps a stream with callbacks and returns a function that cancels delivery.

```typescript theme={"system"}
import { consumeStream } from "reygrid";

const cancel = consumeStream(
  client.streamChat("{AgentId}", "Hello!"),
  (frame) => process.stdout.write(frame.delta?.reply?.content ?? ""),
  () => console.log("\ndone"),
  (err) => console.error(err),
);
```

## Errors

* HTTP errors before the stream starts throw the usual typed errors (for example `ReyGridAuthError`).
* Failures during the stream, including malformed frames, are thrown as `ReyGridStreamError`.
* Connection failures before the response throw `ReyGridNetworkError`.
* `timeoutMs` covers the whole stream.

See [Error handling](/integrations/typescript/error-handling).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.