is it possible to join a specivic agent name using...
# ask-ai
b
is it possible to join a specivic agent name using the react sdk
t
Thanks for reaching out, Aman. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Absolutely, you can join a specific agent by name using the React SDK in LiveKit! It works by setting up the agent dispatch on your server when you create the participant token. Then, when the React SDK connects to the room with that token, LiveKit takes care of dispatching the right agent to join the room with you. Here’s a simple way to make it happen: 1. Set up your agent with an
agent_name
in the server-side
WorkerOptions
so you can explicitly dispatch it. 2. Create a participant token using
RoomConfiguration.agents
to specify which agent to dispatch. 3. Connect to the room using the React SDK with that token, and the agent will join automatically. If you'd like, I can walk you through the backend token setup or help you integrate this into your React app—just let me know! Sources: TEMPLATE.md | templates.yaml | packages/livekit-server-sdk/src/AgentDispatchClient.ts | Agent dispatch | LiveKit Docs | React quickstart | LiveKit Docs
b
def generate_access_token(room_name: str, participant_name: str) -> str: now = int(time.time()) exp = now + 3600 # 1 hour payload = { "iss": LIVEKIT_API_KEY, "sub": participant_name, "aud": "livekit", "exp": exp, "nbf": now, "video": { "room": room_name, "roomJoin": True, "roomList": True, "roomRecord": True, "roomAdmin": True, "roomCreate": True, "canPublish": True, "canSubscribe": True, "canPublishData": True, "canUpdateOwnMetadata": True, "ingressAdmin": True, "hidden": False, "recorder": False } } return jwt.encode(payload, LIVEKIT_API_SECRET, algorithm="HS256") where to add the agent name
t
Hey there! To include the agent name in your token generation function, you just need to add a
RoomConfiguration
with
RoomAgentDispatch
to the payload. This way, the specified agent will be dispatched as soon as the participant connects to the room. Here’s a friendly tweak you can make to your function:
Copy code
def generate_access_token(room_name: str, participant_name: str, agent_name: str) -> str:
    now = int(time.time())
    exp = now + 3600  # 1 hour
    payload = {
        "iss": LIVEKIT_API_KEY,
        "sub": participant_name,
        "aud": "livekit",
        "exp": exp,
        "nbf": now,
        "video": {
            "room": room_name,
            "roomJoin": True,
            "roomList": True,
            "roomRecord": True,
            "roomAdmin": True,
            "roomCreate": True,
            "canPublish": True,
            "canSubscribe": True,
            "canPublishData": True,
            "canUpdateOwnMetadata": True,
            "ingressAdmin": True,
            "hidden": False,
            "recorder": False
        },
        "roomConfig": {
            "agents": [
                {
                    "agentName": agent_name,
                    "metadata": "{}"  # Optional metadata
                }
            ]
        }
    }
    return jwt.encode(payload, LIVEKIT_API_SECRET, algorithm="HS256")
So, in this updated version, you pass in the
agent_name
as an argument and add it under
roomConfig
inside
agents
. This makes sure the right agent gets dispatched to the room whenever the token is used. If anything’s unclear or you want a hand with anything else, just let me know—I’m here to help!