Give your agent a real computer: a Firecracker sandbox where it can run Python, shell commands and keep files between steps. Everything below uses the official E2B packages pointed at getrollouts.dev.
pip install langchain langgraph e2b-code-interpreter # code tool pip install deepagents langchain-e2b # Deep Agents backend export E2B_API_KEY=e2b_... # from getrollouts.dev/portal export E2B_DOMAIN=getrollouts.dev export ANTHROPIC_API_KEY=... # or the key of your model provider
Get the API key in the portal (free trial, no card). The model provider key is only needed for the agent itself.
One sandbox per conversation keeps variables, installed packages and files between tool calls, like a notebook.
from e2b_code_interpreter import Sandbox
from langchain.agents import create_agent
from langchain_core.tools import tool
sbx = Sandbox.create(timeout=3600) # one sandbox per conversation
@tool
def run_python(code: str) -> str:
"""Run Python code in a persistent sandbox. Returns stdout, results and errors."""
ex = sbx.run_code(code)
if ex.error:
return f"{ex.error.name}: {ex.error.value}\n{ex.error.traceback}"
out = "".join(ex.logs.stdout)
res = "\n".join(r.text for r in ex.results if r.text)
return (out + res) or "(no output)"
agent = create_agent("anthropic:claude-sonnet-5-5", tools=[run_python])
reply = agent.invoke({"messages": [{"role": "user",
"content": "Compute the 30th Fibonacci number and the mean of the first 30."}]})
print(reply["messages"][-1].content)
sbx.kill()
The same run_python tool works in LangGraph, for example with langgraph.prebuilt.create_react_agent,
and in any other framework that accepts LangChain tools.
langchain-e2b provides the official E2B backend for Deep Agents. With it, the agent's file tools and its
execute tool run inside the sandbox instead of on your machine.
from deepagents import create_deep_agent
from e2b import Sandbox
from langchain_e2b import E2BSandbox
sbx = Sandbox.create(timeout=3600) # template "base": shell, Python, git
agent = create_deep_agent(
model="anthropic:claude-sonnet-5-5",
backend=E2BSandbox(sandbox=sbx),
system_prompt="You are a coding agent. Work in /home/user and test what you write.",
)
result = agent.invoke({"messages": [{"role": "user",
"content": "Write fib.py with a fib(n) function and a test, then run the test."}]})
print(result["messages"][-1].content)
sbx.kill()
Sandboxes live up to 24 hours per session on paid accounts. For agents that wait for a human or run over several days, pause the sandbox between turns: memory and files are kept, billing stops, and resume takes under a second.
sandbox_id = sbx.sandbox_id sbx.pause() # between turns: memory and files kept, billing stops sbx = Sandbox.connect(sandbox_id) # next turn: resumes in under a second sbx.set_timeout(3600)
Size: the code interpreter default is 2 vCPU / 2 GiB. For pandas on larger files use
Sandbox.create("code-interpreter-8g") (2 vCPU, 8 GiB) or "code-interpreter-16g" (4 vCPU, 16 GiB).
Network: sandboxes have internet access for pip install and APIs; outbound mail ports and internal networks are blocked.
Previews: if the agent starts a web server, sbx.get_host(8000) returns a public HTTPS URL for it.
Clean up: call sbx.kill() when the conversation ends, or set a short timeout so forgotten sandboxes stop by themselves.
Sandbox side tested with langchain 1.5, langgraph 1.2, deepagents 0.7, langchain-e2b 0.0.7, e2b 2.66 and e2b-code-interpreter 2.11. Runnable versions of these snippets: github.com/getrollouts/examples. More: docs, comparison with E2B.