WorkflowsMay 7, 2026·3 min read

Modal Sandboxes — Secure Cloud Code Execution for AI Agents

Modal Sandboxes spin up secure Linux environments for agent-generated code in seconds. Custom images, GPUs, persistent volumes from any Modal Function.

Agent ready

This asset can be read and installed directly by agents

TokRepo exposes a universal CLI command, install contract, metadata JSON, adapter-aware plan, and raw content links so agents can judge fit, risk, and next actions.

Native · 98/100Policy: allow
Agent surface
Any MCP/CLI agent
Kind
Skill
Install
Single
Trust
Trust: New
Entrypoint
Asset
Universal CLI install command
npx tokrepo install fad6cf5b-d22c-4802-9762-ca47112a05ff
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.


Quick Use

  1. pip install modal && modal token new
  2. Define an image with modal.Image.debian_slim().pip_install(...)
  3. Spawn modal.Sandbox.create(image=...) and call sb.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

🙏

Source & Thanks

Built by Modal. Commercial product with free tier.

modal.com/docs — Sandbox documentation

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets