give me python code to check how many rooms are ru...
# ask-ai
b
give me python code to check how many rooms are running with a agent name with - "someName" (that name is what we give in the worker options)
t
Thanks for reaching out, Mohammed. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks for reaching out! Could you please share if you're using any particular API or SDK to manage rooms and agents in LiveKit? Also, it would be helpful to know if you're looking for a solution using the LiveKit server API or if you have a custom setup in mind. I'm here to help you through this! Sources: livekit-api/livekit/api/room_service.py
b
python api
t
I'd love to help you with a Python code example that lists all rooms where a specific agent has been dispatched using the LiveKit API. Here's a friendly walkthrough:
Copy code
import asyncio
from livekit import api

async def list_rooms_by_agent(agent_name: str):
    """
    List all rooms that have a specific agent dispatched to them.
    
    Args:
        agent_name: The name of the agent to filter by
        
    Returns:
        List of room names where the agent is dispatched
    """
    # Initialize the LiveKit API client
    lkapi = api.LiveKitAPI()
    
    try:
        # Step 1: Get all active rooms
        rooms_response = await lkapi.room.list_rooms(api.ListRoomsRequest())
        matching_rooms = []
        
        # Step 2: Check each room for the target agent
        for room in rooms_response.rooms:
            dispatches = await lkapi.agent_dispatch.list_dispatch(room.name)
            
            # Step 3: Filter dispatches by agent name
            for dispatch in dispatches:
                if dispatch.agent_name == agent_name:
                    matching_rooms.append({
                        'room_name': room.name,
                        'dispatch_id': dispatch.dispatch_id,
                        'metadata': dispatch.metadata
                    })
                    break  # Found a match, so no need to check other dispatches in this room
        
        return matching_rooms
        
    finally:
        await lkapi.aclose()

# Example usage
async def main():
    agent_name = "test-agent"
    rooms = await list_rooms_by_agent(agent_name)
    
    print(f"Rooms with agent '{agent_name}':")
    for room in rooms:
        print(f"  - {room['room_name']} (dispatch: {room['dispatch_id']})")

if __name__ == "__main__":
    asyncio.run(main())
Here's a quick summary of what this code does: • It retrieves all active rooms by calling
lkapi.room.list_rooms()
. • Then, it checks each room for any agent dispatches using
lkapi.agent_dispatch.list_dispatch(room_name)
. • Finally, it filters to find the rooms where the agent's name matches the one you specify. If you'd like, I’m here to help you tweak this code further—whether that's adding features like partial name matching or improving performance. Just let me know! Sources: mcp/server.py | livekit-api/livekit/api/agent_dispatch_service.py