{ } ð¦ Code Sandboxes
Code Sandboxes is a Python package for creating safe, isolated environments where AI systems can write, run, and test code without affecting the real world or the user's device.
Package Scopeâ
Code Sandboxes is the execution layer in the Datalayer AI stack:
This section clarifies what the package owns versus what is delegated to adjacent layers.
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â agent-runtimes â
â (Agent hosting, protocols, UI) â
ââââââââââââââââââââââââââââ¬âââââââââââââââââââââââââââââââââââ¤
â agent-codemode â agent-skills â
â (discovery, codegen) â (skills management) â
ââââââââââââââââââââââââââââ´âââââââââââââââââââââââââââââââââââ¤
â code-sandboxes â âââ You are here
â (Safe code execution environment) â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
Responsibilities:
- â Execute Python/shell code safely in isolated environments
- â Provide filesystem operations (read, write, list, upload, download)
- â Run shell commands with streaming output
- â Manage sandbox lifecycle (start, stop, snapshot)
- â Support multiple execution variants through one API
Not Responsible For:
- â MCP tool discovery or binding generation (â agent-codemode)
- â Skill management and composition (â agent-skills)
- â Agent protocols or UI components (â agent-runtimes)
Key Featuresâ
- ð Secure Isolation: Run untrusted code safely in sandboxed environments
- ð Python Code Execution: Execute Python code with streaming output and rich results
- ð Filesystem Operations: Read, write, list, upload, and download files
- ð» Command Execution: Run shell commands with streaming support
- ð§ Unified Client API: Use
CodeSandboxClientfor variant-agnostic execution and streaming - ð Detailed Status Reporting: Distinguish between infrastructure and code-level failures
- ð¯ Pydantic Models: Type-safe models with automatic validation and JSON serialization
- â¡ Multiple Backends: eval, Docker, Jupyter, Monty, Kaggle, Colab, Modal, and Datalayer
- ð State Persistence: Maintain variables and context between executions
- ð Rich Output: Support for text, HTML, images, and structured data
- ð¸ Snapshots: Save and restore sandbox state
- ð GPU Support: Access GPU compute for ML workloads
Sandbox Variantsâ
Code Sandboxes supports these execution variants:
| Variant | Isolation Level | Best For |
|---|---|---|
eval | None (Python exec) | Development, testing |
monty | In-process secure interpreter | Safe, fast LLM snippets |
docker | Container | Isolated execution |
jupyter | Process (Jupyter kernel) | Persistent notebook-style state |
kaggle | Managed notebook runtime | Interactive and batch runs |
colab | Managed notebook runtime | Interactive Colab-connected runs |
modal | Managed container runtime | Ephemeral compute tasks |
datalayer | Managed VM/runtime | Production and GPU workloads |
Quick Startâ
pip install code-sandboxes
Basic Usageâ
from code_sandboxes import Sandbox
with Sandbox.create() as sandbox:
result = sandbox.run_code("print('Hello from the sandbox!')")
print(result.stdout) # Hello from the sandbox!
Execution Status Reportingâ
Code Sandboxes provides detailed status information for each execution:
result = sandbox.run_code("x = 1 / 0")
# Check infrastructure-level success
if not result.execution_ok:
print(f"Sandbox failed: {result.execution_error}")
# Check explicit process exit (sys.exit)
elif result.exit_code not in (None, 0):
print(f"Process exited with code: {result.exit_code}")
# Check code-level error (Python exception)
elif result.code_error:
print(f"Python error: {result.code_error.name}: {result.code_error.value}")
print(f"Traceback: {result.code_error.traceback}")
# Success!
else:
print(f"Result: {result.text}")
print(f"Duration: {result.duration:.2f}s")
# Convenience property
if result.success:
print("Everything worked perfectly!")
Integration with Other Packagesâ
With Agent Codemodeâ
Code Sandboxes is used by agent-codemode to execute tool composition code:
from code_sandboxes import Sandbox
from agent_codemode import CodeModeExecutor, ToolRegistry
# agent-codemode uses code-sandboxes internally
executor = CodeModeExecutor(registry, sandbox_variant="datalayer")
With Agent Skillsâ
Agent Skills uses Code Sandboxes to execute skill scripts:
from code_sandboxes import EvalSandbox
from agent_skills import SandboxExecutor
sandbox = EvalSandbox()
executor = SandboxExecutor(sandbox)
Developmentâ
Run the local test suite:
pytest tests/
Default local test suite environment variables:
- None for core local suites (
eval, factory, model, and local Jupyter tests).
Optional integration test variables:
DATALAYER_API_KEYfor Datalayer runtime smoke tests.DATALAYER_RUN_URLfor a custom Datalayer runtime URL.DATALAYER_ENVIRONMENTfor Datalayer environment override.MODAL_TOKEN_IDandMODAL_TOKEN_SECRETfor Modal integration tests.
CI Workflowsâ
This repository uses a reusable GitHub Actions workflow at
.github/workflows/reusable-python.yml.
Workflows that call it:
.github/workflows/build.yml.github/workflows/py-tests.yml.github/workflows/py-code-style.yml.github/workflows/py-typing.yml
Reusable workflow inputs:
python-versioninstall-system-depsinstall-extrasextra-packagesrun-teststest-commandrun-mypymypy-targetrun-pre-commit
Learn Moreâ
Code Sandboxes
123; } ð¦ Code Sandboxes
Sandboxes
A sandbox is an isolated environment where code can be executed safely. Code Sandboxes provides a unified API across different execution backends.
Installation
Basic Installation
Examples
Run the examples from the examples/ directory in this repository.
Comparison
This page compares Code Sandboxes with other popular code execution platforms: E2B and Modal.
API Reference
Sandbox Class
CLI
Code Sandboxes provides a Typer-based CLI that launches an interactive REPL