Free How much of your firm runs on you? Take the 2-minute Key Man Score.
Lesson reading
0%

Lesson 12 / Unit 3: Agent Workers

Job Shutdown

Seventy-five seconds after the caller hung up, the job process still held 439 MB. What the framework waits for, the one-line ctx.shutdown fix, and where it leaks.

We wrote an agent worker, tested it end to end, and shipped a commit message claiming it reported job completion correctly when the entrypoint returned.

Then we watched the process list. Seventy-five seconds after the caller hung up, the job process was still resident, holding 439 MB.

This page covers what the framework actually waits for before a job ends, the one-line fix, and why the leak you should worry about is on a machine you are probably not watching.

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.

Shutdown Conditions

A job process does not exit when your entrypoint returns. It exits when a shutdown future resolves, and returning is not one of the things that resolves it.

We found this by reading the framework internals. The job process sits on one line:

shutdown_info = await self._shutdown_fut

That future is resolved when the room disconnects after connection, by explicit shutdown, or by framework termination. Returning cleanly from the entrypoint resolves nothing.

Our worker deliberately never connected. Another library owned the room connection, and connecting twice would have put a second participant in every call, so none of the resolution paths could fire. The process waited forever for a signal that was never going to arrive.

If your agent uses the standard AgentSession path, it connects to the room for you, and you may never hit this. The trap is any design where something else owns the connection. The job lifecycle docs describe the connected path and the shutdown hooks that run after it.

Explicit Shutdown

For the custom connection lifecycle in this case, signal job completion when the call logic is actually finished:

ctx.shutdown(reason="call ended")

For a normal Python AgentSession, use session.shutdown(drain=True) to finish queued speech and close the session; see the current session-ending guide. Do not shut down immediately after session.start, which completes setup before the conversation ends.

In the custom case, explicit job shutdown resolves the future, the job reports finished, and the process exits (the method is JobContext.shutdown in the Python agents reference). Put it on every exit path of your call logic, including the error path, while also accounting for framework-driven termination.

Cross-Host Resources

The leaked resource was not only the process on the agent host: the media server allocates a per-job handler when it assigns work and releases it when the worker reports the job finished.

An agent that never reports completion leaks on the media server too: a different machine, owned by a different team, watched on a different dashboard. You can stare at the agent host all day and see nothing wrong with the agent host.

The broken link is the dashed report. Without it, the job leaks on both machines, and the half on the media server shows up on a dashboard we were not watching.

This is the completion step of the worker lifecycle failing. The server trusts what the worker reports, so a report that never arrives means a job that never ends, as far as the server knows. A different unreported counter stops dispatch altogether in the next lesson.

Process Inspection

Every metric we emitted said the call completed cleanly, and our own counters ran in a finally block that executed exactly as intended. The counters were correct. The process was still there.

Watch the process table, not the logs. ps found in one command what seven green metrics had missed.

Here is the check we now run after a test call: list every descendant of the worker with its age and resident memory:

root=12345 # replace with the worker PID
descendants() { local p; for p in $(pgrep -P "$1"); do echo "$p"; descendants "$p"; done; }
pids=$(descendants "$root" | paste -sd,)
if [ -n "$pids" ]; then ps -o pid,etimes,rss,comm -p "$pids"; fi

etimes is seconds since the process started and rss is in KiB. The worker keeps a few idle, pre-warmed processes on purpose, so do not expect zero: expect the count to fall back to the idle count once calls end. A process that remains attached to a completed job beyond the expected cleanup window needs investigation; a warm idle process is not itself a leak. For how much memory each of those processes really costs, read RSS with care: PSS is the number to size from.

Shutdown Exercise

We wrote that the job was finalised correctly because the code looked like it should be. It was not. A test against real infrastructure found that out, and by then the claim was already in the history.

  1. Place a real call through the agent and hang up.
  2. Run the ps check above and confirm the job process exits.
  3. Confirm the media server released the job on its side too.
  4. Only then describe the lifecycle in the commit.

A lifecycle claim needs something that observed the lifecycle, and the same habit, applied at fleet scale, is what per-call resource accounting formalises.

Common questions

Why does my LiveKit agent job process stay alive after the call ends?

The job process awaits a shutdown future that is resolved when the room disconnects after connection, by explicit shutdown, or by framework termination. Returning cleanly from the entrypoint resolves none of them, so a worker that never connects to the room waits forever.

How do I end a LiveKit agent job explicitly?

Call ctx.shutdown(reason="call ended") on the JobContext when your call logic is finished. It is one line, and it is what lets the job report completion so both the agent host and the media server release what they allocated for it.

Where does a leaked agent job show up?

On two machines. The job process stays resident on the agent host, and the per-job handler the media server allocated at assignment is never released, because the job finished report is never sent.

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.

Free guide

Be The 10x Entrepreneur

Get the firm out of your head.

  • 8 chapters
  • 7 worksheets
  • 30 day plan