Skip to main content
Mirage gives an agent a unified virtual terminal across different machines. The agent types every command in the same terminal, and each one runs on the machine that fits it, plain Python inside Mirage, numpy in a small container, data processing on a CPU sandbox and training on a GPU, so the agent finishes its tasks faster and at lower cost.

Setup

A Cloudflare lite container with numpy, reached through a relay Worker (see Cloudflare Container):
Dockerfile
wrangler.jsonc
An E2B sandbox from a template with sshfs, pandas and numpy:
For the GPU, any machine reachable over SSH works. This example uses an EC2 g4dn.xlarge with an NVIDIA T4, running a container with PyTorch, CUDA, sshfs and sshd on port 2222.
workspace.yaml

Routing

A policy script picks the machine for each Python command by the library its code uses:
route.py

Count Page Views

/workspace/raw holds a year of page views, one file a quarter. Mirage fans the counting out to four Monty runs, lightweight Python sandboxes inside Mirage, one per quarter, so they run in parallel with no container to start. Only the numpy summary goes to the Cloudflare lite container, which then runs for seconds instead of the whole task. A lite container shares its 1/16 vCPU across every process in it, so counting the quarters in parallel there is no faster, about 16 seconds against 11.7 one after another. The four Monty runs take about 3.3 seconds, and the container runs only for numpy.

Monty: count each quarter in parallel

jobs/count.py

Cloudflare container: summarize with numpy

Train a Model

The features and the model come from the same year of page views. pandas builds the features on the E2B sandbox, and only the training goes to the GPU, so the GPU is paid for the training alone. pandas runs about as fast on the E2B sandbox as on the GPU machine’s CPU, so the two take about the same time. The GPU costs about 4.5 times as much a second, so moving the features off it cuts the cost by about 44%.

E2B: build features

build.py turns the four quarters of page views into one row per user with pandas, and writes them back to /workspace.
jobs/build.py

GPU: train the model

train.py reads the features the E2B sandbox wrote, trains a small model on the T4 and saves it next to them. Started as a background job, ps lists it on the GPU by its real pid there, and jobs, wait and kill reach it the same way.
jobs/train.py
mirage shell --explain shows which machine each command will run on, before the commands are executed.