How to specify agent name in token and in the agen...
# ask-ai
n
How to specify agent name in token and in the agent class as well for explicit dispatch. I'm right now doing this and the agent doesn't get dispatched
Copy code
if __name__ == "__main__":
    cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint, prewarm_fnc=prewarm, agent_name='test_agent_name'))
Copy code
def _create_participant_token(
        self,
        participant_identity: str,
        participant_name: str,
        room_name: str,
        agent_name: str | None = None,
        metadata: dict[str, Any] | None = None,
    ) -> str:
        try:
            # Create access token with 15 minute TTL
            token = api.AccessToken(self.api_key, self.api_secret)
            token.with_identity(participant_identity)
            token.with_name(participant_name)
            token.with_ttl(timedelta(minutes=15))

            if metadata:
                metadata_json = json.dumps(metadata)
                token.with_metadata(metadata_json)

            # Set video grants
            grants = api.VideoGrants(
                room_join=True,
                room=room_name,
                can_publish=True,
                can_publish_data=True,
                can_subscribe=True,
                room_record=True,
            )
            token.with_grants(grants)

            if agent_name:
                room_config = api.RoomConfiguration(agents=[{"agent_name": agent_name}], departure_timeout=10*60)
                token.with_room_config(room_config)

            return token.to_jwt()

        except Exception as e:
            log.exception(f"Failed to create participant token: {e}")
            raise
This is my current logic
t
Thanks for reaching out, Hari. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Let's walk through how to specify the agent name in both the token and the agent class for explicit dispatch. It’s pretty straightforward, and I’m here to help you every step of the way! 1. Set the agent name in the token: You’ll want to use the
RoomConfiguration
and
RoomAgentDispatch
classes to include the agent name in the token. Here’s an example to guide you:
Copy code
token.with_room_config(api.RoomConfiguration(agents=[{"agent_name": "test_agent_name"}], departure_timeout=10*60))
1. Set the agent name in the agent class: In your agent worker, make sure to specify the
agent_name
within the
WorkerOptions
. Here’s how you can do it:
Copy code
WorkerOptions(entrypoint_fnc=entrypoint, prewarm_fnc=prewarm, agent_name='test_agent_name')
It’s really important that the
agent_name
matches exactly in both places — this will turn off automatic dispatch and enable explicit dispatch for you. If you find the agent still isn’t getting dispatched, double-check that the agent name is consistent and free of typos. And of course, if you have any questions or need a hand, just let me know—I’m here to help! Sources: livekit-api/livekit/api/agent_dispatch_service.py | SIP troubleshooting guide | LiveKit Docs | Web and mobile frontends | LiveKit Docs | packages/livekit-server-sdk/src/AgentDispatchClient.ts | livekit-agents/livekit/agents/worker.py