if i user API Agent dispatch, is there a way so i ...
# ask-ai
a
if i user API Agent dispatch, is there a way so i don't mistakenly dispatch twice an agent to a room? like prevent an agent joining twice or having a request sent twice?
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.
Hey there! To make sure the same agent doesn’t get sent twice to a room when using API Agent dispatch, a handy way is to use explicit agent dispatch. You do this by giving your agent a unique
agent_name
when you register them via
WorkerOptions
. This turns off automatic dispatch, so the agent only joins rooms when you tell them to. Also, before you create a new dispatch, you can check if that agent is already dispatched using the
listDispatch
method in the AgentDispatch service. Here’s a quick example:
Copy code
const dispatches = await agentDispatchClient.listDispatch(roomName);
const existing = dispatches.find(d => d.agentName === agentName);
if (!existing) {
  await agentDispatchClient.createDispatch(roomName, agentName);
} else {
  console.log("Agent is already dispatched to this room");
}
This little check helps keep things neat by preventing duplicate agents from showing up in the same room. Hope this helps! Sources: src/test/kotlin/io/livekit/server/AgentDispatchServiceClientTest.kt | pkg/rtc/room.go | Dispatching agents | LiveKit Docs | Agent dispatch | LiveKit Docs | Agents telephony integration | LiveKit Docs
a
but if the agent joined and later left the room, then it would need to join again and i wouldn't be able to recognize that from agent dispatch right?
t
Hey there! If an agent leaves a room and needs to jump back in, you can easily handle this by re-dispatching the agent using the API. Here’s a simple way to manage it: 1. Cleanly Disconnect the Agent: When the agent leaves, call
ctx.shutdown()
to make sure everything tidies up nicely and no stale states hang around. 2. Check Existing Dispatches: Use the
listDispatch
method to see if the agent is already dispatched to the room. If they’re not, then it’s time to re-dispatch. 3. Re-Dispatch the Agent: Create a new dispatch request with
createDispatch
. Here’s a quick example:
Copy code
const dispatches = await agentDispatchClient.listDispatch(roomName);\nconst existing = dispatches.find(d => d.agentName === agentName);\nif (!existing) {\n  await agentDispatchClient.createDispatch(roomName, agentName);\n} else {\n  console.log("Agent is already dispatched to this room");\n}
Also, if you want to make things even smoother, you can embed the agent dispatch right into the participant’s access token. That way, the agent automatically rejoins whenever the participant reconnects. If you have any questions or want a hand setting this up, just let me know—I’m here to help! Sources: Inside a session | LiveKit Docs | Job lifecycle | LiveKit Docs | Recording agent sessions | LiveKit Docs | packages/livekit-server-sdk/src/AgentDispatchClient.ts | pkg/rtc/room.go