Quick Use
pip install modal && modal token new- Define an image with
modal.Image.debian_slim().pip_install(...) - Spawn
modal.Sandbox.create(image=...)and callsb.exec(...)
Intro
Modal Sandboxes are isolated Linux environments you spin up programmatically — perfect for running agent-generated code, fine-tuning, or untrusted user code. Custom Docker-style images, optional GPUs, persistent volumes, sub-second cold start. Best for: AI agents that need to execute generated Python / shell / Node code safely. Works with: Modal SDK (Python). Setup time: 5 minutes.
Spawn a sandbox
import modal
app = modal.App("agent-sandbox")
image = modal.Image.debian_slim(python_version="3.12").pip_install("pandas", "numpy", "matplotlib")
@app.function(image=image)
def run_agent_code(code: str):
sb = modal.Sandbox.create(
image=image,
cpu=2.0,
memory=4096,
timeout=300,
app=app,
)
process = sb.exec("python", "-c", code)
stdout = process.stdout.read()
stderr = process.stderr.read()
sb.terminate()
return {"stdout": stdout, "stderr": stderr, "exit_code": process.returncode}Sandbox with persistent storage
volume = modal.Volume.from_name("agent-workspace", create_if_missing=True)
sb = modal.Sandbox.create(
image=image,
volumes={"/workspace": volume},
workdir="/workspace",
)
# Files written to /workspace persist across sandboxes
sb.exec("python", "fetch_data.py")
volume.commit()
# Later sandbox reads the same files
sb2 = modal.Sandbox.create(volumes={"/workspace": volume})
sb2.exec("python", "process_data.py")GPU sandbox
sb = modal.Sandbox.create(
image=image,
gpu="A10G", # or "H100", "T4", etc
timeout=3600,
)
sb.exec("python", "train.py")Why use Modal Sandboxes vs Docker locally
- Spin up in <1s vs Docker's tens of seconds
- Pre-built images cached at the platform level
- Pay per second of execution
- Same SDK works for Functions, Sandboxes, Volumes, GPUs
- Built-in monitoring, logs, dashboards
FAQ
Q: Is Modal free? A: Modal has a free tier ($30/mo platform credit). Beyond that you pay per second of compute (CPU + memory + GPU). No platform fee — just resources used.
Q: How is this different from E2B? A: E2B and Modal Sandboxes overlap heavily. E2B optimizes for sandbox-as-a-product (the fastest cold start, isolated networking by default). Modal optimizes for the bigger Modal platform (Functions, Volumes, queues, GPUs). Both work for agent code execution; pick by ecosystem fit.
Q: Can I install custom packages in the sandbox?
A: Yes — define an image with modal.Image.debian_slim().pip_install(...) or apt_install(...) or dockerfile_commands(...). The image is built once and cached for fast cold starts.
Source & Thanks
Built by Modal. Commercial product with free tier.
modal.com/docs — Sandbox documentation