When I specify automatic agent dispatch in access ...
# ask-ai
f
When I specify automatic agent dispatch in access tokens grants, it doesn't work. Is it possible that there is some collision with the room creation ahead of it?
Copy code
await self.client.room.create_room(
                CreateRoomRequest(
                    name=input.room_name,
                    departure_timeout=5,
                    empty_timeout=15,
                    max_participants=3,
                    egress=input.egress_config,
                    metadata=json.dumps(input.metadata),
                )
            )

            at = AccessToken(self.api_key, self.api_secret)
            grants = VideoGrants(
                room=input.room_name,
                room_join=True,
                can_publish=True,
                can_publish_data=True,
                can_subscribe=True,
                can_update_own_metadata=True,
            )
            with_grants = at.with_grants(grants)
            with_identity = with_grants.with_identity(input.user_id)
            with_name = with_identity.with_name(input.user_name)
            access_token = with_name.to_jwt()
t
Thanks for reaching out, Anish. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I completely understand how room creation can impact automatic agent dispatch when it comes to access token grants, and I’m here to help clarify this for you. By default, LiveKit will automatically dispatch agents to new rooms as soon as they’re created, which is pretty convenient. However, if an agent is registered with an
agent_name
in its
WorkerOptions
, the automatic dispatch gets disabled, so you’ll need to trigger the dispatch explicitly. If you want to explicitly dispatch an agent during room creation, you can include a
RoomConfiguration
with
RoomAgentDispatch
in the participant’s access token. For instance, by setting
agents=[RoomAgentDispatch(agent_name="test-agent")]
in the
RoomConfiguration
, the agent named "test-agent" will be dispatched as soon as the participant joins and the room is created. Please don’t hesitate to reach out if you’d like me to walk you through this setup or if you have any other questions—I’m here to support you! Sources: livekit-api/tests/test_access_token.py | Dispatching agents | LiveKit Docs | Agent dispatch | LiveKit Docs | Authentication | LiveKit Docs | spec/livekit/access_token_spec.rb
f
If I do that like the following, it still doesn't work:
Copy code
await self.client.room.create_room(
                CreateRoomRequest(
                    name=input.room_name,
                    departure_timeout=5,
                    empty_timeout=15,
                    max_participants=3,
                    egress=input.egress_config,
                    metadata=json.dumps(input.metadata),
                )
            )

            at = AccessToken(self.api_key, self.api_secret)
            grants = VideoGrants(
                room=input.room_name,
                room_join=True,
                can_publish=True,
                can_publish_data=True,
                can_subscribe=True,
                can_update_own_metadata=True,
            )
            with_grants = at.with_grants(grants)
            with_identity = with_grants.with_identity(input.user_id)
            with_name = with_identity.with_name(input.user_name)
            with_room_config = with_name.with_room_config(
                RoomConfiguration(
                    agents=[RoomAgentDispatch(agent_name=input.agent_name)],
                )
            )
            access_token = with_room_config.to_jwt()
t
Thank you for your question! The
RoomAgentDispatch
in
RoomConfiguration
doesn’t actually enable automatic agent dispatch. Instead, it works by triggering a specific dispatch when a participant connects to the room. If your agent has an
agent_name
set in its
WorkerOptions
, automatic dispatch is intentionally turned off, so dispatching needs to be triggered explicitly. When you include
RoomAgentDispatch
in the access token, it means the specified agent will be dispatched right when the participant joins the room. Just to clarify, this doesn’t turn automatic dispatch back on; it simply allows for targeted, on-demand dispatch of that particular agent. If this doesn’t quite match what you were expecting, please don’t hesitate to reach out. I’m here to help you with your setup or explore other options that might work better for you. Sources: Agent dispatch | LiveKit Docs | Dispatching agents | LiveKit Docs | Agents telephony integration | LiveKit Docs | examples/agent-dispatch/index.ts | examples/agent_dispatch.py
f
thats pretty much automatic dispatch? having the agent join when the participant joins
t
You're absolutely right that having an agent join automatically when a participant connects can seem a lot like automatic dispatch. The important thing to keep in mind is that this happens because you've set it up explicitly through the access token using
RoomAgentDispatch
, rather than it being the default for all agents. True automatic dispatch, in the strictest sense, happens without any special configuration and only applies to agents who don’t have an
agent_name
set in their
WorkerOptions
. When you do set an
agent_name
, dispatch becomes more intentional and requires the kind of explicit setup you’re already using. If you’d like, I’m here to help clarify anything further or assist you in fine-tuning this setup. Just let me know!
f
OK but this doesn't work upon participant join
t
I'm here to help you get this sorted out! If the agent isn’t being dispatched when a participant joins, let’s try these steps together: 1. Make sure the agent is registered with an agent name: The agent should have an
agent_name
set in its
WorkerOptions
. Keep in mind, automatic dispatch is turned off when
agent_name
is set, but you can still use explicit dispatch. 2. Check that the participant’s token includes `RoomConfiguration`: It’s important the token contains
RoomConfiguration
with a
RoomAgentDispatch
entry that has the correct
agent_name
. 3. Verify the agent and dispatch names match: Double-check that the
agent_name
in the agent worker is the same as the one in the token’s
RoomAgentDispatch
. 4. Look into the agent dispatch state: You can use the
ListAgentDispatchRequest
to see the agent dispatch state and any jobs linked to it. 5. Take a peek at the logs and dashboard: Checking the LiveKit server logs and dashboard might reveal any errors related to agent dispatch. If you’d like, I’m happy to walk you through any of these steps or assist further. Just let me know! Sources: pkg/loadtester/agentloadtester.go | Dispatching agents | LiveKit Docs | Agent dispatch | LiveKit Docs | SIP troubleshooting guide | LiveKit Docs | protobufs/livekit_room.proto
f
Can the access token creation also create a room?
Copy code
token = (
        AccessToken()
        .with_identity("my_participant")
        .with_grants(VideoGrants(room_join=True, room=room_name))
        .with_room_config(
            RoomConfiguration(
                agents=[
                    RoomAgentDispatch(agent_name="test-agent", metadata='{"user_id": "12345"}')
                ],
            ),
        )
        .to_jwt()
    )
    return token
t
Thanks so much for your question! I want to clarify that creating an access token itself doesn’t immediately create a room. Instead, a room is automatically set up the very first time someone joins using that token—provided the room isn’t already there. When you generate the token, you have the option to specify the room name within the
VideoGrant
. You can also add a
RoomConfiguration
to customize settings like the maximum number of participants or agent dispatch. These settings will take effect when the room is created as the first participant joins. If any part of this feels unclear or if you’d like a hand walking through the steps, please don’t hesitate to reach out—I’m here to help! Sources: Docs::Home | packages/livekit-server-sdk/src/AccessToken.test.ts | cmd/lk/token.go | Authentication | LiveKit Docs | Docs::Home
Thanks for reaching out! I’d love to help clarify how access token creation works. While creating an access token doesn’t directly create a room, no worries—if the room specified in the token doesn’t exist yet, LiveKit will automatically create it as soon as the first participant joins using that token. Also, you have the option to include a
RoomConfiguration
within the token. This lets you set up important details like the maximum number of participants or agent dispatch settings, which will be applied when the room gets created during the participant’s join process. If you have any more questions or want me to walk you through this setup, just let me know! I’m here to help.
f
Give me a min repro example of local agent in console that is dispatched in this way with access token
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!
f
Is it possible to create an access token with room configuration for a room that hasn't been created yet? And then when the user connects using that access token the room will be created?
?