Can two AI agents join the same room?
# ask-ai
v
Can two AI agents join the same room?
t
Thanks for reaching out, Will. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Yes, two AI agents can join the same room in LiveKit. The platform supports multiple participants, including AI agents, in a single room. These agents can interact by subscribing to and publishing tracks, making it possible for them to coexist and collaborate within the same room. Sources: evaluating-agents/README.md | test/agent_test.go | Realtime media | LiveKit Docs
v
Show me the methods to have two agents exchange data
t
LiveKit offers a few handy ways for two agents to share data in real-time: • Remote Procedure Calls (RPC): This lets agents call methods on each other using structured JSON payloads—perfect for sending commands or sharing specific app states. • Byte Streams: Great for sending binary data like files or images. Agents can publish and receive these streams instantly. • Data Packets: Gives you low-level, custom data exchange when you need more precise control. Each option fits different needs. For instance, RPC works well for structured chats, while Byte Streams shine with larger or binary files. Just let me know what kind of data you’re looking to exchange, and I’d be happy to help you figure out the best approach!
v
show me sample code to have multiple agents join a room
t
Here are some examples to show you how to have multiple agents join a room in LiveKit—hope this helps! Rust Example
Copy code
use tokio::try_join;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let url = env::var("LIVEKIT_URL").expect("LIVEKIT_URL is not set");
    let api_key = env::var("LIVEKIT_API_KEY").expect("LIVEKIT_API_KEY is not set");
    let api_secret = env::var("LIVEKIT_API_SECRET").expect("LIVEKIT_API_SECRET is not set");
    let room_name = "test-room";

    let agent1 = connect_participant("agent1", &room_name, &url, &api_key, &api_secret);
    let agent2 = connect_participant("agent2", &room_name, &url, &api_key, &api_secret);
    let agent3 = connect_participant("agent3", &room_name, &url, &api_key, &api_secret);

    try_join!(agent1, agent2, agent3).await?;

    Ok(())
}

async fn connect_participant(
    identity: &str,
    room_name: &str,
    url: &str,
    api_key: &str,
    api_secret: &str,
) -> Result<(), Box<dyn std::error::Error>> {
    Ok(())
}
TypeScript Example
Copy code
import { AccessToken, RoomAgentDispatch, RoomConfiguration } from '@livekit/protocol';
import { AgentDispatchClient } from 'livekit-server-sdk';

async function createTokenWithMultipleAgents() {
  const at = new AccessToken();
  at.identity = 'my-participant';
  at.addGrant({ roomJoin: true, room: 'my-room' });
  at.roomConfig = new RoomConfiguration({
    agents: [
      new RoomAgentDispatch({
        agentName: 'agent1',
        metadata: '{"role": "moderator"}',
      }),
      new RoomAgentDispatch({
        agentName: 'agent2',
        metadata: '{"role": "helper"}',
      }),
    ],
  });
  return await at.toJwt();
}

async function dispatchAgents() {
  const dispatchClient = new AgentDispatchClient(process.env.LIVEKIT_URL);
  await dispatchClient.createDispatch('my-room', 'agent1');
  await dispatchClient.createDispatch('my-room', 'agent2');
}

createTokenWithMultipleAgents().then(token => {
  console.log('Token created:', token);
  dispatchAgents();
});
If you have any questions or want me to walk you through any part of this, just let me know—I’m happy to help!