A wall of stacked colorful shipping containers, a visual for isolated E2B sandboxes that keep AI-generated code apart
Photo by Richard Williams on Unsplash · source · credits

E2B Sandbox Tutorial: Run AI-Generated Python Safely With Timeouts and No Internet

Also available in Hindi: यह लेख हिंदी में पढ़ें

Code written by a language model can delete files, read secrets or loop forever, so it should not run on your laptop or server. E2B runs it in isolated Linux VMs in the cloud, called sandboxes, controlled from a Python or JavaScript SDK. This guide shows a locked-down setup in Python. We checked it against the e2b 2.63.0 and e2b-code-interpreter 2.11.9 SDKs.

Set up

Create an E2B account (new accounts get $100 in one-time credits), copy your API key into .env as E2B_API_KEY, and install the SDK with pip install e2b-code-interpreter python-dotenv.

Step 1: A short-lived, offline sandbox

from dotenv import load_dotenv
load_dotenv()
from e2b_code_interpreter import Sandbox

llm_code = "print(sum(range(10)))"  # code from your model
with Sandbox.create(timeout=120, allow_internet_access=False) as sbx:
    ex = sbx.run_code(llm_code, timeout=30)
    if ex.error:
        print(ex.error.name, ex.error.value)
    else:
        print("".join(ex.logs.stdout))

Three limits protect you here. timeout=120 ends the whole sandbox after two minutes; Python uses seconds, while JavaScript's timeoutMs uses milliseconds. allow_internet_access=False blocks all outbound traffic. run_code(..., timeout=30) stops one runaway execution. The with block kills the sandbox when you are done.

E2B Python settings that limit AI-generated code
From the E2B docs and Python SDK. Illustration, not a screenshot.

Step 2: Move files in and out

Upload input with sbx.files.write("/home/user/data.csv", f), where f is a file opened in binary mode, and read results back with sbx.files.read(path). /home/user is the default working directory. Never copy API keys or .env files into a sandbox that runs untrusted code.

Step 3: Allow only what the code needs

If the code must call one API, deny everything and allow that host: network={"allow_out": ["api.example.com"], "deny_out": ["0.0.0.0/0"]}. Domain rules only work when all other traffic is denied, and E2B then allows its default DNS server, 8.8.8.8, so names resolve.

Know the limits

E2B Hobby and Pro plan limits compared
From E2B's Billing and limits docs. Illustration, not a screenshot.

Compute is billed per second while a sandbox runs. On Hobby a sandbox can run one hour continuously and you get 10 at once; Pro ($150 a month) allows 24 hours. Pausing keeps files and memory and resets that clock.

Related reading: AI agent hosting options, Neon read-only roles for agents, Google Agent Compute Engine and the E2B tool page.

Sources

FAQ

Is E2B free?

New accounts get $100 in one-time credits on the free Hobby plan. After that, usage is billed per second.

Does a sandbox have internet by default?

Yes. Set allow_internet_access=False, or use allow and deny lists.

Can I use JavaScript instead?

Yes. Install @e2b/code-interpreter and call runCode(); note that timeouts there are in milliseconds.

Does it work with any LLM?

Yes. Your app sends the model's code to the sandbox, and E2B's docs show examples for OpenAI, Anthropic, Mistral and others.

About the author

Suraj Kumar builds and edits AIUseFlow, a directory of AI tools for vibe coders, agent builders and creators. Guides like this one walk through a practical workflow with cited sources — not a news wire rewrite. How we rank tools.

Get the weekly stack

Rank movers and new AI tools for builders, once a week. Free, no spam, unsubscribe anytime.