How do I manage two agent sessions in one room (en...
# ask-ai
m
How do I manage two agent sessions in one room (entrypoint)? One is always connected (text), and the other one is connected on request over the text agent's data channel?
t
Thanks for reaching out, Tyom. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To manage two agent sessions in one room, where one is always connected and the other connects on request, you can use explicit agent dispatch. Here's how you can achieve this: • Always-connected agent: Use token-based dispatch to automatically connect the first agent when the room is created. For example:
Copy code
from livekit.api import (
    AccessToken,
    RoomAgentDispatch,
    RoomConfiguration,
    VideoGrants,
)

# Create a token to dispatch the first agent
token = (
    AccessToken()
    .with_identity("my_participant")
    .with_grants(VideoGrants(room_join=True, room=room_name))
    .with_room_config(
        RoomConfiguration(
            agents=[
                RoomAgentDispatch(agent_name="always_connected_agent", metadata='{"type": "permanent"}')
            ],
        ),
    )
    .to_jwt()
)
On-demand agent: Implement RPC methods to control when the second agent connects. For example:
Copy code
@ctx.room.local_participant.register_rpc_method("connect_second_agent")
async def on_connect_second_agent(data: rtc.RpcInvocationData) -> None:
    # Logic to dispatch the second agent to the room
    # Example: await lkapi.agent_dispatch.create_dispatch(...)
The worker can manage multiple agent instances simultaneously in the same room, allowing for flexible session management. Let me know if you need further assistance! Sources: Agent dispatch | LiveKit Docs | Tree View: | dse-workbench | LiveKit Agents
m
Can you provide a Python example and an example of how to call the on-demand agent from the React client?
t
I'm sorry, but I don't have the information you need at the moment. If you would like a member of the LiveKit team to respond, please ask your question in one of the other channels.
m
How should the on-demand agent be disconnected?