Lightweight AI code execution sandbox. Run untrusted code safely with resource limits, timeout protection, and output capture.
npx sandbox-ai "print('hello world')" --lang pythonAI agents generate code. Running that code directly is dangerous. sandbox-ai provides isolated execution with:
- Resource limits (CPU, memory, time)
- No network access by default
- No filesystem access outside sandbox
- Automatic cleanup
- Structured output (stdout, stderr, exit code, timing)
# Run Python
npx sandbox-ai "import math; print(math.pi)" --lang python
# Run JavaScript
npx sandbox-ai "console.log(Array.from({length:10}, (_,i) => i*i))" --lang node
# Run with timeout
npx sandbox-ai "while True: pass" --lang python --timeout 5
# Run from file
npx sandbox-ai --file solution.py --lang python
# Get JSON output
npx sandbox-ai "print(42)" --lang python --json$ npx sandbox-ai "for i in range(5): print(f'fib({i})')" --lang python
sandbox-ai v1.0.0
Language: python
Timeout: 30s
─── Output ───
fib(0)
fib(1)
fib(2)
fib(3)
fib(4)
──────────────
✓ Exit code: 0
⏱ Time: 0.12s
{
"stdout": "fib(0)\nfib(1)\nfib(2)\nfib(3)\nfib(4)\n",
"stderr": "",
"exitCode": 0,
"elapsed_ms": 120,
"language": "python",
"timedOut": false
}| Language | Flag | Runtime |
|---|---|---|
| Python | --lang python |
python3 |
| JavaScript | --lang node |
node |
| TypeScript | --lang ts |
tsx / ts-node |
| Bash | --lang bash |
bash |
| Go | --lang go |
go run |
| Ruby | --lang ruby |
ruby |
| Feature | Default | Description |
|---|---|---|
| Timeout | 30s | Kill process after N seconds |
| No network | On | Block outbound connections |
| Temp dir | On | Run in isolated temp directory |
| Cleanup | On | Remove temp files after execution |
| Max output | 100KB | Truncate output at limit |
import { sandbox } from 'sandbox-ai';
const result = await sandbox({
code: 'print("hello")',
language: 'python',
timeout: 10000,
allowNetwork: false,
});
console.log(result.stdout); // "hello\n"
console.log(result.exitCode); // 0
console.log(result.elapsed_ms); // 45- AI agent code execution — Run LLM-generated code safely
- Code playground — Build web-based code runners
- Testing — Execute test cases in isolation
- Education — Safe student code execution
- CI/CD — Run arbitrary scripts with limits
MIT