In the room Agent is joining whenever the session ...
# ask-ai
c
In the room Agent is joining whenever the session starts can we ask explicitly agent to join when required and exit whenever not required, if yes how to do it?
t
Thanks for reaching out, abilash. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm happy to guide you through how you can explicitly control when an agent joins or leaves a LiveKit room. Here are some friendly steps to help you along: 1. Explicit Agent Dispatch via API: ◦ You can disable automatic dispatch by setting an
agent_name
in the worker configuration. ◦ Then, use the
AgentDispatchService
API to create a dispatch so the agent joins the room. For example:
Copy code
// Node.js
const agentDispatchClient = new AgentDispatchClient(process.env.LIVEKIT_URL);
const dispatch = await agentDispatchClient.createDispatch(roomName, agentName, {
  metadata: '{"mydata": "myvalue"}',
});
◦ If you want the agent to leave, simply delete the dispatch like this:
Copy code
// Node.js
await agentDispatchClient.deleteDispatch(dispatchId, roomName);
1. Dispatch Agent via Access Token: ◦ You can also embed the agent dispatch configuration directly into the participant’s access token. This way, the agent will join automatically when the participant connects. Here’s how:
Copy code
// Node.js
at.roomConfig = new RoomConfiguration({
  agents: [
    new RoomAgentDispatch({
      agentName: agentName,
      metadata: '{"mydata": "myvalue"}',
    }),
  ],
});
1. Agent Exit Behavior: ◦ Agents can exit manually by deleting the dispatch, or you can define custom behavior for when they disconnect using the
on_exit
method in the agent class. If you have any questions or need a hand with implementing these, please don’t hesitate to ask. I’m here to help! Sources: packages/livekit-server-sdk/src/AgentDispatchClient.ts | livekit-api/livekit/api/agent_dispatch_service.py | docs/index.yaml
c
give example in python
t
Here are some helpful examples in Python that show you how to explicitly control when an agent joins or leaves a LiveKit room: 1. Explicit Agent Dispatch via API: ◦ To create a dispatch for the agent to join a room:
Copy code
from livekit import api

lkapi = api.LiveKitAPI()
dispatch = await lkapi.agent_dispatch.create_dispatch(
    api.CreateAgentDispatchRequest(
        agent_name="agent_name_here",
        room="room_name_here",
        metadata="my_metadata"
    )
)
◦ To remove the agent from the room:
await lkapi.agent_dispatch.delete_dispatch(dispatch_id="dispatch_id_here", room_name="room_name_here")
1. Dispatch Agent via Access Token: ◦ To set up the agent to join automatically when a participant connects:
Copy code
from livekit import api

room_config = api.RoomConfiguration(
    agents=[
        api.RoomAgentDispatch(
            agent_name="agent_name_here",
            metadata="my_metadata"
        )
    ]
)
1. Agent Exit Behavior: ◦ To define custom behavior when the agent leaves:
Copy code
async def on_exit(self):
    await self.session.say("Goodbye!")
If you have any questions or would like me to walk you through these examples in more detail, please don’t hesitate to ask. I’m here to help!