Load Average Admission Control: Keep One Machine Usable With a Fleet of Coding Agents

One coding agent running a test suite is fine. A dozen agents in a dozen worktrees, each deciding on its own to run npm test or cargo build, slow the machine down together. The usual fix is a human watching htop and pausing agents by hand. That is the job of an admission controller.
This post builds one: a wrapper for every long command, a hook that makes it mandatory, a queue for resources that cannot be shared, and per-agent containers.
The Idea Is Old
GNU make's -l option already does this: if the load average "is not lower than the limit given with '-l', make waits until the load average goes below that limit, or until all the other jobs finish." The batch command in at(1) runs jobs "when the load average drops below 1.5, or the value specified in the invocation of atd." What is new is the client: it improvises commands, and there are many of it at once.
Know What the Number Measures
On Linux, the first three fields of /proc/loadavg count "jobs in the run queue (state R) or waiting for disk I/O (state D) averaged over 1, 5, and 15 minutes." Two consequences matter for a gate.
It is not a CPU number. Brendan Gregg's history of Linux load averages explains how a 1993 patch turned them into "system load averages." A stuck NFS mount raises load while CPUs sit idle, so a gate that kills on load alone can kill innocent work during a storage problem.
It lags. Gregg describes the values as "exponentially-damped moving sums of a five second average." Ten agents that check at the same moment all see headroom, all start, and the number catches up after the machine is saturated. This is the main failure mode of a naive "check then start" rule.
On Linux, pressure stall information is a better kill signal. /proc/pressure/cpu reports "the share of time in which at least some tasks are stalled" over 10, 60 and 300 second windows, separately from memory and I/O, and every cgroup has its own cpu.pressure file. The example uses load average because it is portable.
The Pattern: Check, Start, Watch, Kill, Retry
Set thresholds relative to core count. The example admits work below 2.5 times nproc and kills its own work above 5 times. On 16 cores that is 40 and 80.
#!/usr/bin/env bash
# agent-run: admission control for long agent commands
set -u
CORES=$(nproc)
START_MAX=$(( CORES * 5 / 2 ))
KILL_AT=$(( CORES * 5 ))
over() { awk -v l="$(cut -d' ' -f1 /proc/loadavg)" -v m="$1" 'BEGIN { exit !(l >= m) }'; }
for attempt in 1 2 3; do
tries=0 # 1. check, with jitter
while over "$START_MAX"; do
sleep $(( 10 + RANDOM % 20 ))
tries=$(( tries + 1 ))
[ "$tries" -ge 30 ] && { echo "agent-run: machine busy, try later" >&2; exit 75; }
done
setsid "$@" & pid=$! # 2. start in its own process group
strikes=0 # 3. watch
while kill -0 "$pid" 2>/dev/null; do
sleep 5
if over "$KILL_AT"; then strikes=$(( strikes + 1 )); else strikes=0; fi
if [ "$strikes" -ge 3 ]; then # 4. kill our group only
kill -TERM -- -"$pid"; sleep 10; kill -KILL -- -"$pid" 2>/dev/null
echo "agent-run: load above $KILL_AT, killed attempt $attempt" >&2
break
fi
done
wait "$pid"; rc=$?
[ "$strikes" -lt 3 ] && exit "$rc"
sleep $(( attempt * 60 + RANDOM % 30 )) # 5. retry with backoff
done
exit 75
Each step answers a specific failure:
| Step | Failure without it | What the wrapper does |
|---|---|---|
| Check | Every agent starts its build at once | Waits for load under the admit limit |
| Jitter | Waiting agents wake and start together | Random 10 to 30 second sleeps |
| Own process group | A kill leaves orphaned compilers and test workers | setsid gives the command its own group |
| Watch with strikes | A run keeps going after saturation, or one spike kills it | Three 5-second samples in a row over the limit |
| Targeted kill | pkill node kills other agents' work too |
TERM then KILL to its own group only |
| Retry and exit 75 | The agent reports "tests failed" when they never ran | Backs off, retries, then exits 75 |
Tell the agent that 75 means "the machine was busy, not your change." Otherwise it reads a killed run as a failure and starts "fixing" correct code.
Make the Gate Mandatory
"Check the load before long commands" in CLAUDE.md or AGENTS.md is a request, not a control. Anthropic's memory docs say so: "Claude treats CLAUDE.md files and auto memory as context. They aren't enforced configuration." They point to the alternative: "To block an action regardless of what Claude decides, use a PreToolUse hook instead."
A PreToolUse hook receives the tool input as JSON on stdin, with the shell command in tool_input.command. Exit code 2 blocks the call and stderr goes back to the model. The hook only has to refuse long commands that are not wrapped:
{ "hooks": { "PreToolUse": [ { "matcher": "Bash",
"hooks": [ { "type": "command", "command": "~/.claude/hooks/require-agent-run.sh" } ] } ] } }
#!/usr/bin/env bash
cmd=$(jq -r '.tool_input.command')
long='(npm (run )?(test|build)|pytest|go test|cargo (build|test)|make)( |$)'
if grep -Eq "$long" <<<"$cmd" && ! grep -Eq '^(agent-run|flock|sem) ' <<<"$cmd"; then
echo "Long command. Run it as: agent-run $cmd" >&2
exit 2
fi
The model reads the reason and rewrites the call. Keep the memory file note for the why and for exit 75.
Load Is Not the Only Shared Resource
Admission control solves "too much at once," not "two things that cannot run together." git worktree gives each agent a checkout, "sharing everything except per-worktree files such as HEAD, index, etc." Tests also touch things outside the tree: port 3000, a local app_test database, /tmp/app-cache, a container named test-redis. Two worktrees running integration tests together fight over those, and the failures look flaky.
For an exclusive resource, use flock(1), which waits "until the lock is available" and takes -w for a timeout. For "at most N at once", GNU parallel's sem is a counting semaphore. Always pass --id: the default name is the controlling tty, so agents in different terminals would never wait for each other.
flock -w 1800 /tmp/locks/app-test-db.lock agent-run npm run test:integration
sem --id e2e -j 2 --fg agent-run npm run e2e
Put the queue outside the gate, so the load check runs just before the command starts, not before a long wait in the queue.
Per-Agent Containers Remove Collisions
A queue serializes; a container removes the conflict. Mount each agent's worktree at the same path, /workspace, in its own container. Port 3000 in one container does not collide with port 3000 in another, and config sees one stable path.
Containers also cap each agent. Docker's resource constraints page starts with "By default, a container has no resource constraints"; --cpus and --memory set them:
N=7
docker run -d --name "agent-$N" --cpus 4 --memory 8g \
-v "$HOME/wt/agent-$N:/workspace" -w /workspace \
-p "127.0.0.1:$((8100 + N)):3000" agent-dev:latest sleep infinity
Publish ports only when an agent needs to show you something, and bind them to 127.0.0.1: without a host IP, Docker publishes "to all host addresses." 8100 + N tells you whose dev server you are looking at. For random ports, -P publishes exposed ports and docker port agent-7 lists them.
Caps do not replace the gate: eight containers at four CPUs each still oversubscribe 16 cores two times. Caps stop one runaway agent; the gate stops the fleet.
Checklist
- Thresholds scale with
nproc. - Waits use jitter; kills need several samples in a row.
- Each command gets its own process group, and the kill targets only it.
- "Killed for load" has its own exit code that the agent understands.
- A PreToolUse hook enforces the wrapper.
- Exclusive test resources sit behind
flock; limited ones behindsem --id. - Each agent has a container with caps, a stable path and loopback-only ports.
- On Linux, consider PSI for the kill decision.
It is the same idea as cooperative throttling with freno, where clients ask before writing instead of letting the database fall over. For the security case for per-agent containers, see why containers beat permission prompts.
Where Akmatori Fits
Akmatori keeps limits outside the model too. In the architecture, every tool call goes through the MCP Gateway, which checks the run's tool allowlist before it calls your systems, and the agent worker has no database credentials. Instructions guide the agent; the gateway enforces.
Conclusion
A fleet of agents on one machine needs what any shared system needs: admission control, queues, isolation, and enforcement that does not depend on the client behaving. Load average is a rough signal, but with jitter, strikes and a process-group kill it keeps a laptop usable.
If your team wants AI-assisted incident workflows with guardrails enforced outside the model, Akmatori helps SRE teams investigate alerts, coordinate response, and automate safe infrastructure actions. Powered by Gcore for global infrastructure reliability.
