Skip to content
GenAI Learn/Multi-Agent Systems & Execution
Browsing as a guest. Sign in to save your progress and earn XP as you complete chapters.

Sandboxed Code Execution for Agents

4 min read

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.

Defense in Depth for Code Execution
Safety = InputScreening ∧ SandboxIsolation ∧ ResourceLimits ∧ AuditLogging
No single layer is trusted alone. Screening catches obviously dangerous patterns before execution. Isolation contains whatever screening misses. Resource limits cap the damage even inside the sandbox. Logging makes every attempt reviewable after the fact.

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.

A generate, execute, observe error, fix loop, matching the sandboxed pattern

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.

Press Run: the screen stops the obvious attack and misses the disguised one

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.

Sign In to ScaleDojo

No comments yet. Be the first to start the thread!