How can I make the deployed agent join the room wh...
# ask-ai
b
How can I make the deployed agent join the room when the user joins
t
Thanks for reaching out, Sajal. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'd be happy to help you set up a deployed agent to join a room whenever a user connects! You can achieve this by configuring agent dispatch through the
RoomConfiguration
in the participant's access token. This way, the agent will automatically be dispatched to the room as soon as the participant joins. Here’s how you can do it step-by-step: 1. Include the agent's name in the
agents
list within the
RoomConfiguration
. 2. Attach this
RoomConfiguration
to the participant’s access token when you generate it. Here’s an example in Node.js to guide you:
Copy code
import { AccessToken } from 'livekit-server-sdk';
import { RoomAgentDispatch, RoomConfiguration } from '@livekit/protocol';

const at = new AccessToken('api-key', 'secret-key', {
  identity: 'my-participant',
});
at.addGrant({ roomJoin: true, room: 'my-room' });

at.roomConfig = new RoomConfiguration({
  agents: [
    new RoomAgentDispatch({
      agentName: 'my-agent',
      metadata: '{"user_id": "12345"}',
    }),
  ],
});

const token = await at.toJwt();
And here’s a Python example for you:
Copy code
from livekit.api import AccessToken, RoomAgentDispatch, RoomConfiguration, VideoGrants

token = (
    AccessToken()
    .with_identity("my_participant")
    .with_grants(VideoGrants(room_join=True, room="my-room"))
    .with_room_config(
        RoomConfiguration(
            agents=[
                RoomAgentDispatch(agent_name="test-agent", metadata='{"user_id": "12345"}')
            ],
        ),
    )
    .to_jwt()
)
Just a quick note: please make sure your agent is already deployed and registered as a worker with the LiveKit server. If you run into any questions or if you’re using a different SDK, feel free to reach out—I’m here to help! Sources: Authentication | LiveKit Docs | examples/agent-dispatch/index.ts | Agent dispatch | LiveKit Docs | examples/agent_dispatch.py | LiveKit Agents | LiveKit Docs