Esta página se muestra en inglés. Una traducción al español está en curso.
WorkflowsMay 7, 2026·3 min de lectura

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.

Listo para agents

Instalación lista para agent

Este activo puede instalarse después de elegir el runtime, revisar el plan y ejecutar el comando correspondiente.

Native · 98/100Política: permitir
Superficie agent
Cualquier agent MCP/CLI
Tipo
Skill
Instalación
Single
Confianza
Confianza: Community
Entrada
Asset
Comando de instalación directa
npx -y tokrepo@latest install fad6cf5b-d22c-4802-9762-ca47112a05ff --target codex

Ejecutar después de confirmar el plan con dry-run.

Introducción

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

🙏

Fuente y agradecimientos

Built by Modal. Commercial product with free tier.

modal.com/docs — Sandbox documentation

Discusión

Inicia sesión para unirte a la discusión.
Aún no hay comentarios. Sé el primero en compartir tus ideas.

Activos relacionados