---
title: "Deploy a Voice Agent on Cloudflare Workers | Micdrop"
description: "Run a Micdrop voice agent on Cloudflare Workers, with one Durable Object per call: no server to manage, the cloud AI providers, and the limits to plan for."
url: "https://micdrop.dev/docs/server/cloudflare-workers"
---

# Deploy a Voice Agent on Cloudflare Workers

`@micdrop/server` runs on [Cloudflare Workers](https://developers.cloudflare.com/workers/) as it runs in Node. A Worker takes the WebSocket of the browser and hands it to a [Durable Object](https://developers.cloudflare.com/durable-objects/), 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](https://github.com/Godefroy/micdrop/tree/main/examples/cloudflare) (`pnpm dev:cloudflare`) is the whole setup, runnable on your machine with no Cloudflare account. The [Cloudflare Workers guide](/blog/voice-agent-cloudflare-workers) walks through it, with the cost of a minute of call.

## Installation

```bash
npm install @micdrop/server @micdrop/openai
npm install -D wrangler
```

Add the packages of the AI providers you call. The ones that run on Workers are listed [below](#providers).

## 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:

`wrangler.jsonc`

```jsonc
{
  "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:

`src/worker.ts`

```typescript
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:

```typescript
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](/docs/server/auth-and-parameters) of a call work as in Node:

```typescript
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](/docs/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](/docs/client/vad) 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](/docs/server/semantic-turn-detection) load an ONNX model with `onnxruntime-node`, a native module Workers cannot run.

## Phone calls

The [telephony transports](/docs/transports/telephony) 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:

```typescript
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.

```bash
npx wrangler dev
npx wrangler secret put OPENAI_API_KEY
npx wrangler deploy
```
