# Sandboxes

> Start an isolated container, run commands or code in it, read and write its files, then terminate it.

Canonical page: https://klio.tech/docs/sandboxes

## Create a sandbox

A sandbox is a container you drive from your code: useful for running code an LLM wrote or a player uploaded, apart from everything else. Every customer's work runs sandboxed, apart from other customers and from the machine.

```python
from klio_compute import Sandbox, Image

sandbox = Sandbox(cpu=1, memory="1Gi", image=Image(python_version="python3.12")).create()
try:
    result = sandbox.process.run_code("print(2 + 2)")
    print(result.result)
finally:
    sandbox.terminate()
```

`sandbox.process.exec(...)` runs a command, and `sandbox.fs` uploads and downloads files. A sandbox can have a GPU with `gpu=`. You pay for the seconds it runs; `terminate()` stops it.

## With the Modal-compatible API

```python
import klio_compute.compat as modal

sandbox = modal.Sandbox.create(image=modal.Image.debian_slim(), timeout=300)
process = sandbox.exec("bash", "-c", "echo hello")
print(process.stdout.read())
sandbox.terminate()
```

`Sandbox.create`, `exec`, `filesystem.write_text`, `filesystem.read_text` and `terminate` work. Options that can't be honored stop with a clear message.
