Skip to content

Code Interpreter

An Ovrin sandbox can execute code in a stateful context: variables, imports, and objects you define in one call are still there in the next. That is what separates it from run, which starts a fresh shell every time.

Everything below uses the ovrin SDK. For the full client surface, see the SDK reference.

Setup

bash
pip install ovrin
export OVRIN_API_KEY="ovrin_..."

Run stateful code

Create a sandbox from the code-interpreter template, then execute code. Reuse the context_id returned by the first call so state carries over:

python
import ovrin

client = ovrin.Client()  # reads OVRIN_API_KEY

sandbox = client.sandboxes.create(template="code-interpreter", timeout=600)

# Define state.
first = sandbox.run_code("x = 20")
context_id = first["context_id"]

# Reuse it in the next call.
second = sandbox.run_code("print(x + 22)", context_id=context_id)
print(second["stdout"])        # 42

# The last expression is returned as a structured result.
third = sandbox.run_code("2 ** 10", context_id=context_id)
print(third["results"])        # 1024

sandbox.kill()

Each response carries {stdout, stderr, results, error, context_id}. Pass the context_id to keep state; omit it to start fresh.

Stream long-running output

stream yields execution events as they are produced, so a slow command is not indistinguishable from a hang:

python
for event in sandbox.stream("for i in range(3): print(i)"):
    print(event)

Files persist

The sandbox filesystem is shared with the interpreter, so code and file I/O see the same paths:

python
sandbox.files.write("/workspace/data.csv", "a,b\n1,2\n")
sandbox.run_code(
    "import csv\n"
    "with open('/workspace/data.csv') as f:\n"
    "    rows = list(csv.reader(f))\n"
    "print(rows)"
)

Other languages

The language argument selects the runtime, subject to what the template provides — python, javascript, and others:

python
sandbox.run_code("console.log([1, 2, 3].reduce((a, b) => a + b))", language="javascript")

Lifecycle

A sandbox stops billing the moment it is killed or paused:

python
sandbox.kill()                 # terminate now
sandbox.pause()                # keep the filesystem, stop the clock
sandbox.resume()               # continue where it left off

Let the model drive

The MCP server exposes these same capabilities as tools, so Claude, Cursor, and other MCP clients can run code without an SDK.

Runnable sample

A complete script lives at examples/ovrin-code-interpreter/main.py in the repository.