TechFabricTechFabricHarness
Building Agents

Voice

Governed server-side voice sessions and the application work required before a phone pilot.

Use session.voice() for application voice. It wraps OpenAI Realtime or an STT/LLM/TTS pipeline with server-owned instructions, registered tools, session capability policy, authorization checks, redaction hooks, bounded execution and durable usage/tool journals. provider.connect() remains a low-level transport without session governance. Your application supplies caller verification; for phone calls, use the signed Twilio Media Streams bridge.

Governed server session

Create the agent/session with the authenticated tenant and actor and a shared atomic SessionStore. Use durable storage in production; the in-memory store is for tests. Resolve call and resource IDs from trusted server state so a browser can't choose another caller's case or forge a call binding.

const voice = await session.voice({
  provider,
  callId: verifiedCall.id,
  resourceId: verifiedCall.caseId,
  instructions: 'Assist with the verified case. Never request SSNs or bank credentials in speech.',
  tools: [lookupCase, requestSecureUpload],
  limits: { durationMs: 300_000, maxTurns: 30, maxToolCalls: 12 },
  costLimit: { perSession: 2, perCall: 0.25, perScope: 50, scopeKey: 'voice:2026-09-23' },
  authorize: operation => authorizeCall(verifiedCall, operation),
  prepareToolInput: (name, input) => validateAndRedactArguments(name, input),
  toolOperationKey: (name, input) => resolveLogicalActionId(verifiedCall, name, input),
  prepareToolOutput: (name, output) => modelSafeReceipt(name, output),
  redactTranscript: text => redactSensitiveText(text),
  onAudit: event => auditSink.write(event),
  signal: requestSignal,
});
try {
  for await (const event of voice.events()) {
    if (event.type === 'audio_delta') await transport.play(event.audio);
    if (event.type === 'speech_started') { await transport.clearPlayback(); await voice.cancelResponse({ playedMs: transport.playedMs(), interruption: event }); }
    if (event.type === 'transcript') await transport.caption(event.text);
    if (event.type === 'error') break;
  }
} finally {
  await voice.close();
}

You'll supply these callbacks, including identity checks and PII detection. Input preparation must validate the selected tool's schema and remove sensitive values before policy, approval or history sees them. Output preparation runs after tool envelopes are unwrapped and before history/model exposure. Use opaque references and safe receipts. Mutating tools must call governed Platform actions and use context.toolCallId as the idempotency key. Don't use dynamic state writes or durable substeps from voice tools. toolOperationKey must resolve a trusted logical action identity that stays stable across provider reconnects. Harness hashes it with the call binding and tool name. Provider-generated call IDs change on reconnect and must not be used for this callback. A genuinely new action needs a distinct key; reusing a key with changed arguments fails closed. Keys must be nonempty and at most 512 characters.

The governor doesn't emit tool calls to clients and rejects client tool results; when the provider requests an action, it rechecks authority before execution, fences replaced connections, and retains only the application's safe completed result so a reconnect can replay that receipt without invoking the external action a second time.

Audio-frame authority and ownership checks are coalesced and cached for at most one second to avoid full durable-store reads for every 20 ms frame. Tools and text inputs always check immediately. Partial provider transcript chunks are discarded without durable-store reads. For instant disconnect on revocation, abort the trusted call signal. The bridge bounds queued input to 128 messages or 1 MiB and closes on overload. Sessions detach from the signal when they close, so a long-lived shutdown or revocation signal can be shared across calls without retaining finished sessions. A started tool without a committed result requires application reconciliation; it isn't automatically retried, and creating a new call ID would bypass the original receipt instead of resolving it.

Only complete, application-redacted transcripts are forwarded; partial text is suppressed because PII can cross chunk boundaries. Harness doesn't persist raw audio. Providers still receive audio under their own data-handling terms. Secure sensitive-data handoff must bypass speech.

Providers and runtimes

Every built-in provider opens its WebSocket through a VoiceSocketFactory. On Node, the default factory lazily loads the optional ws peer (pnpm add ws) to send authorization headers. On Cloudflare Workers and Durable Objects, pass socketFactory: createWorkersVoiceSocketFactory() from @fabric-harness/cloudflare/voice; it upgrades through fetch() and needs no ws. Loading the core SDK doesn't connect or require credentials, and governed voice uses WebCrypto, so it runs on any runtime with globalThis.crypto. Never put provider keys in a browser.

Both factories refuse to follow redirects, because the handshake headers carry the provider key. A provider that answers with a 3xx fails the connection as VoiceConnectionError (code: 'VOICE_CONNECT_REJECTED', the HTTP status, no URL or headers), so a redirect can never send your key to another origin or over plaintext.

Outbound audio to the provider is paced. sendAudio() (and STT send()) waits while the provider socket is backed up, admitting sends in call order. At most 256 sends or one burst (10 s) of audio may wait at once; a send beyond that bound rejects immediately with VoiceBackpressureError (code: 'VOICE_BACKPRESSURE'). A waiting send that isn't admitted within 2 s of the call rejects the same way. Either rejection closes the session.

import { OpenAIRealtimeVoiceProvider } from '@fabric-harness/sdk';
const provider = new OpenAIRealtimeVoiceProvider({
  apiKey: process.env.OPENAI_API_KEY!,
  model: 'gpt-realtime',
});

The adapter uses the GA audio/session schema and response.output_audio.* events without the retired beta header. PCM16 is 24 kHz mono; G.711 μ-law/A-law uses 8 kHz. Your transport must match the codec; phone calls use audioFormat: 'g711_ulaw' with sampleRate: 8000.

cancelResponse({ playedMs }) accepts the milliseconds of assistant audio the caller actually heard. OpenAI Realtime truncates the playing assistant item to that point so its context matches playback; providers without server-side audio history ignore it. Governed voice validates the value and fails closed on a negative or non-integer position.

Pass the speech_started event you are reacting to as interruption. OpenAI Realtime and PipelineVoiceProvider record, when they receive that event, which reply was playing; the cancel then targets only that reply. Without it, the cancel targets the reply playing when cancelResponse() runs, which may already be a newer reply if your handling of the interruption was delayed (for example behind transcript redaction). If the interrupted reply already finished, the cancel is a no-op and the newer reply is untouched. An event the session did not yield is ignored and the call falls back to the reply playing now.

speech_stopped (provider VAD end of caller speech) is opt-in, so existing exhaustive switches over VoiceEvent keep compiling and never see it. Pass emitSpeechStopped: true to session.voice() or provider.connect() to receive it, and iterate them with voiceSessionEvents(session), which types them as VoiceSessionEvent. The Twilio bridge's voiceOptions enable it for reply first-audio latency metrics. See OpenAI WebSockets.

For pipeline mode, compose PipelineVoiceProvider with Deepgram or Cartesia STT, a text model, and ElevenLabs or Cartesia TTS; pass it to the same session.voice() interface. See Voice Providers for the formats and sample rates each provider supports; an unsupported combination throws instead of falling back to another rate.

For budgeted pipeline barge-in, configure PipelineVoiceProvider({ interruptedLlmCostUsd, ... }) with a conservative per-request estimate for your model/context limits. When cancellation prevents the LLM from reporting usage, this explicit estimate is charged so the conversation can continue; without it, unknown usage stops the call for reconciliation. TTS estimates count submitted characters, including interrupted synthesis, rather than implying that unplayed audio was free. Reconcile these estimates with provider billing. OpenAI Realtime rejects sample rates that don't match its codec.

Cost and recovery

Every session requires a perSession ceiling, while perCall limits each individual model response. Optional perScope and scopeKey aggregate voice spend across sessions within a tenant using atomic idempotent ledgers in the session store. These are separate from text-loop cost stores. Reconnect repairs journal-to-ledger updates and retains the original call deadline and turn/tool counts.

These estimated post-response limits can overshoot while a response is in flight, especially when several calls share a budget. Use provider billing caps and application admission/concurrency limits for financial hard stops. Telephony, recording, storage and verification costs aren't included.

Realtime text/audio counts are disjoint, including modality-specific cached input. Pipeline usage charges each STT delta once, the LLM response and that response's TTS characters. Register negotiated rates with registerModelPrices. Unknown costs stop a governed session. Interrupted input without a billed response blocks same-call reconnect until application reconciliation. Each billed response settles only the oldest input admitted before that response began (each sendText, or one run of audio frames). A response begins at its first provider event (audio, text, transcript or tool call), which ends the current run of audio, so input admitted while an earlier reply was still in flight stays outstanding even when unrelated provider events arrive before that reply's response_done; audio admitted after the provider's previous event is also treated as outstanding. Disconnects cannot guarantee final provider billing, so you'll need reconciliation against provider usage/invoices.

Audit metadata and safe tool receipts are durable. onAudit delivery is awaited and failures stop processing; external audit delivery isn't exactly-once. Apply tenant access and retention controls to voice: and voice-cost: records. Store loss removes replay/budget protection. Connections don't survive process restart. Open voice in a server, activity or Durable Object, never deterministic workflow code. See Phone calls for the per-call Durable Object host and its trade-offs.

Node endpoint and migration

WS /sessions/:id/voice is disabled unless startDevServer({ connectVoice }) is configured. The callback receives authenticated principal, tenantId, sessionId and signal; resolve the authorized application binding and return session.voice(...). The route requires voice:invoke and an existing tenant-owned session. session:read alone is insufficient.

const handle = connectFabricVoice({
  url: `wss://app.example.com/sessions/${sessionId}/voice`,
  authToken: shortLivedVoiceToken,
  tenantId,
  onEvent(event) {
    if (event.type === 'audio') speaker.write(event.audio);
    if (event.type === 'speech_started') speaker.clear();
  },
});
mic.on('frame', frame => handle.sendAudio(frame));

Move model, voice, codec and instructions into the trusted server callback. Browser configuration fields are deprecated and ignored; query parameters cannot configure the provider. Remove client submitToolResult() calls. Install ws, grant voice:invoke and configure storage, limits, identity, validators and redactors. Use short-lived query tokens with log redaction, or authenticated same-origin cookies. For phone calls, use the governed Twilio Media Streams bridge rather than a hand-written or scaffolded bridge.

Custom implementations of the public FabricSession interface must add voice(options), returning a governed session or rejecting with an unsupported-runtime error. Existing text-agent authoring and the finite/persistent builders are unchanged.

AutoRefi readiness

AutoRefi can build against the governed interface. Harness now provides signed Twilio ingress, a call-bound stream token, μ-law codec enforcement, clear-first barge-in and latency metrics through the Twilio bridge, hosted on a Cloudflare Durable Object or Node. Before a phone pilot the application still needs:

  • OTP identity, caller-to-case authorization, consent and recording disclosure.
  • Transcript redaction, secure sensitive-data handoff and retention rules.
  • Accessible captions/alternative channels, human transfer/takeover and outage recovery.
  • Idempotent Platform tools and reconciliation of uncertain effects/costs.
  • A credentialed staging canary covering barge-in, disconnect, denied access and exhausted budgets.

Start with the credential-free smokes in examples/voice-data-collector (governed session) and examples/voice-twilio-cloudflare (Twilio bridge and Durable Object host). You'll still need a real-phone staging canary to measure barge-in stop and first-audio latency before certifying a production rollout.