
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.
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
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
- Running your first Sandbox, E2B docs
- Sandbox lifecycle, E2B docs
- Internet access, E2B docs
- Billing and limits, E2B docs
- E2B Code Interpreter README, GitHub
- e2b-code-interpreter package, PyPI
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.