LiveKit
Join a LiveKit room as a participant and hold a conversation in it, using livekitRoom from voiceai-sdk/livekit.
LiveKit runs WebRTC rooms that browsers and phones join. Your server joins the same room as another participant with @livekit/rtc-node, and livekitRoom(room, { identity }) from voiceai-sdk/livekit makes that room the session's transport. The session hears one participant and speaks into the room.
Install
npm install voiceai-sdk @livekit/rtc-node livekit-server-sdkpnpm add voiceai-sdk @livekit/rtc-node livekit-server-sdkyarn add voiceai-sdk @livekit/rtc-node livekit-server-sdkbun add voiceai-sdk @livekit/rtc-node livekit-server-sdk@livekit/rtc-node joins rooms from Node. livekit-server-sdk mints the access token your server joins with. You also need a LiveKit server URL, API key and API secret, from LiveKit Cloud or your own LiveKit server.
Quickstart
This script joins the room support-42 as voice-agent and talks with the participant caller-7.
import { Room, RoomEvent } from "@livekit/rtc-node";
import { AccessToken } from "livekit-server-sdk";
import { createSession } from "voiceai-sdk";
import { livekitRoom } from "voiceai-sdk/livekit";
import { openai } from "voiceai-sdk/openai";
const roomName = "support-42";
const caller = "caller-7";
// Mint a token for the agent. The API secret stays on this server.
const token = new AccessToken(process.env.LIVEKIT_API_KEY, process.env.LIVEKIT_API_SECRET, { identity: "voice-agent" });
token.addGrant({ room: roomName, roomJoin: true, canPublish: true, canSubscribe: true });
const room = new Room();
await room.connect("wss://your-project.livekit.cloud", await token.toJwt());
await using session = await createSession(openai(), { sampleRate: 48000, encoding: "opus" });
session.connect(livekitRoom(room, { identity: caller }));
// Close the session when the caller leaves.
room.on(RoomEvent.ParticipantDisconnected, (participant) => {
if (participant.identity === caller) session.close();
});
await session.say`Hi, I'm here. How can I help?`;
await session.converse(async (text) => `You said: ${text}`);
await room.disconnect();When the caller leaves, the session closes and converse resolves. The script then leaves the room, and await using releases the provider connection if it is still open.
Choose who the session hears
A room can hold many participants. identity names the one the session listens to; audio from everyone else is ignored. The session speaks into the room as its own participant, under the identity in its token, so every participant subscribed to it hears the reply.
- Identities come from tokens. Your server sets them when it mints each participant's token. Give the caller a known identity, such as your user id, and pass the same string to
livekitRoom. - The agent needs to publish and subscribe. Without
canPublishthe session can't speak, and withoutcanSubscribeit can't hear. - Close what you're done with. One session per conversation. When the caller leaves, close it, then disconnect from the room.
Audio format
LiveKit carries voice as Opus at 48 kHz, so the session asks for sampleRate: 48000 and encoding: "opus". Whether a provider takes Opus depends on the provider; see Capabilities. If it can't, createSession throws VoiceError with code unsupported instead of quietly using another format.
Where keys stay
| Part | Runs on | Holds |
|---|---|---|
| Browser or phone app | The user's device | A token for its own identity, minted by your server |
| LiveKit | LiveKit Cloud or your own LiveKit server | No provider key. It routes audio between participants. |
| Your agent | Your server | The LiveKit API secret, the provider's key, and the session |
| The provider | The provider's API | Audio from your agent only |
Here the provider's key is OPENAI_API_KEY, read by openai() on your server. Participants in the room never see it, and LiveKit never sees it either.
Next steps
- Give your agent a voice: put a model behind the conversation.
- Interruptions and barge-in: what happens when the caller talks over the session.
- Latency and audio formats: sample rates and encodings for each transport.