Running Untrusted Code Safely: A Guide to E2B Sandboxes

Don't risk your local machine with untrusted LLM code. Use E2B cloud sandboxes to provide secure, isolated execution environments for your AI agents.

When building autonomous AI agents, you face a critical security dilemma: how do you execute code generated by an LLM without compromising your local machine or production environment? Running untrusted, model-generated scripts directly on your hardware is a massive security risk.

This is where E2B becomes essential. It provides isolated cloud sandboxes specifically designed for running the code that a model writes. Instead of execution happening on your laptop, E2B handles it in a secure, remote environment via a clean SDK.

The Security Gap in AI Agents

A truly autonomous agent needs the ability to run code to solve complex problems, such as data analysis or file manipulation. However, as you explore advanced frameworks like smolagents, the risk of executing malicious or broken code increases.

E2B acts as the natural partner to a code-agent; while your agent decides what logic to execute, E2B provides the sandboxed execution layer that keeps your primary infrastructure safe. It is particularly useful for handling untrusted input where you cannot verify the safety of the code beforehand.

What is E2B?

security, cloud, code

E2B is an open-source infrastructure that allows you to run AI-generated code in secure, isolated sandboxes in the cloud. Using their JavaScript or Python SDKs, you can spin up environments in seconds.

  1. Isolation: Code runs in a dedicated cloud container, far from your production box.
  2. Integration: A clean SDK drops sandboxed execution into an agent in just a few lines of code via the code-interpreter repository.
  3. Maintenance: The project is actively maintained under the Apache 2.0 license.

Implementation Guide

security, cloud, code

Setting up your first sandbox is straightforward. Follow these steps to get running with Python or JavaScript/TypeScript.

Step 1: Install the SDK

# For JavaScript/TypeScript
npm i @e2b/code-interpreter

# For Python
pip install e2b-code-interpreter

Step 2: Configure Environment Variables

After signing up for an E2B account, retrieve your API key and set it as an environment variable to authorize your requests:

E2B_API_KEY=e2b_***

Step 3: Execute Code

Here is a quick example of how to trigger code execution within a sandbox using Python. This demonstrates the ease of sending instructions and receiving processed results:

from e2b_code_interpreter import Sandbox

with Sandbox.create() as sandbox:
    sandbox.run_code("x = 1")
    execution = sandbox.run_code("x+=1; x")
    print(execution.text)  # outputs 2

Evaluating E2B: Pros and Cons

While E2B is powerful, it is important to weigh the benefits of managed infrastructure against the costs involved.

FeatureBenefit / Constraint
SecurityFull isolation from your local or production environment.
Ease of UseExtremely clean SDK for rapid agent development.
InfrastructureManaged cloud service; the free tier has limits.
DeploymentSelf-hosting the sandbox stack is non-trivial.

For more advanced implementation details, you should consult the official documentation or explore the E2B cookbook for inspiration from different LLM frameworks. If you are looking for a production-ready setup, check out this verified E2B workflow with CI proof.

Ready to secure your agents? Start by installing the SDK today and move your code execution away from your local machine.

security, cloud, code