Best sandbox · Code interpreter
The best sandbox for an LLM code interpreter
Updated
A code interpreter lets a model write a snippet, run it and read the output, charts included. The sandbox behind it has to run code you have never seen, so it needs its own kernel, no network unless you allow one, time limits, files that persist between calls and a way to return images. kosa8's run_code does this in a microVM on your own machine, and Kosa8Cloud does the same on kosa8 Cloud.
01What a code interpreter needs from a sandbox
Every call runs untrusted code: the model wrote it, possibly after reading a prompt injection. It may loop forever, fill the disk, try to read your files or phone home. It also needs to be useful: libraries installed, uploaded files available, plots returned as images, and results kept between calls so the model can build on them.
02Checklist for choosing one
- Isolation below the language. Not a Python sandbox inside your process; a separate kernel.
- Network off by default. Opened per host when a task needs it.
- Timeouts per call. A runaway snippet returns
timed_outinstead of hanging. - Files in and out. Upload inputs, read back outputs.
- Charts as images. matplotlib figures returned as PNG bytes.
- Fast reset. Rewind after a destructive call.
- Runs where the data is. Locally for private data, hosted for scale.
03How kosa8 meets it
Install kosa8 and the Python SDK, then run snippets in a closed sandbox. Each run_code call is a new process that starts in /workspace; files there persist between calls.
from kosa8 import Kosa8Client client = Kosa8Client() client.create_sandbox(name="interp", egress="deny", auto_stop_min=15) sb = client.sandbox("interp") r = sb.run_code("import math; print(math.factorial(20))", timeout_s=30) print(r.ok, r.stdout) # True 2432902008176640000 sb.write_file("notes.txt", "kept between calls") print(sb.run_code("print(open('notes.txt').read())").stdout)
run_code also takes language="javascript" or "typescript". A chart drawn with matplotlib comes back in r.artifacts as PNG bytes when the image has matplotlib installed: build a template with your libraries (kosa8 template build) and create the sandbox with template=. Before a risky call, sb.checkpoint(); afterwards, sb.rewind().
To expose the interpreter to a model, wrap the call in a tool, or use the ready-made tools for the OpenAI Agents SDK and LangChain. On kosa8 Cloud the same idea is Kosa8Cloud().sandboxes.create(template="python") and box.exec(["python3", "-c", code]).
04When another option fits better
- Model providers' hosted code execution. OpenAI and Anthropic both offer code execution tools that run on their side. If the data may go there, they need no infrastructure at all.
- A notebook kernel. If users need variables to stay in memory between cells, a Jupyter kernel keeps state that separate
run_codeprocesses do not; keep it inside a sandbox if the code is untrusted. - WebAssembly runtimes. Pyodide in the browser isolates small snippets cheaply with no server, but has limited packages and no real shell.
05FAQ
Does state persist between code runs?
Files in /workspace do; Python variables do not, because each run_code is a new process. For a shell that keeps cd and export, use sb.shell().
Can the interpreter return charts?
Yes. Figures a Python snippet draws with matplotlib come back as PNG bytes in CodeResult.artifacts, provided matplotlib is installed in the sandbox's image.
How do I stop runaway code?
Pass timeout_s; a call past it returns with timed_out=True. Size memory with mem_mb on create, and use auto_stop_min or ephemeral=True so idle sandboxes stop.
Is a Python-level sandbox enough?
Not for model-written code. Restricted interpreters have a long history of escapes; a separate kernel behind a hypervisor is the boundary kosa8 uses.
06Get started
Install kosa8 on a Mac, Linux or Windows (WSL2) machine, or make an account and run the same sandboxes on kosa8 Cloud.
07Sources
- kosa8 docs: Python quickstart
- kosa8 docs: Agent frameworks (Python)
- kosa8 docs: kosa8 Cloud
- kosa8 docs: Network
- kosa8 command reference
Related
- How do I build a self-hosted code interpreter for my LLM?
- How do I run untrusted LLM-generated code safely?
- Docker containers vs microVMs: which isolates an AI agent better?
- The best sandbox for data analysis agents
- All best-sandbox pages
Updated 2026-11-14