how to configure load_threshold or load_func such ...
# ask-ai
e
how to configure load_threshold or load_func such that I only allow 7 - 10 concurrent jobs (sessions) per agent worker ?
t
Thanks for reaching out, Hamza. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'd be happy to help you configure
load_threshold
or
load_func
to allow 7-10 concurrent jobs per agent worker. You can achieve this by defining a custom
load_func
that calculates the worker's load based on the number of active jobs. Just a quick note: this setup is supported only in custom deployments, not in LiveKit Cloud. Here are some examples in Python and Node.js to guide you: Python Example:
Copy code
from livekit.agents import Worker, WorkerOptions

def compute_load(worker: Worker) -> float:
    return min(len(worker.active_jobs) / 10, 1.0)

opts = WorkerOptions(
    load_fnc=compute_load,
    load_threshold=0.9,
)
Node.js Example:
Copy code
import { Worker, WorkerOptions } from '@livekit/agents';

const computeLoad = (worker: Worker): Promise<number> => {
    return Math.min(worker.activeJobs.length / 10, 1.0);
};

const opts = new WorkerOptions({
    loadFunc: computeLoad,
    loadThreshold: 0.9,
});
In these examples, the
load_func
calculates the load as the ratio of active jobs to a maximum of 10. The
load_threshold
is set at 0.9, which means the worker will stop accepting new jobs once it reaches 9 or more concurrent jobs. Feel free to adjust the denominator or threshold value to better fit your specific needs. If you have any questions or want to explore this further, I'm here to help! Sources: Docs::Agents | Worker options | LiveKit Docs
e
and if we were to emit any scaling event if the threshold is crossed, how can we do so ?
t
Thanks for reaching out! I’d love to help clarify how LiveKit handles scaling. Instead of sending explicit "scaling events" when the
load_threshold
is crossed, LiveKit updates the worker's availability status automatically. This status change lets the LiveKit server know the worker should pause accepting new jobs, and it’s really handy for external autoscalers to manage scaling smoothly. Here’s a simple breakdown: • The worker regularly checks its load using the
load_fnc
, which gives a value between 0 and 1. • When the load goes over the
load_threshold
, the worker marks itself as unavailable (
WS_FULL
), and this update is logged and sent to the LiveKit server. • Your autoscaler (like Kubernetes Horizontal Pod Autoscaler) can watch these updates to decide when to scale. To set this up, just configure your autoscaler to keep an eye on the same load metric from
load_fnc
. You’d want it to scale out a bit before hitting the threshold—say, if your threshold is 0.9, you might start scaling at 0.7—to keep everything running smoothly. If you’d like, I’m here to provide more details or walk you through some examples. Just let me know! Sources: Deployment and scaling | LiveKit Docs | Docs::Agents | agents/src/worker.ts | livekit-agents/livekit/agents/worker.py