How can I connect my custom Agent to the room usin...
# ask-ai
f
How can I connect my custom Agent to the room using api.AccessToken?
t
Thanks for reaching out, Taras. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hi there! To connect a custom Agent to a room using
api.AccessToken
, you’ll want to include an agent dispatch configuration in the access token through the
roomConfig
field. Let me walk you through the steps: 1. Define the Agent Name: First, make sure your agent worker is set up to listen for dispatch requests using a specific
agentName
. This is something you configure when initializing the agent worker. 2. Create an AccessToken with Room Configuration: Next, use the LiveKit server SDK to create an
AccessToken
and add a
RoomConfiguration
that includes a
RoomAgentDispatch
specifying the agent to dispatch. Here’s a handy example in Node.js:
Copy code
import { AccessToken } from 'livekit-server-sdk';
import { RoomAgentDispatch, RoomConfiguration } from '@livekit/protocol';

const roomName = 'my-room';
const agentName = 'test-agent';

async function createTokenWithAgentDispatch() {
  const at = new AccessToken(API_KEY, API_SECRET, {
    identity: 'my-participant',
    ttl: '15m',
  });

  at.addGrant({
    roomJoin: true,
    room: roomName,
  });

  at.roomConfig = new RoomConfiguration({
    agents: [
      new RoomAgentDispatch({
        agentName: agentName,
        metadata: '{"mydata": "myvalue"}', // Optional metadata
      }),
    ],
  });

  return await at.toJwt();
}
3. Ensure Agent Is Running: Lastly, please double-check that your agent is deployed and running with the matching
agentName
in its
WorkerOptions
. If the agent isn’t active, the dispatch won’t work as expected. If you’d like, I’m more than happy to share details and examples for other SDKs like Python or Rust—just let me know! Sources: Authentication | LiveKit Docs | Agent dispatch | LiveKit Docs | examples/agent_dispatch/README.md | examples/agent-dispatch/index.ts | examples/agent_dispatch.py
f
I need the code in Python
t
I'd be happy to guide you through connecting a custom Agent to a room using
api.AccessToken
in Python. Here's a simple example for you:
Copy code
from livekit_server_sdk import AccessToken, VideoGrant, RoomConfiguration, RoomAgentDispatch

API_KEY = "your_api_key"
API_SECRET = "your_api_secret"
room_name = "my-room"
agent_name = "test-agent"

# Create an access token with agent dispatch
access_token = AccessToken(API_KEY, API_SECRET)
access_token.identity = "my-participant"

# Add room join grant
video_grant = VideoGrant(room_join=True, room=room_name)
access_token.add_grant(video_grant)

# Configure agent dispatch in room configuration
room_config = RoomConfiguration(
    agents=[
        RoomAgentDispatch(
            agent_name=agent_name,
            metadata="{\"mydata\": \"myvalue\"}"  # Optional metadata
        )
    ]
)
access_token.room_config = room_config

# Generate the token
jwt_token = access_token.to_jwt()
print("Generated JWT Token:", jwt_token)
Just replace
your_api_key
and
your_api_secret
with your LiveKit API credentials. This will create a JWT token that includes the agent dispatch setup. If you have any questions or need a hand with anything else, please feel free to ask—I’m here to help!
f
Now, when I have jwt_token, how do I use it to add my custom agent to a room?
t
Once you have the
jwt_token
, you’re all set to connect your custom agent to the room. Here’s a simple way to do it: 1. Start Your Agent: Make sure your custom agent is up and running, and it’s set to listen for dispatch requests with the same
agentName
you included in the token. 2. Use the Token to Join the Room: The
jwt_token
lets the participant join the room. When they do, LiveKit will automatically dispatch the agent specified in the token to that room. 3. Optional: Explicit Dispatch: If you’d like to dispatch the agent without waiting for a participant to join, you can use the LiveKit API or AgentDispatchClient directly. Here’s an example in Python:
Copy code
from livekit_server_sdk import AgentDispatchClient

LIVEKIT_URL = "your_livekit_url"

client = AgentDispatchClient(LIVEKIT_URL, API_KEY, API_SECRET)
response = client.create_dispatch(
    room_name="my-room",
    agent_name="test-agent",
    metadata="{\"mydata\": \"myvalue\"}"
)
print("Dispatch Response:", response)
If any part of this feels unclear or you’d like a hand with these steps, please don’t hesitate to reach out—I’m here to help!
f
Provide code example for steps 1 Start Your Agent and 2 Use the Token to Join the Room
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!