Deploy a Voice Agent on Cloudflare Workers
@micdrop/server runs on Cloudflare Workers
as it runs in Node. A Worker takes the WebSocket of the browser and hands it to
a Durable Object, one per
call, which runs MicdropServer with the cloud providers you choose. There is
no server to keep up, each call scales on its own, and the page of the call is
served by the same Worker.
The Cloudflare example
(pnpm dev:cloudflare) is the whole setup, runnable on your machine with no
Cloudflare account. The Cloudflare Workers guide
walks through it, with the cost of a minute of call.
Installation
npm install @micdrop/server @micdrop/openainpm install -D wranglerAdd the packages of the AI providers you call. The ones that run on Workers are listed below.
Configure the Worker
The server uses Buffer, node:stream and node:events, which Workers
provides with the nodejs_compat flag. Each call gets a Durable Object, which
the config declares:
{ "name": "voice-agent", "main": "src/worker.ts", "compatibility_date": "2026-11-01", "compatibility_flags": ["nodejs_compat"], "durable_objects": { "bindings": [{ "name": "VOICE_CALL", "class_name": "VoiceCall" }] }, "migrations": [{ "tag": "v1", "new_sqlite_classes": ["VoiceCall"] }]}Run each call in a Durable Object
MicdropServer takes the server half of a WebSocketPair as it takes a ws
socket in Node. The Worker routes each new call to a Durable Object of its own,
which holds the call from the first word to the hang up:
import { OpenaiAgent, OpenaiSTT, OpenaiTTS } from '@micdrop/openai'import { MicdropServer } from '@micdrop/server'import { DurableObject } from 'cloudflare:workers'
export default { async fetch(request, env) { if (request.headers.get('Upgrade') !== 'websocket') { return new Response('Expected a WebSocket', { status: 426 }) } // A new Durable Object for each call const call = env.VOICE_CALL.get(env.VOICE_CALL.newUniqueId()) return call.fetch(request) },} satisfies ExportedHandler<Env>
export class VoiceCall extends DurableObject<Env> { async fetch(): Promise<Response> { const [client, server] = Object.values(new WebSocketPair()) server.accept()
const apiKey = this.env.OPENAI_API_KEY new MicdropServer(server, { firstMessage: 'Hi! What can I do for you?', agent: new OpenaiAgent({ apiKey, systemPrompt: 'Be brief.' }), stt: new OpenaiSTT({ apiKey }), tts: new OpenaiTTS({ apiKey, voice: 'alloy' }), })
return new Response(null, { status: 101, webSocket: client }) }}The client does not change. It connects to the address of the Worker:
await Micdrop.start({ url: 'wss://voice-agent.example.workers.dev/call' })The connections to the providers go out from the Durable Object. Micdrop opens
them with a fetch and an Upgrade header, the way Workers sends the
authentication headers along, so the provider packages need nothing more.
Parameters and authentication
waitForParams and handleError take the socket of the pair too, so the
parameters and authentication of a call work as in
Node:
const [client, server] = Object.values(new WebSocketPair())server.accept()
const params = waitForParams(server, (value) => paramsSchema.parse(value))params .then((params) => new MicdropServer(server, createConfig(params))) .catch((error) => handleError(server, error))
return new Response(null, { status: 101, webSocket: client })The fetch handler of the Worker can also check a token in the query string or the headers before it creates the Durable Object, and refuse the upgrade with a 401.
Providers
Every provider below talks to its API over fetch and WebSockets, which Workers
provides.
| Package | Components |
|---|---|
@micdrop/openai | Agent, STT, TTS, Realtime, classifier |
@micdrop/elevenlabs | TTS |
@micdrop/cartesia | TTS |
@micdrop/gladia | STT |
@micdrop/mistral | Agent, STT, TTS |
@micdrop/gemini | Agent, STT, TTS, Live, classifier |
@micdrop/ai-sdk | Agent, with the providers of the AI SDK that run there |
The local models need a machine of their own and stay on Node: Whisper, Kokoro, Piper and the other packages that load a model or a native runtime.
Turns
By default the browser detects the turns of its user, so the
Durable Object never runs a VAD. When the server has to detect them,
turns: { vad: 'volume' } works on Workers, since VolumeVAD is plain
TypeScript. Silero and semantic turn detection
load an ONNX model with onnxruntime-node, a native module Workers cannot run.
Phone calls
The telephony transports take the socket of the pair
too. A Durable Object can answer the media stream of Twilio, Vonage, Telnyx or
Plivo, with turns: { vad: 'volume' } since a phone line carries no turns:
const transport = new TwilioTransport(server)new MicdropServer(transport, { turns: { vad: 'volume' }, agent, stt, tts })Limits
- Each incoming WebSocket message gives the Durable Object up to 30 seconds of
CPU, raised to 5 minutes with
limits.cpu_ms. A call spends most of its time waiting on the network, which costs no CPU. - Durable Objects exist on the free plan, where a Worker gets 10 ms of CPU per request. Plan the Workers paid plan for real calls.
- A Durable Object stays alive as long as its WebSocket is open, so a call can last as long as it needs.
- An isolate has 128 MB of memory, far more than a call with cloud providers uses.
- A call keeps timers and connections to its providers open, so its Durable Object does not hibernate while it lasts, and is billed for that time.
- A Worker waits on at most 6 connections at once while they open, WebSockets included. A call opens one or two per provider.
Run it locally
wrangler dev runs the Worker and its Durable Objects on your machine, with no
Cloudflare account. Keep the keys of the providers in .dev.vars locally, and
set them with wrangler secret put once deployed.
npx wrangler devnpx wrangler secret put OPENAI_API_KEYnpx wrangler deploy