llm-sandbox

Lightweight and portable LLM sandbox runtime (code interpreter) Python library.

https://github.com/vndee/llm-sandbox

Science Score: 44.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
    Found CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (15.2%) to scientific vocabulary

Keywords

code-generation code-interpreter large-language-models llm-sandbox

Keywords from Contributors

interactive mesh interpretability profiles sequences generic projection standardization optim embedded
Last synced: 4 months ago · JSON representation ·

Repository

Lightweight and portable LLM sandbox runtime (code interpreter) Python library.

Basic Info
Statistics
  • Stars: 440
  • Watchers: 4
  • Forks: 43
  • Open Issues: 10
  • Releases: 19
Topics
code-generation code-interpreter large-language-models llm-sandbox
Created over 1 year ago · Last pushed 5 months ago
Metadata Files
Readme Changelog Contributing License Code of conduct Citation Security

README.md

LLM Sandbox

Securely Execute LLM-Generated Code with Ease

SonarQube Cloud

Quality Gate Status PyPI Downloads Release Build status CodeFactor codecov

LLM Sandbox is a lightweight and portable sandbox environment designed to run Large Language Model (LLM) generated code in a safe and isolated mode. It provides a secure execution environment for AI-generated code while offering flexibility in container backends and comprehensive language support, simplifying the process of running code generated by LLMs.

Documentation: https://vndee.github.io/llm-sandbox/

New: This project now supports the Model Context Protocol (MCP) server, which allows your MCP clients (e.g. Claude Desktop) to run code generated by LLMs in a secure sandbox environment.

🚀 Key Features

🛡️ Security First

  • Isolated Execution: Code runs in isolated containers with no access to host system
  • Security Policies: Define custom security policies to control code execution
  • Resource Limits: Set CPU, memory, and execution time limits
  • Network Isolation: Control network access for sandboxed code

🏗️ Flexible Container Backends

  • Docker: Most popular and widely supported option
  • Kubernetes: Enterprise-grade orchestration for scalable deployments
  • Podman: Rootless containers for enhanced security

🌐 Multi-Language Support

Execute code in multiple programming languages with automatic dependency management: - Python - Full ecosystem support with pip packages - JavaScript/Node.js - npm package installation - Java - Maven and Gradle dependency management - C++ - Compilation and execution - Go - Module support and compilation - R - Statistical computing and data analysis with CRAN packages

🔌 LLM Framework Integration

Seamlessly integrate with popular LLM frameworks such as LangChain, LangGraph, LlamaIndex, OpenAI, and more.

📊 Advanced Features

  • Artifact Extraction: Automatically capture plots and visualizations
  • Library Management: Install dependencies on-the-fly
  • File Operations: Copy files to/from sandbox environments
  • Custom Images: Use your own container images
  • Fast Production Mode: Skip environment setup for faster container startup

📦 Installation

Basic Installation

bash pip install llm-sandbox

With Specific Backend Support

```bash

For Docker support (most common)

pip install 'llm-sandbox[docker]'

For Kubernetes support

pip install 'llm-sandbox[k8s]'

For Podman support

pip install 'llm-sandbox[podman]'

All backends

pip install 'llm-sandbox[docker,k8s,podman]' ```

Development Installation

bash git clone https://github.com/vndee/llm-sandbox.git cd llm-sandbox pip install -e '.[dev]'

🏃‍♂️ Quick Start

Basic Usage

```python from llm_sandbox import SandboxSession

Create and use a sandbox session

with SandboxSession(lang="python") as session: result = session.run(""" print("Hello from LLM Sandbox!") print("I'm running in a secure container.") """) print(result.stdout) ```

Installing Libraries

```python from llm_sandbox import SandboxSession

with SandboxSession(lang="python") as session: result = session.run(""" import numpy as np

Create an array

arr = np.array([1, 2, 3, 4, 5]) print(f"Array: {arr}") print(f"Mean: {np.mean(arr)}") """, libraries=["numpy"])

print(result.stdout)

```

Multi-Language Support

JavaScript

```python with SandboxSession(lang="javascript") as session: result = session.run(""" const greeting = "Hello from Node.js!"; console.log(greeting);

const axios = require('axios'); console.log("Axios loaded successfully!"); """, libraries=["axios"]) ```

Java

python with SandboxSession(lang="java") as session: result = session.run(""" public class HelloWorld { public static void main(String[] args) { System.out.println("Hello from Java!"); } } """)

C++

```python with SandboxSession(lang="cpp") as session: result = session.run("""

include

int main() { std::cout << "Hello from C++!" << std::endl; return 0; } """) ```

Go

```python with SandboxSession(lang="go") as session: result = session.run(""" package main import "fmt"

func main() { fmt.Println("Hello from Go!") } """) ```

R

```python with SandboxSession( lang="r", image="ghcr.io/vndee/sandbox-r-451-bullseye", verbose=True, ) as session: result = session.run( """

Basic R operations

print("=== Basic R Demo ===")

Create some data

numbers <- c(1, 2, 3, 4, 5, 10, 15, 20) print(paste("Numbers:", paste(numbers, collapse=", ")))

Basic statistics

print(paste("Mean:", mean(numbers))) print(paste("Median:", median(numbers))) print(paste("Standard Deviation:", sd(numbers)))

Work with data frames

df <- data.frame( name = c("Alice", "Bob", "Charlie", "Diana"), age = c(25, 30, 35, 28), score = c(85, 92, 78, 96) )

print("=== Data Frame ===") print(df)

Calculate average score

avgscore <- mean(df$score) print(paste("Average Score:", avgscore)) """ ) ```

Capturing Plots and Visualizations

Python Plots

```python from llm_sandbox import ArtifactSandboxSession import base64 from pathlib import Path

with ArtifactSandboxSession(lang="python") as session: result = session.run(""" import matplotlib.pyplot as plt import numpy as np

x = np.linspace(0, 10, 100) y = np.sin(x)

plt.figure(figsize=(10, 6)) plt.plot(x, y) plt.title("Sine Wave") plt.xlabel("x") plt.ylabel("sin(x)") plt.grid(True) plt.savefig("sinewave.png", dpi=150, bboxinches="tight") plt.show() """, libraries=["matplotlib", "numpy"])

# Extract the generated plots
print(f"Generated {len(result.plots)} plots")

# Save plots to files
for i, plot in enumerate(result.plots):
    plot_path = Path(f"plot_{i + 1}.{plot.format.value}")
    with plot_path.open("wb") as f:
        f.write(base64.b64decode(plot.content_base64))

```

R Plots

```python from llm_sandbox import ArtifactSandboxSession import base64 from pathlib import Path

with ArtifactSandboxSession(lang="r") as session: result = session.run(""" library(ggplot2)

Create sample data

data <- data.frame( x = rnorm(100), y = rnorm(100) )

Create ggplot2 visualization

p <- ggplot(data, aes(x = x, y = y)) + geompoint(alpha = 0.6) + geomsmooth(method = "lm", se = FALSE) + labs(title = "Scatter Plot with Trend Line", x = "X values", y = "Y values") + theme_minimal()

print(p)

Base R plot

hist(data$x, main = "Distribution of X", xlab = "X values", col = "lightblue", breaks = 20) """, libraries=["ggplot2"])

# Extract the generated plots
print(f"Generated {len(result.plots)} R plots")

# Save plots to files
for i, plot in enumerate(result.plots):
    plot_path = Path(f"r_plot_{i + 1}.{plot.format.value}")
    with plot_path.open("wb") as f:
        f.write(base64.b64decode(plot.content_base64))

```

🔧 Configuration

Basic Configuration

```python from llm_sandbox import SandboxSession

Create a new sandbox session

with SandboxSession(image="python:3.9.19-bullseye", keep_template=True, lang="python") as session: result = session.run("print('Hello, World!')") print(result)

With custom Dockerfile

with SandboxSession(dockerfile="Dockerfile", keep_template=True, lang="python") as session: result = session.run("print('Hello, World!')") print(result)

Or default image

with SandboxSession(lang="python", keep_template=True) as session: result = session.run("print('Hello, World!')") print(result) ```

LLM Sandbox also supports copying files between the host and the sandbox:

```python from llm_sandbox import SandboxSession

with SandboxSession(lang="python", keeptemplate=True) as session: # Copy a file from the host to the sandbox session.copyto_runtime("test.py", "/sandbox/test.py")

# Run the copied Python code in the sandbox
result = session.execute_command("python /sandbox/test.py")
print(result)

# Copy a file from the sandbox to the host
session.copy_from_runtime("/sandbox/output.txt", "output.txt")

```

Custom runtime configs

```python from llm_sandbox import SandboxSession

podmanifest = { "apiVersion": "v1", "kind": "Pod", "metadata": { "name": "test", "namespace": "test", "labels": {"app": "sandbox"}, }, "spec": { "containers": [ { "name": "sandbox-container", "image": "test", "tty": True, "volumeMounts": { "name": "tmp", "mountPath": "/tmp", }, } ], "volumes": [{"name": "tmp", "emptyDir": {"sizeLimit": "5Gi"}}], }, } with SandboxSession( backend="kubernetes", image="python:3.9.19-bullseye", dockerfile=None, lang="python", keeptemplate=False, verbose=False, podmanifest=podmanifest, ) as session: result = session.run("print('Hello, World!')") print(result) ```

Remote Docker Host

```python import docker from llm_sandbox import SandboxSession

tlsconfig = docker.tls.TLSConfig( clientcert=("path/to/cert.pem", "path/to/key.pem"), cacert="path/to/ca.pem", verify=True ) dockerclient = docker.DockerClient(baseurl="tcp://<yourhost>:", tls=tls_config)

with SandboxSession( client=dockerclient, image="python:3.9.19-bullseye", keeptemplate=True, lang="python", ) as session: result = session.run("print('Hello, World!')") print(result) ```

Kubernetes Support

```python from kubernetes import client, config from llm_sandbox import SandboxSession

Use local kubeconfig

config.loadkubeconfig() k8s_client = client.CoreV1Api()

with SandboxSession( client=k8sclient, backend="kubernetes", image="python:3.9.19-bullseye", lang="python", podmanifest=pod_manifest, # None by default ) as session: result = session.run("print('Hello from Kubernetes!')") print(result) ```

⚠️ Important for Custom Pod Manifests:

When using custom pod manifests, ensure your container configuration includes: - "tty": True (keeps container alive) - Proper securityContext at both pod and container levels - Container name can be any valid name (no restrictions)

See the Configuration Guide for complete requirements.

Podman Support

```python from llm_sandbox import SandboxSession

with SandboxSession( backend="podman", lang="python", image="python:3.9.19-bullseye" ) as session: result = session.run("print('Hello from Podman!')") print(result) ```

🤖 LLM Framework Integration

LangChain Tool

```python from langchain.tools import BaseTool from llm_sandbox import SandboxSession

class PythonSandboxTool(BaseTool): name = "python_sandbox" description = "Execute Python code in a secure sandbox"

def _run(self, code: str) -> str:
    with SandboxSession(lang="python") as session:
        result = session.run(code)
        return result.stdout if result.exit_code == 0 else result.stderr

```

Use with OpenAI Functions

```python import openai from llm_sandbox import SandboxSession

def executecode(code: str, language: str = "python") -> str: """Execute code in a secure sandbox environment.""" with SandboxSession(lang=language) as session: result = session.run(code) return result.stdout if result.exitcode == 0 else result.stderr

Register as OpenAI function

functions = [ { "name": "execute_code", "description": "Execute code in a secure sandbox", "parameters": { "type": "object", "properties": { "code": {"type": "string", "description": "Code to execute"}, "language": {"type": "string", "enum": ["python", "javascript", "java", "cpp", "go", "r"]} }, "required": ["code"] } } ] ```

🔌 Model Context Protocol (MCP) Server

LLM Sandbox provides a Model Context Protocol (MCP) server that enables AI assistants like Claude Desktop to execute code securely in sandboxed environments. This integration allows LLMs to run code directly with automatic visualization capture and multi-language support.

Features

  • Secure Code Execution: Execute code in isolated containers with your preferred backend
  • Multi-Language Support: Run Python, JavaScript, Java, C++, Go, R, and Ruby code
  • Automatic Visualization Capture: Automatically capture and return plots and visualizations
  • Library Management: Install packages and dependencies on-the-fly
  • Flexible Backend Support: Choose from Docker, Podman, or Kubernetes backends

Installation

Install LLM Sandbox with MCP support using your preferred backend:

```bash

For Docker backend

pip install 'llm-sandbox[mcp-docker]'

For Podman backend

pip install 'llm-sandbox[mcp-podman]'

For Kubernetes backend

pip install 'llm-sandbox[mcp-k8s]' ```

Configuration

Add the following configuration to your MCP client (e.g., claude_desktop_config.json for Claude Desktop):

json { "mcpServers": { "llm-sandbox": { "command": "python3", "args": ["-m", "llm_sandbox.mcp_server.server"], } } }

Backend-Specific Configuration

For the specific backend, you need to set the BACKEND environment variable to the backend you want to use. You might need to set other environment variables depending on the backend you are using. For example, you might need to set the DOCKER_HOST environment variable to the host you want to use if the DOCKER_HOST is not automatically in your system.

Docker (default): json { "mcpServers": { "llm-sandbox": { "command": "python3", "args": ["-m", "llm_sandbox.mcp_server.server"], "env": { "BACKEND": "docker", "DOCKER_HOST": "unix:///var/run/docker.sock" // change this to the actual host you are using } } } }

Podman: json { "mcpServers": { "llm-sandbox": { "command": "python3", "args": ["-m", "llm_sandbox.mcp_server.server"], "env": { "BACKEND": "podman", "DOCKER_HOST": "unix:///var/run/podman/podman.sock" // change this to the actual host you are using } } } }

For Kubernetes, you might need to set the KUBECONFIG environment variable to the path to your kubeconfig file.

Kubernetes: json { "mcpServers": { "llm-sandbox": { "command": "python3", "args": ["-m", "llm_sandbox.mcp_server.server"], "env": { "BACKEND": "kubernetes", "KUBECONFIG": "/path/to/kubeconfig" // change this to the actual path to your kubeconfig file } } } }

Available Tools

The MCP server provides the following tools:

  • execute_code: Execute code in a secure sandbox with automatic visualization capture
  • get_supported_languages: Get the list of supported programming languages
  • get_language_details: Get detailed information about a specific language

Usage Example

Once configured, you can ask your AI assistant to run code, and it will automatically use the LLM Sandbox MCP server:

text "Create a scatter plot showing the relationship between x and y data points using matplotlib"

The assistant will execute Python code in a secure sandbox and automatically capture any generated plots or visualizations.

🏗️ Architecture

```mermaid graph LR A[LLM Client] --> B[LLM Sandbox] B --> C[Container Backend]

A1[OpenAI] --> A
A2[Anthropic] --> A
A3[Local LLMs] --> A
A4[LangChain] --> A
A5[LangGraph] --> A
A6[LlamaIndex] --> A
A7[MCP Clients] --> A

C --> C1[Docker]
C --> C2[Kubernetes]
C --> C3[Podman]

style A fill:#e1f5fe
style B fill:#f3e5f5
style C fill:#e8f5e8
style A1 fill:#fff3e0
style A2 fill:#fff3e0
style A3 fill:#fff3e0
style A4 fill:#fff3e0
style A5 fill:#fff3e0
style A6 fill:#fff3e0
style A7 fill:#fff3e0
style C1 fill:#e0f2f1
style C2 fill:#e0f2f1
style C3 fill:#e0f2f1

```

📚 Documentation

🤝 Contributing

We welcome contributions! Please see our Contributing Guide for details.

Development Setup

```bash

Clone the repository

git clone https://github.com/vndee/llm-sandbox.git cd llm-sandbox

Install in development mode

make install

Run pre-commit hooks

uv run pre-commit run -a

Run tests

make test ```

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🌟 Star History

If you find LLM Sandbox useful, please consider giving it a star on GitHub!

📞 Support & Community

Contributors

Star History

Star History Chart

Owner

  • Name: Duy Huynh
  • Login: vndee
  • Kind: user
  • Location: Vietnam
  • Company: Working on some cool things

Senior Specialist in AI/ML, Independent Researcher, Freelance Developer

Citation (CITATION.cff)

cff-version: 1.2.0
message: "If you use this software, please cite it as below."
type: software
title: "LLM Sandbox"
abstract: "A lightweight and portable sandbox environment designed to run Large Language Model (LLM) generated code in a safe and isolated mode. It provides a secure execution environment for AI-generated code while offering flexibility in container backends and comprehensive language support."
authors:
  - family-names: "Huynh"
    given-names: "Duy V."
    email: "vndee.huynh@gmail.com"
    orcid: "https://orcid.org/0000-0000-0000-0000"  # Add your ORCID if available
identifiers:
  - type: url
    value: "https://github.com/vndee/llm-sandbox"
    description: "The GitHub repository"
  - type: url
    value: "https://pypi.org/project/llm-sandbox/"
    description: "The PyPI package"
repository-code: "https://github.com/vndee/llm-sandbox"
url: "https://vndee.github.io/llm-sandbox/"
repository-artifact: "https://pypi.org/project/llm-sandbox/"
license: MIT
version: "0.3.8"
date-released: "2025-06-13"  # Update this to the actual release date
keywords:
  - "LLM"
  - "sandbox"
  - "code execution"
  - "container"
  - "docker"
  - "kubernetes"
  - "security"
  - "python"
  - "javascript"
  - "java"
  - "artificial intelligence"
  - "large language models"
  - "secure execution"
preferred-citation:
  type: software
  title: "LLM Sandbox: Secure Execution Environment for AI-Generated Code"
  authors:
    - family-names: "Huynh"
      given-names: "Duy V."
      email: "vndee.huynh@gmail.com"
  version: "0.3.8"
  date-released: "2025-06-13"  # Update this to the actual release date
  url: "https://github.com/vndee/llm-sandbox"
  license: MIT
  repository-code: "https://github.com/vndee/llm-sandbox"

GitHub Events

Total
  • Create event: 42
  • Commit comment event: 3
  • Release event: 15
  • Issues event: 41
  • Watch event: 316
  • Delete event: 4
  • Issue comment event: 189
  • Push event: 239
  • Pull request review comment event: 46
  • Pull request review event: 62
  • Pull request event: 80
  • Fork event: 33
Last Year
  • Create event: 42
  • Commit comment event: 3
  • Release event: 15
  • Issues event: 41
  • Watch event: 316
  • Delete event: 4
  • Issue comment event: 189
  • Push event: 239
  • Pull request review comment event: 46
  • Pull request review event: 62
  • Pull request event: 80
  • Fork event: 33

Committers

Last synced: 7 months ago

All Time
  • Total Commits: 115
  • Total Committers: 9
  • Avg Commits per committer: 12.778
  • Development Distribution Score (DDS): 0.226
Past Year
  • Commits: 115
  • Committers: 9
  • Avg Commits per committer: 12.778
  • Development Distribution Score (DDS): 0.226
Top Committers
Name Email Commits
Duy Huynh v****h@g****m 89
Tomasz Wrona t****z@c****i 9
wben w****7@g****m 8
antonkulaga a****a@g****m 3
Martin Simonovsky m****y@a****i 2
tarunnv-uio t****t@u****o 1
dependabot[bot] 4****] 1
Ricardo Madriz r****3@g****m 1
DSri01 d****s@g****m 1
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 4 months ago

All Time
  • Total issues: 34
  • Total pull requests: 82
  • Average time to close issues: 12 days
  • Average time to close pull requests: 4 days
  • Total issue authors: 22
  • Total pull request authors: 13
  • Average comments per issue: 1.79
  • Average comments per pull request: 1.63
  • Merged pull requests: 67
  • Bot issues: 0
  • Bot pull requests: 11
Past Year
  • Issues: 34
  • Pull requests: 82
  • Average time to close issues: 12 days
  • Average time to close pull requests: 4 days
  • Issue authors: 22
  • Pull request authors: 13
  • Average comments per issue: 1.79
  • Average comments per pull request: 1.63
  • Merged pull requests: 67
  • Bot issues: 0
  • Bot pull requests: 11
Top Authors
Issue Authors
  • vndee (10)
  • snps-mahavadi (2)
  • Link87 (2)
  • Nifury (2)
  • aagarwal-amit (1)
  • GuinsooM (1)
  • wgzesg-bd (1)
  • Tmegaa (1)
  • Bullish-Design (1)
  • dkarten (1)
  • m7mdhka (1)
  • ammannm (1)
  • blairhudson (1)
  • kevroy314 (1)
  • jmaddington (1)
Pull Request Authors
  • vndee (50)
  • dependabot[bot] (9)
  • iamhatesz (6)
  • walter-bd (5)
  • coderabbitai[bot] (4)
  • tarunnv-uio (2)
  • khubo (2)
  • DSri01 (2)
  • mys007 (2)
  • antonkulaga (2)
  • richin13 (2)
  • vdmitriyev (1)
  • rhiza-fr (1)
Top Labels
Issue Labels
feature request (7) bug (3) enhancement (3) good first issue (1) backend (1) security (1) macos (1) question (1) test-issue (1)
Pull Request Labels
dependencies (10) python (8) bug (2) size:XS (1) enhancement (1) size:XXL (1) documentation (1) size:XL (1) size:M (1)

Packages

  • Total packages: 1
  • Total downloads:
    • pypi 12,482 last-month
  • Total dependent packages: 0
  • Total dependent repositories: 0
  • Total versions: 35
  • Total maintainers: 1
pypi.org: llm-sandbox

LLM Sandbox is a lightweight and portable sandbox environment designed to run large language model (LLM) generated code in a safe and isolated mode.

  • Versions: 35
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 12,482 Last month
Rankings
Dependent packages count: 10.7%
Average: 35.3%
Dependent repos count: 60.0%
Maintainers (1)
Last synced: 4 months ago