This guide shows how to use ElevenLabs Speech Engine as the voice layer for a LiveKit room. A LiveKit Agents worker joins the room as a participant, subscribes to the user's audio track, opens a WebSocket to Speech Engine, and publishes Speech Engine's synthesized audio back to the room as its own track.
Speech Engine accepts two kinds of WebSocket connections:
The brain WebSocket that the ElevenLabs API connects to. Your server runs this with the Speech Engine SDK (engine.serve() / engine.attach()) and receives transcripts to respond to.
The conversation WebSocket that clients connect to. Browsers connect via a WebRTC token; non-browser clients (like a LiveKit Agents worker) connect via a signed URL and stream raw PCM audio in both directions.
The LiveKit worker uses the second connection. It acts as a "client" of Speech Engine on behalf of the participants in the LiveKit room.
sequenceDiagram
participant Browser
participant LK as LiveKit Room
participant Worker as Agents Worker
participant EL as ElevenLabs (conversation WS)
participant Brain as Brain Server
Browser->>LK: Join room (LiveKit token)
Worker->>LK: Join room (dispatched)
Worker->>EL: Open conversation WebSocket (signed URL)
loop Conversation
Browser->>LK: Microphone audio (Opus)
LK->>Worker: Decoded PCM frames
Worker->>EL: user_audio_chunk (base64 PCM)
EL->>Brain: user_transcript
Brain-->>EL: agent_response (streamed)
EL->>Worker: audio (base64 PCM)
Worker->>LK: Publish PCM frames
LK->>Browser: Audio (Opus)
end
The brain server is unchanged from the Speech Engine quickstart — the LiveKit worker replaces the browser as the audio source but the LLM logic stays the same.
Reach for the LiveKit bridge when the room itself is part of the experience:
Multi-participant sessions where users speak with the agent alongside each other
Existing LiveKit deployments where switching transports would break clients
Voice agents sharing a room with screen share, video, or text chat
SIP-to-LiveKit dispatched calls that need an AI agent on the line
If you only need a browser-to-Speech-Engine voice loop with no other participants, the WebRTC client in the Speech Engine quickstart is simpler — Speech Engine speaks WebRTC directly to the browser, no LiveKit room required.
LiveKit's AudioStream resamples incoming Opus tracks to whatever PCM sample rate you request, so you can match Speech Engine's input directly. Update the Speech Engine to accept 16 kHz PCM for ASR input and emit 24 kHz PCM for TTS output.
The worker is a long-running process that connects to your LiveKit server, waits for jobs, joins assigned rooms, and bridges audio between the room and Speech Engine.
The worker requests a short-lived signed URL for the Speech Engine conversation WebSocket. The signed URL embeds the engine ID and a one-time signature, so the worker can open the WebSocket without exposing your API key.
Each time the worker is dispatched to a room, its entrypoint runs. The entrypoint connects to the room, opens a Speech Engine conversation WebSocket, and starts two audio bridges: one for caller audio going to Speech Engine, and one for synthesized audio coming back.
bridge.py
bridge.py
import asyncio
import base64
import json
import os
import aiohttp
from dotenv import load_dotenv
from elevenlabs import AsyncElevenLabs
from livekit import agents, rtc
from livekit.agents import JobContext, WorkerOptions, cli
load_dotenv()
elevenlabs = AsyncElevenLabs(api_key=os.environ["ELEVENLABS_API_KEY"])
SPEECH_ENGINE_ID = os.environ["SPEECH_ENGINE_ID"]
USER_INPUT_RATE = 16000
AGENT_OUTPUT_RATE = 24000asyncdef signed_url() -> str:
response = await elevenlabs.conversational_ai.conversations.get_signed_url(
agent_id=SPEECH_ENGINE_ID,
)
return response.signed_url
asyncdef entrypoint(ctx: JobContext):
el_ws_ready: asyncio.Future[aiohttp.ClientWebSocketResponse] = (
asyncio.get_running_loop().create_future()
)
asyncdef pump_user_audio(track: rtc.Track):
el_ws = await el_ws_ready
stream = rtc.AudioStream(
track, sample_rate=USER_INPUT_RATE, num_channels=1,
)
asyncfor event in stream:
payload = base64.b64encode(bytes(event.frame.data)).decode()
await el_ws.send_str(json.dumps({"user_audio_chunk": payload}))
# Register the subscriber BEFORE ctx.connect() so we don't miss tracks# that get auto-subscribed during the connection handshake.
@ctx.room.on("track_subscribed")
def on_track_subscribed(track, publication, participant):
if track.kind != rtc.TrackKind.KIND_AUDIO:
returnif participant.identity == ctx.room.local_participant.identity:
return
asyncio.create_task(pump_user_audio(track))
await ctx.connect()
# Publish a track for the agent's synthesized audio.
source = rtc.AudioSource(sample_rate=AGENT_OUTPUT_RATE, num_channels=1)
track = rtc.LocalAudioTrack.create_audio_track("elevenlabs-agent", source)
await ctx.room.local_participant.publish_track(
track,
rtc.TrackPublishOptions(source=rtc.TrackSource.SOURCE_MICROPHONE),
)
# Open the Speech Engine conversation WebSocket.
http = aiohttp.ClientSession()
el_ws = await http.ws_connect(await signed_url())
await el_ws.send_str(json.dumps({"type": "conversation_initiation_client_data"}))
el_ws_ready.set_result(el_ws)
asyncdef el_to_room():
asyncfor msg in el_ws:
if msg.type != aiohttp.WSMsgType.TEXT:
continue
event = json.loads(msg.data)
etype = event.get("type")
if etype == "audio":
pcm = base64.b64decode(event["audio_event"]["audio_base_64"])
samples_per_channel = len(pcm) // 2
frame = rtc.AudioFrame(
pcm, AGENT_OUTPUT_RATE, 1, samples_per_channel,
)
await source.capture_frame(frame)
elif etype == "interruption":
source.clear_queue()
elif etype == "ping":
event_id = event.get("ping_event", {}).get("event_id")
await el_ws.send_str(json.dumps({
"type": "pong", "event_id": event_id,
}))
pump_task = asyncio.create_task(el_to_room())
asyncdef cleanup():
pump_task.cancel()
await el_ws.close()
await http.close()
ctx.add_shutdown_callback(cleanup)
if __name__ == "__main__":
cli.run_app(WorkerOptions(
entrypoint_fnc=entrypoint,
agent_name="elevenlabs-bridge",
))
The worker filters out its own published audio in the track_subscribed handler by comparing against the local participant's identity. Without this check, the worker would try to send its own synthesized audio back to Speech Engine.
Two ordering details matter for correctness:
Listener timing: TrackSubscribed is registered before ctx.connect(). LiveKit auto-subscribes to existing tracks during the connection handshake, and a listener registered afterwards may miss the event. The audio pump waits on a Future / Promise for the Speech Engine WebSocket so it can subscribe immediately and forward audio as soon as the connection is open.
TypeScript only — capture serialization: @livekit/rtc-node's AudioSource.captureFrame throws InvalidState if called concurrently. The TypeScript handler serializes captures with a promise chain. Python's single async for el_to_room loop is naturally sequential and does not need this.
Because the worker has an agent_name, it uses explicit dispatch — it only joins rooms when your backend tells it to. The simplest pattern is to include a RoomAgentDispatch in the LiveKit access token that the browser uses to connect.
When the button is clicked, the browser fetches a LiveKit token, joins the room with the microphone enabled, and starts receiving the agent's audio track. The worker is dispatched, opens its Speech Engine session, and bridges audio in both directions.
Speech Engine supports the following audio formats. Configure them on the engine via asr.user_input_audio_format and tts.agent_output_audio_format.
Format
.
Sample rate
.
Encoding
.
Notes
.
pcm_8000
.
8 kHz
.
Signed 16-bit LE PCM
.
ASR input only.
.
pcm_16000
.
16 kHz
.
Signed 16-bit LE PCM
.
Recommended for LiveKit user input.
.
pcm_22050
.
22.05 kHz
.
Signed 16-bit LE PCM
.
.
pcm_24000
.
24 kHz
.
Signed 16-bit LE PCM
.
Recommended for LiveKit agent output.
.
pcm_44100
.
44.1 kHz
.
Signed 16-bit LE PCM
.
TTS output requires Independent Publisher tier or above.
.
pcm_48000
.
48 kHz
.
Signed 16-bit LE PCM
.
ASR input only.
.
ulaw_8000
.
8 kHz
.
μ-law
.
Used by Twilio Media Streams.
.
AudioStream and AudioSource in LiveKit handle resampling for you — you can request any sample rate from AudioStream and the SDK converts from the underlying 48 kHz Opus track.
Explicit dispatch: Always set agent_name / agentName on WorkerOptions. Auto-dispatch fires the worker for every room created on your LiveKit project, which is rarely what you want.
Brain server authentication: Set a shared secret on the Speech Engine and verify it in your brain server, so only the Speech Engine can reach your endpoint:
Python
The brain server then checks request.headers["x-api-key"] before accepting the WebSocket upgrade.
Token server: Mint LiveKit and Speech Engine tokens server-side. Never expose LIVEKIT_API_SECRET or ELEVENLABS_API_KEY to the browser.
Event loop hygiene: Keep CPU-bound work off the worker's event loop. AudioSource.capture_frame and AudioStream iteration are time-sensitive; long synchronous calls will delay or drop interruption events. Use asyncio.to_thread() (Python) or worker_threads (Node) for blocking work.
Shutdown: Register ctx.add_shutdown_callback / ctx.addShutdownCallback to close the ElevenLabs WebSocket cleanly. By default, the room (and the job) is terminated when the last non-agent participant leaves.