Skip to content
Dashboard

Build realtime voice agents on AI Gateway

Link to headingGetting started

npm install ai @ai-sdk/react @ai-sdk/gateway

Link to headingRealtime voice agents

import { gateway } from '@ai-sdk/gateway';
export async function POST() {
const { token, url } = await gateway.experimental_realtime.getToken({
model: 'openai/gpt-realtime-2',
});
return Response.json({ token, url, tools: [] });
}

'use client';
import { experimental_useRealtime as useRealtime } from '@ai-sdk/react';
import { gateway } from '@ai-sdk/gateway';
import { useMemo } from 'react';
export default function Page() {
const model = useMemo(
() => gateway.experimental_realtime('openai/gpt-realtime-2'),
[],
);
const { status, connect, startAudioCapture } = useRealtime({
model,
api: { token: '/api/realtime/token' },
sessionConfig: { voice: 'alloy', turnDetection: { type: 'server-vad' } },
});
// Call connect(), then startAudioCapture(stream) with a microphone MediaStream.
}

Link to headingInside a realtime session

Link to headingText to speech

import { generateSpeech } from 'ai';
import { writeFile } from 'node:fs/promises';
const result = await generateSpeech({
model: 'xai/grok-tts',
text: 'Thanks for trying out AI Gateway.',
voice: 'eve',
outputFormat: 'mp3',
});
await writeFile('speech.mp3', result.audio.uint8Array);

Link to headingSpeech to text (transcription)

import { transcribe } from 'ai';
import { readFile } from 'node:fs/promises';
const result = await transcribe({
model: 'openai/whisper-1',
audio: await readFile('audio.mp3'),
});
console.log(result.text);

Link to headingPlayground

Link to headingRouting audio through AI Gateway

Link to headingMore information

Ready to deploy?