Can you have multiple Workers in LiveKit? If so ho...
# ask-ai
s
Can you have multiple Workers in LiveKit? If so how can you initialize them in python with
cli.run_app()
?
t
Thanks for reaching out, Antonio. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Absolutely, you can have multiple Workers in LiveKit, and the great news is they’re built to scale effortlessly. When you start a worker, it registers itself with the LiveKit server and joins a pool of workers. From there, LiveKit takes care of balancing the workload across all the available workers automatically. To get a Worker up and running in Python using
cli.run_app()
, you’ll want to create an instance of
WorkerOptions
and pass it in. This
WorkerOptions
needs an
entrypoint_fnc
, which is an async function that kicks in whenever a job is assigned to the worker. If you like, you can also add a
prewarm_fnc
to set up any resources before the job starts — super handy! Here’s a quick example to show you how it looks:
Copy code
from livekit.agents import WorkerOptions, cli

async def entrypoint(ctx):
    # Your agent logic here
    pass

def prewarm(proc):
    # Initialize resources (e.g., load models)
    proc.userdata["model"] = MyModel.load()

if __name__ == "__main__":
    cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint, prewarm_fnc=prewarm))
Every time you run this script, it spins up one worker in the pool, and LiveKit handles all the orchestration and scaling behind the scenes. Pretty neat, right? Sources: Deployment and scaling | LiveKit Docs | Deployment and scaling | LiveKit Docs | Anatomy of an Agent | LiveKit Docs | Anatomy of an Agent | LiveKit Docs | livekit-agents/livekit/agents/cli/cli.py
s
How can I have workers with different agent names?
t
Hey there! If you want to assign different agent names to workers in LiveKit, you can do this by setting the
agent_name
field in the
WorkerOptions
when you initialize the worker. This way, you get explicit control over which agent joins a specific room. Here’s a quick example to help you out:
Copy code
if __name__ == "__main__":
    cli.run_app(
        WorkerOptions(
            entrypoint_fnc=entrypoint,
            agent_name="my-agent-1",  # Just pop in a unique name here
        ),
    )
You can spin up multiple workers with different names like
"my-agent-1"
,
"my-agent-2"
, and so on, each running as its own process or service. After you’ve got your workers named, you can use the LiveKit API to send a specific agent to a room like this:
Copy code
import asyncio
from livekit import api

async def dispatch_agent():
    lkapi = api.LiveKitAPI()
    dispatch = await lkapi.agent_dispatch.create_dispatch(
        api.CreateAgentDispatchRequest(
            agent_name="my-agent-1",  # Make sure this matches the WorkerOptions name
            room="my-room",
            metadata="optional metadata"
        )
    )
    print("Agent dispatched:", dispatch)
Just a few things to keep in mind: • Each worker needs its own unique
agent_name
so you can dispatch them individually. • Once you set
agent_name
, automatic dispatch is turned off—agents will only join rooms when you explicitly send them via the API. • This setup is super handy for things like telephony integrations or when you have multiple agents and want to control behavior based on the agent type. Sources: Dispatching agents | LiveKit Docs | Making calls using SIP | LiveKit Docs | Agents telephony integration | LiveKit Docs | Accepting incoming calls | LiveKit Docs | Agent dispatch | LiveKit Docs
s
Can you give me an example of how I can spin up multiple workers with different names like
"my-agent-1"
,
"my-agent-2"
, each running as its own process or service?
t
Hey there! If you're looking to spin up multiple workers with different agent names in LiveKit, each running as its own process or service, here’s a friendly step-by-step guide to help you out: 1. Deploy Multiple Worker Instances with Unique Agent Names You can start each worker process independently, giving each one its own
agent_name
in the
WorkerOptions
. Here’s a quick example:
Copy code
from livekit.agents import WorkerOptions, cli

async def entrypoint():
    # Put your agent logic here
    pass

if __name__ == "__main__":
    # Start the worker with a specific agent name
    cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint, agent_name="my-agent-1"))
Then, just run multiple instances of this script, each with a different
agent_name
like so:
Copy code
# Terminal 1
LIVEKIT_URL=<ws://localhost:7880> LIVEKIT_API_KEY=xxx LIVEKIT_API_SECRET=yyy python agent1.py start --agent_name "my-agent-1"

# Terminal 2
LIVEKIT_URL=<ws://localhost:7880> LIVEKIT_API_KEY=xxx LIVEKIT_API_SECRET=yyy python agent2.py start --agent_name "my-agent-2"
2. Use a Dispatcher to Launch Workers Dynamically If you want to get a bit fancier, you can launch multiple worker processes programmatically with different settings or identities. Here’s how:
Copy code
import subprocess
import os

def launch_worker(agent_name, room_name):
    cmd = ["python", "agent_runner.py"]
    env = os.environ.copy()
    env["AGENT_NAME"] = agent_name
    env["ROOM_NAME"] = room_name
    process = subprocess.Popen(cmd, env=env)
    return process

# Launch two different agents
launch_worker("my-agent-1", "room1")
launch_worker("my-agent-2", "room2")
3. Production Deployment Best Practices • Run multiple servers or containers, each with its own
agent_name
. • Consider using orchestration tools like Kubernetes or Docker to help manage scaling smoothly. • Each worker will automatically share availability and capacity info with the LiveKit server, which helps with load balancing. If you’d like, I can also share an example Docker setup or Kubernetes deployment config to get you started with this pattern. Just let me know! Sources: Worker lifecycle | LiveKit Docs | Deployment and scaling | LiveKit Docs | Deployment and scaling | LiveKit Docs | WorkerOptions | LiveKit Agents | examples/avatar_agents/audio_wave/dispatcher.py