Practical technical guides / AI engineering: deployment and shared-host execution

Agents Sharing One Host: Admit Shell Commands by Memory Budget, Not by a Concurrency Cap

A simulation of twelve agent commands on an 8 GiB host: a low concurrency cap is safe but slow, budget admission needs per-command cgroup limits and corrected estimates.

By Sandeep Belgavi · Published 2026-11-05

A simulation of twelve agent commands on an 8 GiB host: a low concurrency cap is safe but slow, budget admission needs per-command cgroup limits and corrected estimates.

Three agents and one out-of-memory kill

Jenya, Tanishka and Geetha each run a coding agent against the Belgavi's Book Store repositories on one shared build machine with 8 GiB of memory. Each agent runs shell commands: installs, test suites, builds, the occasional grep. Most of the time this works. Then three of them start heavy commands within a few seconds of each other, the machine runs out of memory, and the kernel's out-of-memory killer ends whichever process it judges worst. Often that is a test run belonging to an agent that did nothing unusual. The agent sees a non-zero exit, reads the truncated output, and tries again, adding more load.

A preprint posted on 3 November 2026, "ToolSched: Resource-Efficient Tool Scheduling for Shared-Host LLM Agents" (arXiv 2611.05269), studies exactly this setting. Its abstract makes the key observation: agents' resource demands are bursty, and "shell command delay is imperceptible to agents", so there is room to choose when commands actually run. The authors build a Shell MCP server where agents send a resource estimate with each command, and report better utilisation and throughput than unscheduled execution on Terminal Bench. Their figures are theirs; this guide does not reproduce them. It explains the underlying admission choices with a small simulation you can run, and the Linux controls that make them safe.

Three admission policies

There are three simple ways to decide when a command starts.

Unscheduled. Every command starts at once. This is what happens when each agent has its own shell on a shared host. It is fine until the sum of concurrent peaks exceeds memory.

A concurrency cap. At most N commands run at once; the rest wait in arrival order. It is easy to build and understand, but N is a count, not a size. Two large builds under a cap of 2 can still exhaust memory, and a cap low enough to prevent that leaves the machine idle when the commands are small.

Memory-budget admission. Each command arrives with an estimate of its peak memory. A command starts only if the estimates of the running commands plus its own fit in the budget, the host's memory minus a reserve for the system and the agents themselves. Commands wait in arrival order and do not overtake each other, so a large command cannot be starved by a stream of small ones.

Budget admission is only as good as its estimates, and agents are not good at estimating memory. So it needs two companions: a hard per-command limit enforced by the kernel, and estimates corrected from what each command actually used last time.

The kernel backstop: cgroup v2

On Linux, cgroup v2 gives each command its own memory accounting and limits. The kernel's cgroup v2 documentation defines the controls that matter here:

  • memory.max is the hard limit. If a cgroup's usage reaches it and cannot be reduced, the OOM killer is invoked inside that cgroup, so the command that exceeded its own limit is the one killed, not a bystander.
  • memory.high is a throttle: above it, the cgroup's processes are put under heavy reclaim pressure but not killed. It is useful as an early brake.
  • memory.peak records the highest usage of the cgroup since it was created, which is exactly the observed peak needed to correct the next estimate.

On a systemd host, a transient scope puts a command in its own cgroup with limits:

# Run one agent command with a hard memory limit and a soft brake below it.
systemd-run --user --scope --quiet \
  -p MemoryMax=2400M -p MemoryHigh=2000M \
  -- pytest -n 4

Reading memory.peak requires the cgroup path, which is easier when the scheduler creates the cgroup itself or runs the command as a transient unit and queries it before it is removed. The simulation below records the observed peak directly. We did not run cgroups for this guide; the machine used for the simulation runs Windows.

A simulation of one busy minute

The companion example replays twelve synthetic commands from three agents on an 8 GiB host with a 1 GiB reserve. The commands range from ls at 20 MiB to building a search index at 3,200 MiB. One estimate is badly wrong: the agent says the index build needs 1,500 MiB. Every command holds its true peak for its whole run, which is pessimistic, and the host kills the largest running command whenever demand exceeds 8,192 MiB. A killed command is retried after 5 seconds, at most three attempts in all.

PolicyHost OOM killsCommands given upHighest demand (MiB)Total wait (s)Last command done (s)
Unscheduled9311,000038
Cap of 2005,800722140
Cap of 4109,000173105
Budget, estimates as given209,100148106
Budget, per-command limit, corrected estimates007,36020388

The unscheduled run finishes early only because three commands gave up after three kills each. A cap of 2 is safe and slow; a cap of 4 is faster and still causes a kill. Budget admission with the given estimates looks good on waiting but still overcommits, because the index build's estimate is half its real size.

The last row adds a per-command limit of 1.5 times the estimate. The index build hits its own limit at once and is the only command killed; its observed peak replaces the estimate, and on retry it is admitted only when 3,200 MiB really is free. No other agent's command is touched, and everything finishes sooner than under any cap.

What the agent should see

The scheduler decides when a command runs; the agent should know that its command is waiting and why. Return a status such as "queued behind 2 commands, 2,900 MiB requested, 1,700 MiB free" rather than blocking silently, so the agent does not interpret a long wait as a hang. When a command is killed by its own limit, return that fact and the observed peak, not just the exit code 137. An agent that reads "killed: exceeded its 2,250 MiB limit, peak 3,200 MiB" can split the work or ask for more; an agent that reads only truncated output usually retries the same command.

Keep the estimate field optional and default it from history: the last observed peak for the same command and working directory, or a conservative default for unknown commands. Agents' own estimates are hints, and the scheduler should never trust a hint below a known observed peak.

Things that still go wrong

  • Memory charged somewhere else. Work a command causes in a daemon it talks to, such as a database or a container engine, is charged to the daemon's cgroup, not the command's. Shared file pages are charged to the cgroup that first touched them. Keep the reserve for these.
  • Commands that fork into the background. A build server or file watcher started by a command outlives it. Run each command in its own cgroup and kill the cgroup at the end, not just the parent process. Setting memory.oom.group makes the OOM killer treat the cgroup as one workload and kill all of it together.
  • Head-of-line blocking. Strict arrival order means a large command at the front holds back small ones behind it. That is the price of not starving large commands; an alternative is to let small commands pass only while the large one's wait stays under a bound.
  • CPU and disk. Memory is the resource whose exhaustion kills processes, but CPU and disk I/O contention slow everything down. The same admission idea extends to CPU shares and I/O weight; ToolSched's abstract mentions memory, CPU and I/O.

When this is unnecessary

One agent per machine, or agents in separate containers with their own memory limits, already have isolation. A machine with far more memory than the sum of plausible peaks does not need admission control. And a fixed concurrency cap is perfectly adequate when commands are of similar size, which is common for agents that only run tests.

Limits

The simulation uses invented commands, peaks and durations, a 1-second tick, a pessimistic constant-peak model, and a simplified OOM victim choice (the largest command); the real kernel's choice uses a badness score and can differ. Killing at a command's own limit is modelled at start; in reality it happens when usage reaches the limit. The ToolSched figures are from an unreviewed preprint.

Primary references and authorship

Original explanation and synthetic examples by Sandeep Belgavi. Companion code: Sandeep-BA/agent-decision-lab/examples/python/host_memory_admission. Views are my own and do not represent any employer, past or present.