Skip to content

nhevers/agent-sandbox

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

agent-sandbox

Sandboxed code execution for AI agents. Run untrusted code safely with resource limits and output capture.

Installation

pip install -r requirements.txt

Quick Start

from agentsandbox import Sandbox

sandbox = Sandbox()

result = sandbox.execute("""
x = 5 + 3
print(f"Result: {x}")
""")

print(result.stdout)  # "Result: 8"

Features

  • Isolated execution environment
  • Configurable timeout and memory limits
  • Output capture (stdout, stderr)
  • Support for multiple languages
  • Docker-based isolation option

API Reference

Sandbox

sandbox = Sandbox(
    timeout: int = 30,
    memory_limit: str = "256m",
    use_docker: bool = False
)

Methods

  • execute(code: str, language: str = "python") -> Result - Execute code
  • execute_file(path: str) -> Result - Execute file
  • install_package(package: str) - Install package in sandbox

Result

result.stdout      # Standard output
result.stderr      # Standard error
result.exit_code   # Exit code
result.duration    # Execution time
result.error       # Exception if any

Configuration

sandbox = Sandbox(
    timeout=60,
    memory_limit="512m",
    allowed_imports=["math", "json", "re"]
)

Examples

Basic execution

sandbox = Sandbox(timeout=10)

result = sandbox.execute("""
import math
print(math.sqrt(16))
""")

if result.exit_code == 0:
    print(result.stdout)
else:
    print(f"Error: {result.stderr}")

With Docker isolation

sandbox = Sandbox(use_docker=True)

result = sandbox.execute("""
import os
print(os.getcwd())
""")
# Runs in isolated container

Handling timeouts

sandbox = Sandbox(timeout=5)

result = sandbox.execute("""
import time
time.sleep(100)  # Will timeout
""")

if result.error:
    print(f"Execution failed: {result.error}")

License

MIT

About

Sandboxed code execution for AI agents

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages