How do I dispatch multiple agents to the same room...
# ask-ai
l
How do I dispatch multiple agents to the same room using Python
t
Thanks for reaching out, Derek. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm happy to help you dispatch multiple agents to the same room using Python! Here are two friendly methods you can try: Method 1: Dispatch Multiple Agents via API You can use the
AgentDispatchService
to send out multiple agents by calling
create_dispatch()
for each agent name. Here's a simple example:
Copy code
import asyncio
from livekit import api

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

    room_name = "my-room"
    agent_names = ["agent-1", "agent-2", "agent-3"]

    for name in agent_names:
        dispatch = await lkapi.agent_dispatch.create_dispatch(
            api.CreateAgentDispatchRequest(
                agent_name=name,
                room=room_name,
                metadata='{"purpose": "multi-agent-demo"}'
            )
        )
        print(f"Dispatched {name} to {room_name}: {dispatch}")

    await lkapi.aclose()

asyncio.run(dispatch_multiple_agents())
Method 2: Dispatch Multiple Agents via Participant Token Another approach is to set up a participant’s access token to automatically dispatch multiple agents when they connect. You can do this by adding multiple
RoomAgentDispatch
entries in the
RoomConfiguration
. Here's how it looks:
Copy code
from livekit.api import (
    AccessToken,
    RoomAgentDispatch,
    RoomConfiguration,
    VideoGrants,
)

def create_token_with_multiple_agents():
    token = (
        AccessToken()
        .with_identity("user123")
        .with_grants(VideoGrants(room_join=True, room="my-room"))
        .with_room_config(
            RoomConfiguration(
                agents=[
                    RoomAgentDispatch(agent_name="transcription-agent", metadata='{"role": "transcriber"}'),
                    RoomAgentDispatch(agent_name="summarization-agent", metadata='{"role": "summarizer"}'),
                    RoomAgentDispatch(agent_name="moderation-agent", metadata='{"role": "moderator"}'),
                ],
            ),
        )
        .to_jwt()
    )
    return token

# Usage
token = create_token_with_multiple_agents()
print("Generated token:", token)
If you have any questions or want me to walk you through any part of this, please don’t hesitate to ask. I’m here to help! Sources: Docs::Agents | How to Enable Agent-to-Agent Speech Detection in LiveKit
l
Can two workers be assigned to the same room?
t
Absolutely, you can assign two workers to the same room in LiveKit! This is done through explicit agent dispatch, where you have the flexibility to specify multiple agent dispatches for a single room, each with its own
agent_name
. For instance, using the
AgentDispatchService
, you can create multiple dispatches for the same room like this:
Copy code
await lkapi.agent_dispatch.create_dispatch(
    api.CreateAgentDispatchRequest(agent_name="agent1", room="my-room")
)
await lkapi.agent_dispatch.create_dispatch(
    api.CreateAgentDispatchRequest(agent_name="agent2", room="my-room")
)
This way, you’re assigning two different workers (for example,
agent1
and
agent2
) to the same room. Just a quick note: by default, automatic dispatch assigns only one agent per room, so to have multiple workers, explicit dispatch is the way to go. Also, if you want your agents to communicate with each other, you can set
RoomInputOptions.participant_kinds = [rtc.ParticipantKind.PARTICIPANT_KIND_AGENT]
. If you have any questions or need a hand with this, just let me know—I’m here to help! Sources: Docs::Agents | How to Enable Agent-to-Agent Speech Detection in LiveKit | Agent dispatch | LiveKit Docs
l
Instead of video_gen, can I use hedra's avatar session?
Copy code
async def entrypoint(ctx: JobContext, agent_identity: str | None = "agent"):

    avatar_options = AvatarOptions(...)

    video_gen = ...

    runner = AvatarRunner(
        room,
        audio_recv=DataStreamAudioReceiver(
            room,
            sender_identity=agent_identity
        ),
        video_gen=video_gen,
        options=avatar_options,
    )
t
Hi there! I wanted to clarify that
hedra.AvatarSession
can't be used as a substitute for
video_gen
in
AvatarRunner
, as they actually serve different purposes and operate at different levels: •
video_gen
in
AvatarRunner
is a
VideoGenerator
implementation that creates video frames based on audio input. •
hedra.AvatarSession
is a higher-level integration connecting to Hedra's API to animate avatars, and it works alongside
AgentSession
, not
AvatarRunner
. To use Hedra avatars smoothly, you’ll want to integrate
hedra.AvatarSession
with
AgentSession
like this:
Copy code
session = AgentSession(...)
avatar = hedra.AvatarSession(avatar_image=image)
await avatar.start(session, room=ctx.room)
await session.start(...)
If anything’s unclear or if you’d like a hand with this, please don’t hesitate to reach out—I’m here to help! Sources: examples/avatar_agents/audio_wave/avatar_runner.py | Hedra Realtime Avatar integration guide | LiveKit Docs | Docs::Agents
l
Can I run the avatar session as a separate worker
t
Absolutely! A
hedra.AvatarSession
can indeed run as a separate worker in LiveKit. Think of the avatar worker as its own participant in the room, happily publishing synchronized audio and video tracks on behalf of the main agent session. To get this going, you’d start the
AvatarSession
alongside the
AgentSession
and the room context. The agent session sends its audio output over to the avatar worker, which then takes care of rendering and publishing it to the room. You’ll recognize the avatar worker as a participant with kind
agent
and the attribute
lk.publish_on_behalf
. If you’d like, I’m here to help you step through the setup or answer any questions you might have! Sources: Virtual avatar integrations | LiveKit Docs | Hedra Realtime Avatar integration guide | LiveKit Docs | Docs::Agents | livekit-plugins/livekit-plugins-hedra/README.md
l
How do I create a token for multiple agent dispatches
t
To help you create a token for multiple agent dispatches in LiveKit, you just need to include multiple
RoomAgentDispatch
entries in the
RoomConfiguration
while generating the participant’s access token. Here’s a friendly example in Python to guide you through:
Copy code
from livekit.api import AccessToken, RoomAgentDispatch, RoomConfiguration, VideoGrants

token = (
    AccessToken()
    .with_identity("participant")
    .with_grants(VideoGrants(room_join=True, room="my-room"))
    .with_room_config(
        RoomConfiguration(
            agents=[
                RoomAgentDispatch(agent_name="agent1", metadata='{"role": "moderator"}'),
                RoomAgentDispatch(agent_name="agent2", metadata='{"task": "transcription"}'),
            ]
        )
    )
    .to_jwt()
)

print("Generated token:", token)
Each
RoomAgentDispatch
lets you specify an
agent_name
and optional
metadata
to provide context to the agent. This way, the token will dispatch the agents you’ve specified as soon as the participant connects to the room. If you have any questions or need a hand with anything else, please don’t hesitate to ask—I’m here to help! Sources: Docs::Agents