Two agents run fine. Five run fine. Somewhere around ten, the fan gets loud, the mouse starts to stutter, and the editor you are typing in takes a second to show each letter. Then a terminal tab closes on its own, and the log says the kernel killed a process to free memory. It picked the test run you were waiting for.
This post is for people who run many coding agent sessions on one machine. It covers what actually takes the memory, how to measure it, how to put a hard cap on each agent with plain Linux tools or with Docker, and what happens when an agent reaches its cap. At the end it says how Sessionboxer sets these limits, and what it does not do.
Where the memory goes
A game studio described the problem well in issue #107433 on the Claude Code repository, opened on November 5, 2026. On one workstation with 125 GB of RAM, 24 Claude Code sessions took 9.3 GB, about 390 MB each. The processes those sessions started added another 6.3 GB across 201 processes. Two MCP servers ran about 20 copies each, one per session. One session's child processes reached 1.8 GB, and a single vitest process used 1.1 GB.
So the agent process itself is only part of the bill. The larger and less predictable part is what the agent starts: test runners, bundlers, language servers, a browser for an MCP server, a database for the integration tests. Those come and go with each turn, which is why memory use looks fine for an hour and then spikes.
The studio's log also showed the kernel's out-of-memory killer at work, with node and python3 as the most frequent victims. They were careful to say some of those kills came from CI jobs, not agents. The request in the issue is still open: shared MCP servers between sessions, and a memory budget across a session's processes. Claude Code does not have either today.
Measure before you cap
Start by finding out what one session costs on your own project. On Linux, this lists every process under a Claude Code session with its resident memory in kilobytes:
ps -o pid,rss,cmd --ppid "$(pgrep -n claude)"
That shows direct children only, and the numbers overlap. A commenter on the issue pointed out that adding up RSS across many similar node processes counts shared libraries and shared pages once per process, so the total looks bigger than what the machine really pays. The kernel's PSS figure splits shared pages fairly between the processes that use them:
grep Pss /proc/<pid>/smaps_rollup
If your agents run in containers, docker stats is simpler. Its MEM USAGE / LIMIT column shows what each container uses and what it is allowed, and --no-stream prints one snapshot instead of a live view (docs).
Watch a session through a few full turns, including one that runs the test suite. The peak matters more than the average.
Cap a single agent without containers
On a Linux machine with systemd, you can start an agent inside its own control group with a hard memory limit. systemd-run --user --scope keeps the command attached to your terminal, and -p sets a resource property on the group (systemd-run):
systemd-run --user --scope -p MemoryMax=4G -p MemorySwapMax=0 -p CPUQuota=200% claude
MemoryMax and CPUQuota are described in systemd.resource-control. CPUQuota=200% means at most two CPUs' worth of time. Everything the agent starts inherits the group, so a runaway vitest counts against the same 4 GB. MemorySwapMax=0 stops the group from swapping when it reaches the cap; without it, a session at its limit can slow to a crawl instead of failing.
This is the per-session limit the studio in the issue said they already use. It protects the rest of the machine. It does not isolate files or the network: the agent still runs as you, in your home directory.
Cap it with Docker
If the agent already runs in a container, Docker sets the same cgroup limits with two flags. Docker's resource constraints page is the reference:
docker run --memory=4g --cpus=2 ...
Two details from those docs are easy to miss. First, a container has no limits at all unless you set them. Second, with --memory set and --memory-swap left out, the container can also use as much swap as its memory limit, if the host has swap. Set --memory-swap to the same value as --memory to turn swap off for that container.
--cpus is a ceiling. An agent capped at two CPUs can still use both of them fully while it compiles, but it cannot take the other cores your editor needs.
What happens when an agent hits the cap
When the processes in the group ask for more than the limit, the kernel's OOM killer kills one of them, inside that group only. As a commenter on the issue explained, the kernel scores processes by how much they allocated, so the largest one usually dies first. In practice that is the test runner or the build, not the agent.
That is often the outcome you want: the agent sees the command fail, reads the error, and can retry with fewer workers. Sometimes the agent process itself is the one killed, and the session ends. Either way, the work in that process is lost. A cgroup limit kills; it cannot pause or queue. The studio wrote the same thing in the issue: their per-session limit "protects the machine but kills work when it trips".
So pick the cap from your measurement, with room for the peak. If the test suite needs 3 GB, a 2 GB cap means every test run fails. It also helps to tell the agent about the limit in its instructions and ask it to run tests with fewer workers, for example vitest --maxWorkers=2 or pytest -n 2.
CPU caps never kill anything. A capped agent just takes longer.
How Sessionboxer sets the limits
Sessionboxer runs every session in its own Docker container, and every container gets a CPU and a memory limit. The defaults are 2 CPUs and 4 GB, set in Global settings → Environment. The Connect your agent chapter covers those defaults. A session can override them under Advanced… → Environment → Limits when you start it, in steps of half a CPU and half a gigabyte.
Everything the agent starts inside the box counts against that box's limit: the test runner, the browser on the box's desktop, and an MCP server it launches there. With Docker inside the box turned on, the containers the agent starts run inside the box too, so a Postgres for the tests shares the session's memory. Windows and macOS sessions are different: the VM gets its own RAM and CPUs (4 GB and 2 by default), on top of the box's limit, plus about 1.5 GB for QEMU.
The other half is not running what you are not using. Stop pauses a session's container, and a stopped box uses no CPU or memory. Resume brings back the files, the installed packages and the conversation. A scheduled task can stop its own session when the turn ends, so nightly runs do not pile up.
What it does not do:
- Each box runs a Linux desktop, VS Code and the agent, so one session needs more memory than a bare
claudein a terminal. Sessionboxer is not the lighter option. - Changing the limits of an existing session applies to the next box built for it, through Rebuild Sandbox or a fork. A running box keeps the limits it started with.
- Sessionboxer sets the memory limit and leaves swap at Docker's default, so on a host with swap a box can swap as described above.
- Nothing moves memory between boxes or queues a new session until there is room. A box at its limit gets the same OOM kill as any other cgroup. If the box itself goes down, the session shows Sandbox exited unexpectedly with the exit code.
If you are deciding how to split agents in the first place, running several coding agents in parallel compares worktrees, containers and cloud machines. For agents that need their own databases and compose stacks, the Docker-in-Docker post covers the options and what each one hands to the agent.