experimental_useRealtime()
experimental_useRealtime is an experimental feature.
Creates a browser-side realtime session for bidirectional audio and text conversations with a realtime provider model.
The hook connects to a realtime WebSocket using a short-lived token from your
setup endpoint, returns messages as UIMessage[], and provides controls for
audio capture, playback, text input, and tool output.
import { openai } from '@ai-sdk/openai';import { experimental_useRealtime } from '@ai-sdk/react';
const realtime = experimental_useRealtime({ model: openai.experimental_realtime('gpt-realtime'), api: { token: '/api/realtime/setup', },});For AI Gateway, pass gateway.experimental_realtime(...) as the model and point
api.token at a server-side setup endpoint that calls
gateway.experimental_realtime.getToken().
Import
import { experimental_useRealtime } from "@ai-sdk/react"API Signature
Parameters
model:
api:
token:
sessionConfig?:
sampleRate?:
maxEvents?:
onToolCall?:
onEvent?:
onError?:
Returns
status:
messages:
events:
isCapturing:
isPlaying:
connect:
disconnect:
addToolOutput:
sendEvent:
sendTextMessage:
sendAudio:
commitAudio:
clearAudioBuffer:
requestResponse:
cancelResponse:
startAudioCapture:
stopAudioCapture:
stopPlayback:
Tool Calling
Realtime tool execution is client-driven. Use onToolCall to handle tool calls
and return the tool output:
const realtime = experimental_useRealtime({ model: openai.experimental_realtime('gpt-realtime'), api: { token: '/api/realtime/setup', }, onToolCall: async ({ toolCall }) => { if (toolCall.toolName === 'getWeather') { const response = await fetch('/api/weather', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(toolCall.args), });
return response.json(); } },});For tools that require user interaction, return undefined from onToolCall
and call addToolOutput later.
See Realtime for a complete example with server-backed app-specific tool endpoints.