· via dev.to (home feed)
A 50-line Python sandbox runner for safely executing AI-agent code
A dev.to post by Neeraj Sharma walks through a sub-50-line Python runner that contains the predictable failure modes of agent-generated code, from secret leaks to runaway loops.

Agent code usually runs somewhere unsafe
When an AI agent generates Python and executes it, the question of where that code actually runs often goes unasked. According to a dev.to post by Neeraj Sharma, in many agent products the generated code runs on the same server as everything else, inheriting the application's environment variables — API keys, database URLs and network access included. One bad generation, or a prompt injection buried in a document the agent happened to read, is enough for the code to dump secrets, fill the disk or loop forever.
The post points to how the industry is responding: LangChain has made LangSmith Sandboxes generally available, describing them as hardware-virtualized microVMs with full kernel isolation, each carrying its own filesystem, shell and network boundary. The stated rationale is that containers share a kernel with the host, which is not enough for untrusted code. monday.com, the post notes, uses the service so its Sidekick assistant can write and run code.
A small runner paired with a test for each failure
Sharma's position is that you may not need a microVM on day one, but you do need a runner that treats generated code as untrusted. He publishes a runner in under 50 lines of Python and pairs it with tests covering the ways agent code predictably fails: reading secrets from the environment, accidental infinite loops, sleeping or spawning a sleeping child to pin the worker, allocating gigabytes of memory, writing large files to the working directory, and making network requests nobody approved.
In the demo output, every failure is contained while the normal case still works. The environment test reports no key visible even though the host process carried a fake live API key. The CPU loop is killed, the sleeper times out, the memory and disk attempts fail with errors, and the network request cannot connect. The author also verified that a child process started by the sleeping test was gone after the run.
How the containment works
Several mechanisms combine, each explained in the post:
- Environment allow-listing: the child receives three variables and nothing else. The post warns against trying to filter sensitive names out of the host environment, because you will miss one.
- Python's isolated mode (-I), which ignores PYTHON* variables and user site-packages, so leftover host configuration cannot steer the code.
- Kernel-enforced resource limits (CPU seconds, address space, file size) applied in the child through a pre-exec hook, which the code cannot catch its way out of.
- A wall-clock timeout separate from the CPU limit, because a CPU cap does nothing against sleeping code. The child gets its own process group so the timeout kills everything it spawned, not just the parent.
- No network by default, using unshare with an empty network namespace when available. The post cautions that if the distribution blocks unprivileged user namespaces, the runner silently falls back to allowing network access, so operators should verify it works on their hosts.
- A fresh temporary working directory per run, deleted afterwards, plus capped stdout and stderr so a chatty program cannot flood logs or the model's next context window.
Where the line is
The post is explicit about the limits: the runner shares the host kernel, can still read world-readable files on the machine, and a kernel exploit escapes it. That is acceptable for an internal tool where your own agent writes code from trusted instructions, and unacceptable for a product where any user can make the agent run arbitrary code. At that point, Sharma argues, execution should move to a microVM — Firecracker-style, or a managed service such as LangSmith Sandboxes, E2B or Modal — while keeping the same principles on top: allow-listed environment, resource limits, wall-clock kills, network denied unless a specific domain is approved, and credentials injected by a proxy at the network layer rather than handed to the code.
The post closes with a checklist: allow-list environment variables; set CPU, memory, file-size and wall-clock limits that kill the process group; deny network by default; give each run a throwaway directory; cap output before it returns to the model; log every execution with its code, limits and result so incidents can be replayed; and move to microVMs the day untrusted users can trigger execution.
Why it matters
Code-generating agents are becoming a standard product feature, and the execution layer determines whether the feature is safe to expose to customers. This post offers a graduated path rather than an all-or-nothing choice: contain the cheap, predictable failures first, and escalate to hardware isolation exactly when trust in the code's origin drops. The test-per-failure structure also makes the security claims verifiable rather than aspirational — a template any team shipping an agent with a code tool can copy today.
- #ai-agents
- #python
- #security
- #sandboxing
- #code-execution