Your AI Agent Wrote Code. Where Does It Run? A Sandbox Runner in Under 50 Lines of Python
Your AI agent just wrote some Python and wants to run it. Where does that code actually execute? For a lot of agent products the honest answer is "on the same server as everything else, with the same environment variables". That means the same API keys, the same database URL and the same network access as your app. One bad generation, or one prompt injection hidden in a document the agent read,…
A sandbox runner for untrusted AI-generated code can be implemented in under 50 lines of Python. This runner isolates the code from the host environment and enforces strict limits to prevent potential security breaches. The code defines a set of limits for CPU usage, memory usage, file size, wall clock time, and output size. It also sets a safe environment with restricted PATH, LANG, and HOME variables.
The runner uses the subprocess module to execute the agent's code in a separate process group, ensuring that any timeouts or errors are properly handled. By limiting the resources and isolating the code, this runner provides a basic level of security for running potentially malicious AI-generated code.
Rewritten by urgent.news; may contain errors — check the original.