DEV Community

Thomas Metz
Thomas Metz

Posted on

Running smolagents code in a sandbox without a monthly fee

Hugging Face's smolagents writes Python and runs it. By default that happens in a restricted local interpreter, and the docs are clear that no local Python sandbox is ever completely secure. The safer option is a remote sandbox: with executor_type="e2b" every code step runs in an isolated microVM instead of on your machine.

from smolagents import CodeAgent, LiteLLMModel

with CodeAgent(
    model=LiteLLMModel(model_id="anthropic/claude-sonnet-5-5"),
    tools=[],
    executor_type="e2b",
    additional_authorized_imports=["numpy"],
) as agent:
    print(agent.run("Use numpy to compute the standard deviation of the first 20 primes."))
Enter fullscreen mode Exit fullscreen mode

Packages in additional_authorized_imports are installed in the sandbox, and the with block removes the sandbox right after the run.

Pointing it somewhere else

The E2B SDK reads E2B_API_KEY and E2B_DOMAIN from the environment, and smolagents passes everything through to it. So the same code runs on a self-hosted E2B deployment or on any E2B-compatible service by setting one variable:

pip install "smolagents[e2b,litellm]"
export E2B_API_KEY=...
export E2B_DOMAIN=getrollouts.dev   # or your own E2B domain
Enter fullscreen mode Exit fullscreen mode

I tested this with smolagents 1.26, e2b 2.64 and e2b-code-interpreter 2.11: package installation, code execution and cleanup all work unchanged. If you see 'Sandbox' object has no attribute '_transport', upgrade the code interpreter: pip install -U "e2b-code-interpreter>=2.11" (more on that error). I also suggested documenting E2B_DOMAIN in the smolagents docs: https://github.com/huggingface/smolagents/issues/3381


Disclosure: getrollouts.dev is the E2B-compatible service I run (Firecracker microVMs in Switzerland, billed per second, sessions up to 24 hours without a subscription). Runnable example: 12_smolagents.py, guide: getrollouts.dev/guides/frameworks.

Top comments (0)