Lesson 13 / Unit 3: Agent Workers
Load Thresholds
A worker stopped taking jobs at exactly 141, with no errors and no restarts. How a fixed count points at a counter, the 0.70 threshold, and the log lines that told us.
A worker stopped receiving jobs after 141 of them. It stayed registered, stayed connected, logged nothing, showed zero restarts and idle file descriptors. Only a process restart cleared it.
No error. No crash. Just a worker that the server had stopped talking to.
The numbers below are saved case-study results. The original raw run bundle is not included here, and this review did not rerun the fleet experiment.
Counter Symptoms
When a failure repeats at the same count, something is counting. A stable boundary is a useful counter or limit hypothesis, although leaks and races can also produce repeatable counts.
It reproduced at 140, 141, and 141, across windows of 56 minutes and 15 minutes, at two different concurrencies. Different durations, different load, the same count. It was driven by count alone, which rules out most of what you would normally suspect.
A fixed count is a strong signal. We did not use it early enough: we blamed the job maximum, then concurrent WebSocket writes, then a status loop, and instrumented the third one before doing the thirty-second division that identified the bug outright.
Threshold Arithmetic
This custom worker was configured with a maximum of 200 jobs; that is not a LiveKit default. It reports load as a fraction of that maximum, and the server stops assigning work to a worker above 0.70.
141 / 200 = 0.705That is the whole bug. The counter had reached the threshold and the server had quietly stopped sending work, exactly as designed.
| Jobs counted as active | Reported load | Server assigns new jobs? |
|---|---|---|
| 100 | 0.500 | yes |
| 141 | 0.705 | no |
Nothing was wrong with the server. It did what it was told by a number that had stopped being true.
Load Reporting
Every worker reports a load value between 0 and 1, and the server stops dispatching to it above a threshold. The worker computes the number; the server only reads it. Both knobs are listed in the Python API reference under the worker options.
In the Python agents framework these are two server options: load_fnc returns the current load, and load_threshold is the maximum load at which the worker still accepts jobs. The documented defaults are average CPU utilisation over a 5-second window and a threshold of 0.7. The docs also show a custom load_fnc that divides active jobs by a fixed maximum, which is the same shape as the worker here. That shape is fine, as long as the active count is correct. If jobs are counted in and never counted out, the load only climbs, and dispatch stops at a predictable number.
This is the worker lifecycle again: the server trusts what workers report. In the shutdown lesson the missing report was completion. Here it was load.
Counter Cleanup
The original case involved a custom Go worker, not the Python AgentServer example. Each assignment incremented its active job counter. A deferred decrement was supposed to run when the job’s goroutine finished.
The termination handler logged the event and never cancelled the job, so the job’s loop kept waiting on a context that only closed when the whole process did. No job ever returned, so no decrement ever ran.
So the fix is in the termination path: whatever handles it has to end the job, and then the code on the return path (the decrement) gets to run.
Paired Events
The log said so all along, and we read past it twice: 141 lines saying the server had terminated a job, and zero lines saying a job had ended. All 141 ended lines appeared at once on restart, the first time anything cancelled that context.
N of one event and zero of its counterpart is worth more than most metrics, and it was visible from the first capture. You can check it mechanically. List the job IDs that have a “terminated” line and no “ended” line:
comm -23 \ <(grep 'terminated' worker.log | grep -o 'job_id=[^ ]*' | sort -u) \ <(grep 'ended' worker.log | grep -o 'job_id=[^ ]*' | sort -u)Adapt the two patterns to your own log format. Any output is a job that never finished.
Dispatch Checks
- Blaming the configured maximum. The maximum was 200. The worker stopped at 141 because of the threshold applied to it.
- Instrumenting before dividing. The failure count divided by the job maximum named the bug in thirty seconds. Do that division first.
- Reading log lines alone. Every started needs its ended, every assigned needs its released. A missing partner is the finding.
- Trusting “connected” as healthy. A worker can be registered and connected while receiving nothing. Track jobs assigned per worker over time, and alert when it flattens while traffic continues.
Next: how to load test a voice agent so that failures like this show up in a test run before they show up in production.
Common questions
Why did my LiveKit worker stop receiving jobs?
Check its reported load first. The server stops assigning work to a worker whose load is above the threshold (0.70 in our case), so a load counter that climbs and never comes down will stop dispatch while the worker stays registered and connected.
What is the LiveKit load threshold?
It is the load value above which a worker stops receiving new jobs. In the Python agents framework it is the load_threshold option, which defaults to 0.7, and the load itself comes from load_fnc, a function that returns a value between 0 and 1.
How do I find a leaked job counter from logs?
Pair the events that should come in pairs. If you have N lines saying a job was terminated and zero saying a job ended, the jobs never returned and whatever runs on return, such as a counter decrement, never ran.
We do this work for teams shipping voice agents: Self-Hosted Voice Agent .
Edit this lesson on GitHub ↗Discussion
Questions, corrections and war stories welcome. Sign in with GitHub to comment.