Sandboxed Code Execution for Agents
You'll learn to
- -Explain why code written by a model must run in an isolated sandbox, not in your main process
- -See why input screening is only a first layer, with a demo of a disguised call slipping past it
- -Run a generate, execute, observe error, fix loop that has its own attempt cap
A data scientist wants to say "analyze this CSV and make some charts" and have an agent write and run Python to do it. That is enormously useful, and described plainly also a little terrifying. An LLM generating code that then runs with real system access could delete files, leak data or run arbitrary commands, through an honest mistake or an input crafted to trick it.
An Analogy: The Laboratory Fume Cupboard
Chemists do not trust every experiment to stay quiet. They do risky work inside a fume cupboard, a sealed hood with its own ventilation, so that if something goes wrong, the room is safe. They also check labels before mixing chemicals, but nobody relies on the label check alone. A sandbox is the fume cupboard for code. Screening the code first is the label check.
The Sandbox Is the Actual Safety Boundary
The fix is not to try to make the model perfectly trustworthy. It is to run whatever code it writes inside an isolated environment that cannot do real damage even if the code is malicious: no network access, strictly limited memory and CPU, and only an approved set of safe packages. It is the same sandboxing idea behind this course's own runnable code blocks, applied to agent-written code instead of pre-written examples.
The Loop Is Iterative, Not One-Shot
Generated code rarely works on the first try, and that is fine by design. Generate code, execute it in the sandbox, and if it fails, feed the actual error message back to the model as new context and let it write a fix. Most real tasks succeed within two or three attempts. The loop needs its own cap too, the same lesson as ReAct's max_iterations. An agent that never gives up fixing broken code burns tokens as boundlessly as one that never stops researching.
Why Screening Alone Is Not Enough
Input screening, which looks for dangerous patterns such as os.system calls, subprocess spawning or raw network requests, catches the obviously bad cases before execution starts. The demo below shows its limit. It checks three snippets, and one disguises its dangerous call by building the name from pieces. Nothing in this demo runs the code; the strings are only inspected.
Screening is not a substitute for the sandbox. It can be fooled by an obfuscated or unusual pattern it was not written for. The sandbox is what holds when screening fails, which is why both layers matter together and neither alone.
Interview Signal is part of Pro
See a real weak answer next to a real strong one for this exact topic.
Quiz is part of Pro
Test what you just read with a short quiz, and bank the XP.
Discussion0
Join the Discussion
Sign in to leave comments, reply to others, or like insights.
No comments yet. Be the first to start the thread!