swift-oyster-64824
08/20/2025, 12:09 AMcli.run_app()
?tall-airline-16756
08/20/2025, 12:09 AMtall-airline-16756
08/20/2025, 12:10 AMcli.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:
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.pyswift-oyster-64824
08/20/2025, 12:11 AMtall-airline-16756
08/20/2025, 12:11 AMagent_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:
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:
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 Docsswift-oyster-64824
08/20/2025, 12:15 AM"my-agent-1"
, "my-agent-2"
, each running as its own process or service?tall-airline-16756
08/20/2025, 12:15 AMagent_name
in the WorkerOptions
. Here’s a quick example:
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:
# 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:
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