Realtime
Learn how to build realtime voice conversations with the AI SDK.
This guide covers legacy token-based, turn-based realtime conversations over WebSockets. These sessions run in the browser and connect directly to the provider using a short-lived token that you create on your server. You can also route the connection through AI Gateway.
For OpenAI Live's continuous JSON/PCM16 WSS relay runtime and application-handled
client delegation, see
experimental_useRealtime.
The typical flow is:
- The browser calls your setup endpoint.
- Your server creates a short-lived realtime token with
experimental_realtime.getToken(). - The browser opens a WebSocket connection to the provider or AI Gateway.
- The model streams audio, text, and tool calls back to the browser.
- Tool calls are handled by your application with
onToolCall.
For continuous OpenAI Live conversations, use an application-owned WebSocket
relay or optional WebRTC via api.session. Live uses client delegation: your
application handles delegated work and submits context rather than using the
turn-based tool loop below. See the
experimental_useRealtime reference
for SDP setup, server-owned permissions, capture ownership, and graceful close.
Setup Endpoint
Create a setup endpoint that returns a short-lived token for the realtime provider. This endpoint can also attach tool definitions to the session.
In production, authenticate and rate-limit your setup endpoint. It creates realtime sessions using your server-side API key.
AI Gateway
Use AI Gateway when you want the same realtime client code to work across supported upstream providers. The Gateway normalizes realtime events server-side, and the browser still receives only a short-lived client secret.
Create the short-lived Gateway realtime token from a server-side setup endpoint:
Then use the matching Gateway realtime model in the browser:
gateway.experimental_realtime.getToken() must run on your server because it
uses your Gateway credential to mint a vcst_ client secret. Creating the
realtime model with gateway.experimental_realtime() is safe in the browser.
Tool definitions work the same way with AI Gateway: convert AI SDK tools with
experimental_getRealtimeToolDefinitions() in your setup endpoint and return
the definitions alongside the token. The hook includes them in the session
update after the WebSocket opens.
Client Session
Use the experimental_useRealtime hook to connect to a realtime model, capture
microphone audio, play model audio, send text messages, and render messages.
Keep model and session configuration objects stable across renders. Use module
scope as above, or useMemo when configuration depends on props. Replacing either
object replaces the hook's session.
Tool Calling
Realtime tool execution is client-driven. The provider sends tool calls over the
WebSocket, and your application handles them with onToolCall. If the result is
available immediately, return it from onToolCall. The SDK sends it back to the
provider as tool output.
For server-backed tools, call an app-specific API endpoint from onToolCall.
Avoid generic "execute tool by name" routes. App-specific endpoints are easier
to secure because they can use your normal authentication, authorization,
validation, and rate limiting rules.
Server-Backed Tool Endpoint
Client Tool Handler
You can also submit tool output manually with addToolOutput when the tool
requires user interaction or another asynchronous process:
Supported Providers
Realtime models are available on providers that expose realtime WebSocket APIs:
You can also route realtime through the AI Gateway, which normalizes the session so the same client code works across upstream providers:
gateway.experimental_realtime.getToken() mints a short-lived Gateway client
secret on your server. The browser uses that token to open the Gateway
WebSocket; the SDK handles the Gateway-specific WebSocket subprotocols for you.
See the AI Gateway realtime docs
for Gateway-specific token and provider option details.