Give a model a shell for long enough and it will write while True: without a break, load a 4 GB CSV into a 2 GB box, or spawn a process per file in node_modules. None of this needs an attacker. The sandbox's job is to keep the damage inside the box. Your job is to make sure the agent learns what happened and does something sensible next.
This post goes through the four common failures, the limits that contain each, and how to report each to the model.
1. Runaway time: loops and hangs
Looks like: a command that never returns. Often a loop, a network call with no timeout, or a process waiting for input that never comes (npm init without -y, a git command opening an editor).
Contain it with: a per-command timeout and a sandbox lifetime. The command timeout should be per command type; the lifetime is the backstop if your orchestrator dies. Our post on three timeouts covers both. Also close stdin for commands that should not be interactive, and set CI=true and GIT_TERMINAL_PROMPT=0 so tools do not wait for a person.
Tell the model: "The command was stopped after 120 seconds without finishing. The last output was: ...". With the tail of the output, the model can usually see where it hung.
2. Memory: the OOM kill
Looks like: a process that dies with Killed, exit code 137, or a stack trace from a language runtime that could not allocate.
Contain it with: the box's memory limit. In a VM, the guest kernel's OOM killer picks a process inside the guest; the host and other runs are unaffected. Inside the box, you can make it fail earlier and more clearly with a lower limit on the agent's command than on the whole box, for example ulimit -v or prlimit --as for a single process, or a cgroup memory limit if the template sets one up.
Tell the model: be explicit. "The process was killed because it ran out of memory (the box has 4 GiB)." A model told that will often stream the file, use chunks or drop columns. A model shown only Killed tends to rewrite unrelated code.
3. Process count: the fork bomb
Looks like: the box becoming unresponsive, commands failing with fork: Resource temporarily unavailable, or a timeout on every subsequent command.
Contain it with: a process limit. ulimit -u caps processes for a user; a cgroup pids.max caps them for a group. Set it in the template or wrap the agent's commands:
prlimit --nproc=256 --as=3000000000 -- python3 main.py Fork bombs are rarer from models than loops, but test suites with unbounded worker pools and recursive scripts that call themselves produce the same effect. If a box has stopped responding after one, stop it and start a fresh one rather than trying to clean up.
Tell the model: "The command created too many processes and the sandbox stopped responding. It has been replaced with a fresh sandbox; files from before were not kept." Then make sure that is true.
4. Disk and output
Disk. A log written in a loop, a dataset downloaded twice, a build cache that grows. Looks like: No space left on device, which then breaks unrelated commands. Contain it with: the box's disk size, and cleaning temporary directories between steps. Check df -h /workspace before large downloads.
Output. A command that prints a million lines does not hurt the box, but it hurts everything downstream: your memory, your logs and the model's context window. Contain it with: a byte cap on what you keep and send to the model, with the head and tail kept and the middle cut. See truncating tool output for LLMs.
Keep the agent from thrashing
The worst outcome is not one failure; it is the agent retrying the same failing command twenty times. Two rules help:
- Report the cause, not just the output. Distinguish timeout, memory, process limit, disk and normal non-zero exit in the tool result. The model can only adapt to what it is told.
- Count retries. After the same class of failure two or three times in a row, stop and escalate to a person, or switch to a larger box for that workload if the failure is memory and the data is legitimate. See spend caps for AI agents.
What Sandscale reports
On Sandscale, sandboxes.run() returns a result rather than throwing when a command ends badly:
const result = await sandscale.sandboxes.run(box.id, ['python3', 'main.py'], { region: box.region, timeoutSeconds: 120, }); const cause = result.timedOut ? 'timeout' : result.oomKilled ? 'out_of_memory' : result.exitCode !== 0 ? 'error' : 'ok'; timedOut and oomKilled are set by the server, so you do not have to guess from exit codes. Each run is its own Firecracker microVM with its own kernel, and every box gets exactly the vCPUs and memory you picked (plus any burst you set), never more, so one run's exhaustion stays in its box. Set timeoutSeconds on create as the lifetime backstop. Process and per-command memory limits inside the box are yours to set in the template or the command, as above.
Originally published on the Sandscale blog.
