is there a way i can force that no more than one a...
# ask-ai
a
is there a way i can force that no more than one agent joins a room? at the same time
t
Thanks for reaching out, Sebastián. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks for reaching out! To make sure I fully understand your needs, could you please let me know if you're trying to limit how many agents can be sent to a room, or if you'd prefer that only a specific agent can join? Also, are you currently using automatic agent dispatch, or are you assigning agents manually? I'm here to help you through this!
a
sorry, i meant i want to limit to only a single agent to join a room (only one participant kind = agent). im using manual dispatch via API
t
To help you restrict a room so that only one agent can join at a time using manual dispatch via API, here’s a simple step-by-step guide you can follow: 1. Register the Agent with a Specific Name: Start by disabling automatic dispatch. You do this by setting an
agentName
in the
WorkerOptions
. This way, the agent will only join when you explicitly dispatch them. 2. Manually Dispatch the Agent: Next, use the
AgentDispatchClient
to manually dispatch the agent to the room. This method makes sure only one instance of that agent joins the room. Here’s an example in Node.js:
Copy code
const agentDispatchClient = new AgentDispatchClient(
  process.env.LIVEKIT_URL,
  process.env.LIVEKIT_API_KEY,
  process.env.LIVEKIT_API_SECRET
);

const dispatch = await agentDispatchClient.createDispatch(
  "my-room",
  "my-agent",
  { metadata: '{"user_id": "12345"}' }
);
console.log("Agent dispatched:", dispatch);
3. Enforce Uniqueness: Before you dispatch, it’s a good idea to check if the agent is already dispatched by using
listDispatch(roomName)
. If you find an existing instance, you can skip or cancel the new dispatch to avoid duplicates:
Copy code
const dispatches = await agentDispatchClient.listDispatch("my-room");
const existing = dispatches.find(d => d.agentName === "my-agent");
if (!existing) {
  await agentDispatchClient.createDispatch("my-room", "my-agent");
}
This approach will help ensure that only one agent of the specified kind can join your room at any given time. If you have any questions or need more help, please don’t hesitate to ask—I’m here to support you! Sources: Dispatching agents | LiveKit Docs | packages/livekit-server-sdk/src/AgentDispatchClient.ts | examples/agent_dispatch.py | Agent dispatch | LiveKit Docs