Secure Code Execution

The power of code agents comes with a sharp edge: you are executing code written by an LLM, and an LLM can be wrong, or manipulated into writing something harmful. Running that code unsandboxed is one of the most dangerous things you can do in an application, so sandboxing isn't optional for code agents — it's the price of admission.

The previous posts made the case for code agents. This one covers the cost that comes with them: security. Executing model-written code is genuinely dangerous, and smolagents (and anyone using code agents) must run that code in a sandbox. This post explains the risk, why it’s serious, and how code agents are made safe — because the code-agent advantage is only usable if you handle its security, and getting this wrong is catastrophic.

The risk: executing model-written code

The code-agent approach means the framework executes Python code written by the LLM. That is inherently risky, for reasons that should give you pause:

Put bluntly: running LLM-written code unsandboxed, in an environment with real access, is one of the most dangerous things you can do. It’s the difference between an agent that can only call your approved tools and one that can execute anything. The very generality that gives code agents their power (the last posts) is the source of the danger — you can’t have arbitrary-code expressiveness without arbitrary-code risk. This is the non-negotiable cost of the approach.

The solution: sandboxing

The answer is to execute the model-written code in a sandbox — an isolated, restricted environment where code runs without access to anything it could harm. Sandboxing is what makes code agents safe enough to use, and it’s mandatory, not optional, for any code agent processing anything less than fully trusted input. The idea:

smolagents supports secure execution options — running code in a restricted local interpreter with limits, or (more securely) in isolated sandboxes (containerized or remote execution environments) — so that the code an agent writes runs safely. The strong guidance: for anything beyond trusted, controlled input, use a proper sandbox (an isolated container or remote execution service), not just a lightly-restricted local interpreter, because the stakes of a sandbox escape are severe. The more untrusted the input your agent processes, the stronger the isolation you need.

Sandboxing as the enabler, not just a caveat

It’s worth reframing sandboxing not as a burden but as the enabler of the whole code-agent approach:

So the mental model is: code agents trade JSON’s inherent safety (a JSON tool call can only do pre-defined things) for code’s expressiveness, and recover safety through sandboxing (isolate the execution so arbitrary code is contained). The expressiveness lives in the code; the safety lives in the sandbox. Both are necessary, and treating sandboxing as integral to the approach — not a bolt-on — is what makes code agents responsible to deploy.

The security discipline for code agents

Practical guidance, tying it to the broader AI-security concerns:

Secure code execution is the price of the code-agent power, and it’s a price worth paying because it’s payable — sandboxing is established technology that contains the risk. Handle it well and code agents are both powerful and safe; ignore it and you’ve built one of the most dangerous things in software. The next post covers tools — how code agents are equipped with capabilities to call from within their code.

Key takeaways

Further reading

Sources & References