how to distribute the load to multiple livekit pyt...
# ask-ai
g
how to distribute the load to multiple livekit python agents. As of now, if one of the agent picks the room again and again, the CPU utilization goes too high and eventually crashes the server. I have 5 ECS tasks running my livekit agent.
t
Thanks for reaching out, Harsh. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're asking how to stop one LiveKit Python agent from repeatedly picking the same rooms (causing high CPU) across your 5 ECS tasks. Below are the practical checks and changes that will distribute load and prevent a single agent from getting overloaded. 1) Confirm all agents are healthy and registered • Verify all 5 ECS tasks are running and successfully connect/register with the LiveKit server. If only one agent is visible/healthy, LiveKit will route jobs to it. • Check agent logs for successful registration calls (where your agent calls the agent runtime to join the cluster). 2) Use agent-side load control • Implement a
load_fnc
and
load_threshold
so an agent rejects new jobs when its load is high. Example pattern:
Copy code
def compute_load(server):
    return min(len(server.active_jobs) / 10, 1.0)  # scale by max sessions

server = AgentServer(load_fnc=compute_load, load_threshold=0.9)
• This causes LiveKit to dispatch new rooms to other available agents once an instance reaches capacity. 3) Graceful drain on shutdown/rollouts • Set a drain timeout so tasks finish existing rooms before deregistering (avoid sudden concentration of rooms on the remaining agents). • Example:
AgentServer(drain_timeout=600)
to allow in-flight sessions to complete. 4) Monitor and scale • Monitor CPU and active job counts per ECS task (CloudWatch/custom metrics). If average sessions per agent approach ~10–25 depending on instance size, scale out. • Choose non-burstable instance types (e.g., c5/m5) for consistent CPU performance. 5) Avoid sticky room routing • If many clients reconnect to the same room name, they will be routed to the agent currently hosting that room. Use unique room IDs per session or ensure rooms are closed when sessions end if you want the scheduler to rebalance. If you’d like, tell me: • what ECS instance types you’re using, • how many concurrent rooms/sessions you see per agent, • and whether agents report themselves as healthy in the LiveKit server logs — and I’ll suggest concrete
load_fnc
parameters and scaling guidance. Sources: Deploying to custom environments | LiveKit Docs | Agent server options | LiveKit docs | Troubleshooting Latency and Timeout Errors with Turn Detection on AWS | Agent server | LiveKit docs
g
i have not specified any load_func as of now in my agents, what is the default that it will take?
t
When no
load_fnc
is provided, the agent uses average CPU utilization over a 5‑second window as its load metric. The default
load_threshold
is 0.7 (70%), so the agent will stop accepting new jobs while its CPU load remains above 70%. Sources: Agent server options | LiveKit docs | Deploying to custom environments | LiveKit Docs
g
but its still picking jobs i guess because cpu utlization is above 90%
t
Thanks — I understand this is urgent. You said CPU is >90% but the agent still picks jobs. Here’s what to check and next steps to stop that behavior. 1) Confirm the agent is actually reporting CPU load • Check agent logs for periodic load reports/health pings to the LiveKit server. If the agent isn’t reporting load, the scheduler may consider it available. • Verify network/connectivity between each ECS task and the LiveKit server so registration and heartbeats succeed. 2) Verify the runtime defaults apply to your deployment • Defaults: load is average CPU over 5s and load_threshold = 0.7. These defaults are only used in self‑hosted/custom deployments. If you’re on LiveKit Cloud, `load_fnc`/threshold aren’t configurable. 3) Inspect ECS task CPU configuration • If your container has a CPU quota or is running on burstable instance types, reported CPU percent may be misleading or the process may be throttled. Use host-level metrics (instance CPU) as well as container metrics. • Ensure the agent process can read host CPU stats (some container runtimes need permissions). 4) Short-term mitigation options • Lower the threshold or provide a custom
load_fnc
that returns 1.0 when your measured metric (e.g., process CPU, active_sessions) exceeds your capacity so the agent immediately rejects new jobs. • Example approach: stop accepting new jobs when process CPU > 75% or active sessions > N.
Copy code
def compute_load(server):
    proc_cpu = get_process_cpu_percent()  # your implementation
    if proc_cpu > 75:
        return 1.0
    return proc_cpu / 75.0

server = AgentServer(load_fnc=compute_load, load_threshold=0.9)
5) Longer-term fixes • Scale out ECS tasks or use larger instance types (c5/m5) to keep per-agent sessions below ~10–25 depending on workload. • Ensure graceful drain during deploys so remaining agents don’t get overloaded. If you want, tell me: • ECS instance type and whether tasks have CPU limits, • whether agents’ logs show load reporting/registration, • and the LiveKit agent version — I’ll recommend a concrete
load_fnc
and threshold and specific monitoring checks.