@app.post("/get-token") async def get_token(reques...
# ask-ai
s
@app.post("/get-token") async def get_token(request: GetTokenRequest): """ Generate LiveKit token for user with specific agent dispatch. This endpoint generates a token that: 1. Allows the user to join a room 2. Automatically dispatches a specific agent to that room """ print("Generating token...") print(f"Agent ID: {request.agent_id}, Room Name: {request.room_name}, Identity: {request.identity}") token = ( api.AccessToken(settings.livekit_api_key, settings.livekit_api_secret) .with_identity(request.identity) .with_grants(api.VideoGrants(room_join=True, room=request.room_name)) .with_room_config( api.RoomConfiguration( agents=[api.RoomAgentDispatch(agent_name=request.agent_id)] ) ) ) logger.info(f"key: {settings.livekit_api_key}, secret: {settings.livekit_api_secret}") jwt_token = token.to_jwt() logger.info(f"Generated token for agent {request.agent_id} and identity {request.identity}") return {"token": jwt_token} im using this ut it is only dispatching me the first agnet it had
t
Thanks for reaching out, ABHIRAM. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
It sounds like you're encountering an issue where only the first agent is being dispatched, and I totally understand how that can be a bit frustrating. This usually happens because of how the agent dispatch is set up in your code. To make sure a specific agent is dispatched to a room, you’ll want to use the
AgentDispatchService
through LiveKit’s server-side SDKs. This gives you more precise control over which agent gets dispatched. Here’s a quick rundown of the steps to help guide you: • First, make sure the agent is registered with an
agent_name
. This step disables automatic dispatch and allows you to dispatch agents explicitly via the API. • Then, use the
createDispatch
method from the
AgentDispatchService
, providing the
room_name
and
agent_name
. You can also add metadata if you want to pass extra information to the agent. Here’s a simple example in Python to illustrate:
Copy code
from livekit import api

async def create_dispatch():
    lkapi = api.LiveKitAPI()

    dispatch = await lkapi.agent_dispatch.create_dispatch(
        api.CreateAgentDispatchRequest(
            agent_name="my-agent",
            room="my-room",
            metadata='{"user_id": "123", "name": "Alice"}'
        )
    )
    print("Dispatch created:", dispatch)
Just double-check that your API calls have the right
agent_name
and
room_name
so the correct agent gets dispatched. And if you run into any other questions or need a hand, please don’t hesitate to reach out—I’m here to help! Sources: Authentication | LiveKit Docs | lib/livekit/agent_dispatch_service_client.rb | Agent dispatch | LiveKit Docs | packages/livekit-server-sdk/src/AgentDispatchClient.ts | livekit-api/livekit/api/agent_dispatch_service.py