Build an AI Voice Agent with SvelteKit + WebRTC + OpenAI Realtime (2026)
SvelteKit 2 + Svelte 5 runes give you reactive voice UI with 30% smaller bundles than React. Wire WebRTC ephemeral keys to OpenAI Realtime for browser-direct voice.
TL;DR — Svelte 5 runes (
$state,$derived) plus SvelteKit 2 form actions make WebRTC voice agents 30-40% smaller than the React equivalent. OpenAI ephemeral keys make browser-direct WebRTC safe to expose.
What you'll build
A SvelteKit page where a button mounts a WebRTC peer connection to OpenAI Realtime, the audio plays through a hidden <audio> element, and live transcripts stream into a runes-driven UI.
Prerequisites
@sveltejs/kit@^2.5,svelte@^5, Vite 5+.OPENAI_API_KEYserver-side.- Node 20+ or Bun 1.3.
Architecture
flowchart LR
UI[Svelte 5 page] -->|form action| SK[SvelteKit /api/key]
SK -->|POST /v1/realtime/sessions| OA1[OpenAI sessions]
OA1 --> SK --> UI
UI -- WebRTC SDP --> OA2[OpenAI Realtime]
Step 1 — Server: mint ephemeral key
```ts // src/routes/api/key/+server.ts import { OPENAI_API_KEY } from "$env/static/private"; import { json } from "@sveltejs/kit";
export async function POST() {
const r = await fetch("https://api.openai.com/v1/realtime/sessions", {
method: "POST",
headers: { Authorization: Bearer ${OPENAI_API_KEY},
"Content-Type": "application/json" },
body: JSON.stringify({ model: "gpt-realtime", voice: "verse" }),
});
return json(await r.json());
}
```
Hear it before you finish reading
Talk to a live CallSphere AI voice agent in your browser — 60 seconds, no signup.
Step 2 — Svelte 5 page with runes
```svelte
{transcript}
```
Step 3 — Add tool calls
Send tool definitions on session.update via dc.send(...). When the model emits response.function_call_arguments.done, run your tool and reply with conversation.item.create.
Step 4 — Deploy
@sveltejs/adapter-vercel deploys both the SSR routes and /api/key to Vercel functions.
Step 5 — Bundle size
The voice page weighs ~32kb gzipped (vs ~52kb for the React equivalent), thanks to runes' compiled-out reactivity.
Still reading? Stop comparing — try CallSphere live.
CallSphere ships complete AI voice agents per industry — 14 tools for healthcare, 10 agents for real estate, 4 specialists for salons. See how it actually handles a call before you book a demo.
Pitfalls
- Svelte 4
$:reactivity doesn't carry over to runes — pick one and stay consistent. bind:thistiming: Audio element is null inonMountif you reference it too early.- Ephemeral key TTL: Default 60s — mint right before
createOffer.
How CallSphere does this in production
CallSphere ships voice UIs across 6 verticals with 37 agents and 90+ tools powered by 115+ DB tables. While the public sites use Next.js, internal admin panels were prototyped in SvelteKit for the smaller bundle. Pricing $149/$499/$1,499, 14-day trial, 22% affiliate.
FAQ
Svelte 5 stable? Yes — GA October 2024, ~5M weekly downloads by 2026.
SvelteKit 2 + Vite 5? Vite 5+ is required; Vite 6 supported in 2.5+.
SSR for the voice page? Use +page.svelte with ssr = false — WebRTC needs the browser.
Tool streaming? Yes — Realtime data channel emits tool deltas just like JSON events.
Sources
- SvelteKit docs - https://kit.svelte.dev/
- Svelte 5 runes - https://svelte.dev/docs/svelte/what-are-runes
- OpenAI Realtime WebRTC - https://developers.openai.com/api/docs/guides/realtime-webrtc
- ForaSoft Voice Agents 2026 - https://www.forasoft.com/blog/article/openai-realtime-api-voice-agent-production-guide-2026
Try CallSphere AI Voice Agents
See how AI voice agents work for your industry. Live demo available -- no signup required.