when i'm doing call , it seems like 2 agents are s...
# ask-ai
e
when i'm doing call , it seems like 2 agents are speaking , it's happening only with outbound call using sip egress server here's my code to make call: export async function initiateLiveKitOutboundCall(options: { phoneNumber: string; agentName: string; agentId?: string; sipOutboundTrunkId: string; roomPrefix?: string; metadata?: string; }) { const { phoneNumber, agentName, agentId, sipOutboundTrunkId, roomPrefix = 'room', metadata } = options; if (!sipOutboundTrunkId || !sipOutboundTrunkId.startsWith('ST_')) { throw new Error('SIP_OUTBOUND_TRUNK_ID is not set or invalid. Must start with "ST_"'); } if (!phoneNumber) { throw new Error('Phone number is required'); } const agentDispatchClient = new AgentDispatchClient( LIVEKIT_URL!, API_KEY, API_SECRET, ); const roomServiceClient = new RoomServiceClient( LIVEKIT_URL!, API_KEY, API_SECRET, ); const sipClient = new SipClient( LIVEKIT_URL!, API_KEY!, API_SECRET!, ); const roomName = `${roomPrefix}_${Math.floor(Math.random() * 10_000)}`; const participantIdentity = `caller_${phoneNumber.replace(/[^0-9]/g, '')}`; // Create room with 60 second empty timeout console.log(
Creating room: ${roomName}
); await roomServiceClient.createRoom({ name: roomName, emptyTimeout: 60 }); // Create agent dispatch console.log(
Creating dispatch for agent "${agentName}" in room ${roomName}
); const dispatch = await agentDispatchClient.createDispatch( roomName, agentName, { metadata: metadata || phoneNumber, }, ); console.log(
Created dispatch: ${dispatch.id}
); // Create SIP participant to initiate the call console.log(
Dialing ${phoneNumber} to room ${roomName}
); try { const sipParticipant = await sipClient.createSipParticipant( sipOutboundTrunkId, phoneNumber, roomName, { participantIdentity, participantAttributes: { ...(agentId && { agent: agentId }), } }, ); console.log(
Created SIP participant: ${sipParticipant.participantId}
); return { roomName, dispatchId: dispatch.id, participantId: sipParticipant.participantId, sipParticipant }; } catch (e) { console.error(
Error creating SIP participant: ${e instanceof Error ? e.message : e}
); throw e; } }
t
Thanks for reaching out, Aman. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — I’m looking at the outbound SIP flow you shared. You’re creating a room, explicitly dispatching an agent, then creating a SIP participant that joins the same room. That flow can cause two agent audio sources to be present if another agent instance or an automatic dispatch rule also joins the room. Let’s narrow this down and fix it. Quick checks to run now • Inspect the room after reproducing the issue: list participants and identities (confirm how many AGENT participants are present). • Confirm whether any SIP dispatch rules might auto-dispatch an agent for the room name/prefix you’re using. • Confirm how many agent workers are registered for that agent name (ensure only one). What to change or try • Ensure only one agent is dispatched to the room: ◦ If you rely on explicit dispatch in code, disable any SIP dispatch rules that could match your room names/prefixes. • Use clearly unique outbound room names so dispatch rules for inbound flows can’t match. Example:
outbound-call_<timestamp>_<rand>
. • Verify agent worker registration so you don’t have multiple workers (processes) registering the same agent name. • Prevent auto-speaking on agent join: ensure your agent logic waits for caller audio / user-started-speaking before generating replies. • Optionally, restrict the agent to process only the SIP caller by targeting a specific participant (RoomIO/participant filtering). If you want, run these debugging steps and share the outputs • Room participant list (room name) — show participant identities and roles. • Agent logs around the time of the call (look for multiple job/context starts or multiple generate_reply calls). • Whether any SIP dispatch rules match your room prefix (list active rules). Tell me which of the above you can check and share the room participant list or agent logs; I’ll help interpret them and give exact changes to your code/config to stop the duplicate agent audio. Sources: How to set a fixed participant for agent audio processing | SIP dispatch rule | LiveKit docs | Agents Framework | livekit/livekit_composite | DeepWiki | Agent dispatch | LiveKit docs | Agents telephony integration | LiveKit docs
e
can i skip the dispatch part
t
Yes — you can skip the explicit dispatch in your code, but an agent still needs to join the room some other way for the call to be handled. Two safe alternatives: • Use a SIP dispatch rule to automatically route the outbound call to an agent (remove your manual dispatch). This makes the SIP egress create/assign the agent when the SIP participant joins the room. • Have your agent worker directly join the room programmatically (no dispatch object). For example, start the agent session and have it join the generated roomName so it’s the single agent in the room. Notes and cautions • Do not do both automatic SIP dispatch rules and manual dispatching for the same room/prefix — that’s what causes multiple agents to appear. • If you skip dispatch, ensure your agent join logic waits for the SIP caller (or user-started-speaking) before speaking to avoid auto-speech collisions. • Use unique outbound room names (e.g.,
outbound-call_<timestamp>_<rand>
) so dispatch rules for inbound flows don’t accidentally match. If you’d like, tell me which approach you prefer (SIP dispatch rule vs. agent joining directly) and I’ll give exact code or config steps.
e
what is dispatch and creating sip participant?
t
Regarding your outbound call flow, here are clear definitions and how they interact: • Dispatch ◦ A dispatch is an instruction that assigns an agent (or agent job) to a specific room so the agent can handle that call. Creating a dispatch tells the system “bring this agent into room X.” ◦ Use cases: you create a dispatch when you want the system to start an agent session tied to the room you made. Don’t mix manual dispatches with automatic SIP dispatch rules that might also assign agents to the same room — that causes multiple agents to join. • Creating a SIP participant ◦ Creating a SIP participant tells the SIP egress to place an outbound call to the phone number and join the resulting SIP leg into a LiveKit room. The SIP participant represents the remote phone endpoint inside the LiveKit room. ◦ When you call
createSipParticipant(...)
, the SIP trunk dials the number; when answered, the SIP participant joins the room and audio/video tracks for that caller appear as a participant. How they interact in your flow • If you create a dispatch AND create a SIP participant for the same room, the SIP caller and the dispatched agent both join the room so they can communicate. • If an additional agent (another dispatch, a SIP dispatch rule, or another registered worker) also joins the same room, you’ll see multiple agent voices — which explains the duplicate speaking you observed. If you want, I can show: • Code to skip manual dispatch and have an agent join directly, or • How to use a SIP dispatch rule instead of manual dispatch. Which would you prefer?