Skip to main content
These hooks allow you to consume real-time streams from your tasks. Streams are useful for displaying AI/LLM outputs as they’re generated, or any other real-time data from your tasks.
To learn how to emit streams from your tasks, see our Realtime Streams documentation.
Available in SDK version 4.1.0 or later. This is the recommended way to consume streams in your React components.
The useRealtimeStream hook allows you to subscribe to a specific stream by its run ID and stream key. This hook is designed to work seamlessly with defined streams for full type safety.

Basic Usage

With Defined Streams

The recommended approach is to use defined streams for full type safety:

Streaming AI Responses

Here’s a complete example showing how to display streaming AI responses:

Options

The useRealtimeStream hook accepts the following options:

Using Default Stream

You can omit the stream key to use the default stream:
For more information on defining and using streams, see the Realtime Streams v2 documentation.

useRealtimeRunWithStreams

For new projects, we recommend using useRealtimeStream instead (available in SDK 4.1.0+). This hook is still supported for backward compatibility and use cases where you need to subscribe to both the run and all its streams at once.
The useRealtimeRunWithStreams hook allows you to subscribe to a run by its ID and also receive any streams that are emitted by the task. This is useful when you need to access both the run metadata and multiple streams simultaneously.
You can also provide the type of the streams to the useRealtimeRunWithStreams hook to get type-safety:
As you can see above, each stream is an array of the type you provided, keyed by the stream name. If instead of a pure text stream you have a stream of objects, you can provide the type of the object:

Streaming AI responses with useRealtimeRunWithStreams

Here’s an example showing how to display streaming OpenAI responses using useRealtimeRunWithStreams:

AI SDK with tools

When using the AI SDK with tools with useRealtimeRunWithStreams, you can access tool calls and results:

Throttling updates

The useRealtimeRunWithStreams hook accepts an experimental_throttleInMs option to throttle the updates from the server. This can be useful if you are getting too many updates and want to reduce the number of updates.
All other options (accessToken, baseURL, enabled, id) work the same as the other realtime hooks. For the newer useRealtimeStream hook, use the throttleInMs option instead (see options above).