AgentDispatchClient not routing to correct named a...
# ask-ai
j
AgentDispatchClient not routing to correct named agent - agents receiving wrong room types Hi LiveKit team, We're experiencing an issue where our PTT (push-to-talk) agent isn't receiving PTT rooms despite using AgentDispatchClient. Here's our setup: Current Implementation: 1. We have two agents running: - cora-voice-agent (continuous conversation mode) - cora-ptt-agent (push-to-talk mode) 2. Our token generator uses AgentDispatchClient: const agentName = mode === 'ptt' ? 'cora-ptt-agent' : 'cora-voice-agent'; const dispatchClient = new AgentDispatchClient( livekitUrl.replace('wss://', 'https://'), apiKey, apiSecret ); const dispatch = await dispatchClient.createDispatch( roomName, agentName, { metadata: JSON.stringify({ mode, threadId, patientId }) } ); Problem: - When creating a PTT room, the voice agent receives it instead of the PTT agent - The dispatch creation doesn't throw errors but seems to have no effect - Both agents are registered with their respective names in WorkerOptions Questions: 1. Is there a way to verify that AgentDispatchClient is actually creating the dispatch? Any logs we should check? 2. Does the agent need to be in a specific state (ready/registered) before dispatch will work? 3. As a workaround, we're considering adding mode filtering where agents check room metadata and call ctx.shutdown() if they receive the wrong room type. Will this cause LiveKit to retry with another agent, or will the room fail to connect? 4. Is there a better pattern for ensuring specific agents handle specific room types? We noticed the official PTT example doesn't use dispatch. Environment: - LiveKit Cloud - Python agents SDK - Agents deployed via Docker/ECS Thanks for any guidance!
t
Thanks for reaching out, Albert. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Here are answers to your questions and guidance for resolving the issue: 1. Verifying Dispatch Creation: Ensure that the agent is registered with the correct
agentName
in
WorkerOptions
. You can also check server logs for the
CreateDispatch
RPC call to confirm that the dispatch is being created. 2. Agent State Requirements: The agent must be registered with a specific
agentName
for dispatch to work. There is no explicit requirement for the agent to be in a specific state like "ready" beyond being registered. 3. Handling Incorrect Room Assignments: If an agent calls
ctx.shutdown()
after receiving the wrong room type, the room will disconnect, and the job will terminate. LiveKit does not automatically retry with another agent, so you would need to implement retry logic in your application. 4. Ensuring Specific Agents Handle Specific Room Types: Use explicit agent dispatch to assign agents to specific rooms. Assign unique
agentName
values to your agents and use the
createDispatch
method to target specific agents. Additionally, you can pass metadata during dispatch to provide context about the room type. Let me know if you need further assistance with implementing these solutions! Sources: packages/livekit-server-sdk/src/AgentDispatchClient.ts | livekit/livekit_agent_dispatch.pb.go | Agent dispatch | LiveKit Docs | Dispatching agents | LiveKit Docs | Dispatching agents | LiveKit Docs
j
AgentDispatchClient not working - wrong agent receiving rooms despite explicit dispatch Hi LiveKit team, Following up on PTT implementation - our RPC handlers are working correctly thanks to your previous help, but we're facing an agent routing issue. Setup: - Two agents deployed with explicit names: - agent.py with agent_name="cora-voice-agent" - agent-ptt.py with agent_name="cora-ptt-agent" - Both agents running successfully on ECS Token Generation with Dispatch: // Token generator bot const agentName = mode === 'ptt' ? 'cora-ptt-agent' : 'cora-voice-agent'; // Create token const token = new AccessToken(apiKey, apiSecret, { identity: patientId, metadata: JSON.stringify({ mode, threadId, patientId }) }); // Grant permissions token.addGrant({ room: roomName, roomJoin: true, ... }); // Create explicit dispatch const dispatchClient = new AgentDispatchClient( 'https://progress-notes-axnubc06.livekit .cloud', apiKey, apiSecret ); await dispatchClient.createDispatch( roomName, agentName, { metadata: JSON.stringify({ mode }) } ); Problem: When mode='ptt', the dispatch is created for 'cora-ptt-agent', but 'cora-voice-agent' receives and handles the room connection. Our logs show: - Token generator: "Creating dispatch for agent: cora-ptt-agent" - Voice agent: "=== PARTICIPANT CONNECTED ===" - PTT agent: (no logs - sitting idle) Specific Questions: 1. Is AgentDispatchClient working in LiveKit Cloud? We see no errors but dispatch seems ignored. 2. Do both agents need different ws_url or other config? Currently both connect to same LiveKit Cloud URL. 3. Is there a race condition? Does the agent need to be fully registered before dispatch works? 4. Default agent behavior? Is there a setting that makes one agent the "default" that receives all non-dispatched rooms? 5. Debugging dispatch? Any way to query active dispatches or see dispatch routing logs? Current Workaround Plan: Add room metadata filtering where agents check mode and call ctx.shutdown() if wrong type: # In voice agent if room_metadata.get('mode') == 'ptt': ctx.shutdown(reason="Wrong agent for PTT mode") return Will this cause LiveKit to retry with the next available agent? Thanks for your help! The PTT implementation itself works great - just need to route to the right agent.
t
Here are answers to your follow-up questions and some extra guidance to help you out: 1. Is AgentDispatchClient working in LiveKit Cloud? Yes, it definitely is! The AgentDispatchClient is fully supported in LiveKit Cloud for explicit dispatching. Just make sure each agent is registered with a unique
agentName
, since that’s key for explicit dispatch to work smoothly. 2. Do agents need different ws_url or other configurations? Nope, all agents can connect using the same LiveKit Cloud URL. There’s no need for different
ws_url
values between agents. 3. Is there a race condition or timing issue? Agents should be registered with their
agentName
before dispatching happens. LiveKit has safeguards to help avoid race conditions, but it’s best to ensure your agents are fully initialized and registered before creating dispatches. 4. Default agent behavior: If an agent starts without an
agentName
, it will automatically get all non-dispatched rooms. If you explicitly set an
agentName
, this automatic behavior is turned off. 5. Debugging dispatch routing: You can use the LiveKit CLI or SDK to check active dispatches. For example, running
lk dispatch list <room_name>
shows all active dispatches, or you can use the
listDispatch
method in the SDK to get dispatch details programmatically. About your workaround plan—if an agent calls
ctx.shutdown()
after getting the wrong room type, the room will disconnect and the job will end. LiveKit doesn’t automatically retry with another agent, so you’d want to add retry logic in your app to handle that. Feel free to reach out if you want to chat more or need any other help! Sources: examples/agent-dispatch/index.ts | Cloud Architecture | LiveKit Docs | delay-handling/raw_data.txt | pkg/service/agentservice.go | Dispatching agents | LiveKit Docs
j
Need help with agent dispatch - createDispatch seems to have no effect Hi LiveKit team, Thanks for the previous response. Based on your answer that ctx.shutdown() terminates the job without retry, we need help figuring out why our dispatch isn't working in the first place. Current Setup Verification: 1. Both agents ARE registered with explicit agent_name in WorkerOptions: # agent.py cli.run_app(WorkerOptions( entrypoint_fnc=entrypoint, agent_name="cora-voice-agent", # Explicitly set ... )) # agent-ptt.py cli.run_app(WorkerOptions( entrypoint_fnc=entrypoint, agent_name="cora-ptt-agent", # Explicitly set ... )) 2. Dispatch IS being created (no errors): const dispatch = await dispatchClient.createDispatch( roomName, "cora-ptt-agent", // Explicit agent name { metadata: JSON.stringify({ mode: "ptt" }) } ); console.log(
Dispatch created:
, dispatch); // This logs successfully 3. BUT: cora-voice-agent still receives all rooms (both PTT and voice) Critical Questions: 1. Agent Registration Timing: Our agents are deployed as separate ECS tasks. Could there be a timing issue where createDispatch is called before the PTT agent registers? How can we verify an agent is registered and ready? 2. Multiple Agents Same Process vs Separate: We run agents as separate processes/containers. Does dispatch only work if agents are in the same process with different names? 3. Default Agent Behavior: Is there a concept of a "default" agent? We notice cora-voice-agent always gets rooms even when we explicitly dispatch to cora-ptt-agent. 4. Debugging Tools: - How can we list all registered agents? - How can we verify a dispatch is active for a room? - Are there LiveKit Cloud logs we can access to see dispatch routing decisions? 5. Alternative Approach: Since ctx.shutdown() doesn't trigger retry, what's the recommended pattern for having multiple agents where each handles different room types? Should we: - Use a single agent with mode switching? - Implement a dispatcher agent that delegates? - Something else? We're on LiveKit Cloud (not self-hosted) if that affects dispatch behavior. Thank you for your help - we're stuck without being able to route to different agents!