What you’ll build
A script that:- Creates an isolated CodeBox
- Runs untrusted Python code inside it
- Auto-installs required packages
- Returns the output to your application
Prerequisites
- Python
- Node.js
Step 1: Run simple code
Start with a basic example — execute a Python snippet and print the result.- Python
- Node.js
run_code.py
- CodeBox creates a lightweight VM with a Python runtime pre-installed
- Your code runs inside the VM — completely isolated from the host
- Output is captured and returned as a string
- The VM is cleaned up automatically when the context exits
Step 2: Auto-install packages
CodeBox detects missing imports and installs them automatically. No manualpip install needed.
- Python
- Node.js
auto_install.py
Step 3: Install packages explicitly
When you need specific versions or want to pre-install packages before running code, useinstall_package().
- Python
- Node.js
explicit_install.py
Step 4: Run a script file
Userun_script() to execute a Python script from your host filesystem inside the box.
run_script() reads the script from your host machine and executes it inside the box. It does not look for files already inside the guest.- Python
- Node.js
analysis.py
run_script.py
Step 5: Run multiple snippets in the same box
A single CodeBox can run many snippets sequentially. Filesystem state persists between calls — installed packages and written files remain available. However, in-memory state (variables, functions) does not persist.- Python
- Node.js
multi_run.py
What’s next?
Upload & download files
Move files in and out of your sandbox to process data and retrieve results.
Connect to an LLM
Wire up OpenAI function calling so an LLM can execute code in your sandbox.
CodeBox API reference
Full API docs for run(), run_script(), install_package(), and more.
AI agent integration
Advanced patterns: concurrency, timeouts, security presets, and production configuration.

