how to use agent_name to request dispatch from fro...
# ask-ai
c
how to use agent_name to request dispatch from frontend?
t
Thanks for reaching out, Rashmi. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To use
agent_name
to request dispatch from the frontend, you can choose between two main approaches: 1. Dispatch via API You can explicitly dispatch an agent to a room using the
AgentDispatchService
. Here’s an example in Node.js:
Copy code
import { AgentDispatchClient } from 'livekit-server-sdk';

const agentDispatchClient = new AgentDispatchClient(process.env.LIVEKIT_URL);

// Dispatch agent to a specific room
const dispatch = await agentDispatchClient.createDispatch(roomName, agentName, {
  metadata: '{"mydata": "myvalue"}',
});
Or in Python:
Copy code
import asyncio
from livekit import api

async def create_explicit_dispatch():
    lkapi = api.LiveKitAPI()
    dispatch = await lkapi.agent_dispatch.create_dispatch(
        api.CreateAgentDispatchRequest(
            agent_name=agent_name, 
            room=room_name, 
            metadata="my_metadata"
        )
    )
    await lkapi.aclose()
2. Dispatch on Participant Connection You can configure a participant's access token to automatically dispatch agents when they connect to a room. Here’s an example in Node.js:
Copy code
import { AccessToken, RoomConfiguration, RoomAgentDispatch } from 'livekit-server-sdk';

async function createTokenWithAgentDispatch() {
  const at = new AccessToken();
  at.identity = 'my-participant';
  at.addGrant({ roomJoin: true, room: roomName });
  at.roomConfig = new RoomConfiguration({
    agents: [
      new RoomAgentDispatch({
        agentName: agentName,
        metadata: '{"mydata": "myvalue"}',
      }),
    ],
  });
  return await at.toJwt();
}
Or in Python:
Copy code
from livekit.api import (
    AccessToken,
    RoomAgentDispatch,
    RoomConfiguration,
    VideoGrants,
)

def create_token_with_agent_dispatch():
    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="my_metadata")
                ],
            ),
        )
        .to_jwt()
    )
    return token
Key Points • If the target room doesn’t exist, it will be created automatically during dispatch. • You can pass custom metadata with the dispatch, which will be available in the agent’s
JobContext
. • To dispatch multiple agents, include multiple
RoomAgentDispatch
entries in
RoomConfiguration
. Choose the API approach for dynamic, programmatic control, or the token-based dispatch for automatic agent assignment when participants join rooms. Sources: Dispatching agents | LiveKit Docs | packages/livekit-server-sdk/src/AgentDispatchClient.ts | livekit-protocol/livekit/protocol/agent_dispatch.pyi