---
name: factor-run
description: Run code on a rented GPU with the Factor CLI. Use when the user wants to train a model, run a batch job, render or simulate on a GPU, benchmark, or run anything that needs hardware the local machine does not have — and when they mention Factor, `factor sandbox`, or GPU jobs generally.
---

# Running work on Factor

Factor rents a GPU, ships a local file or folder to it, runs it, and brings
the results back. Two shapes:

- **Sandbox** — runs a command and stops. When the command exits the machine
  stays **warm for 10 minutes** (billed as running) and is then **suspended**
  (no billing). The next run of the same command, hardware and image **reuses
  it**: within the 10 minutes only your code is swapped and the program
  restarted on the machine that is already up, so it starts in well under a
  minute instead of the few minutes a new machine takes; after that it is
  woken from suspension, which takes a few minutes. Use for anything with an
  end: training, batch jobs, simulations, renders, benchmarks.
- **Factory** — stays up and keeps its disk. Use for serving, a dev box, or
  anything redeployed from a repository (see the factor-manifest skill). A
  factory's `ports:` get public HTTPS addresses — that is how a server is made
  reachable from other computers. **Never use a tunnel** (cloudflared, ngrok,
  …) for that.

**Default to a sandbox.**

## Before running anything

```bash
factor auth status     # signed in, and which team pays
factor hardware        # what exists, cheapest first, with prices
factor billing plan    # balance and the account's limits
```

If `auth status` fails, ask the user to run `factor auth login` — it opens a
browser, so you cannot do it for them.

**Account limits.** A new account with a verified card may run **one machine
at up to $3/hr**; the limits rise as it pays (`factor billing plan` shows
them). An H100 is above that until the first top-up. A refusal that says
`limited to $3.00/hr` is this, not an error in your command — pick cheaper
hardware or tell the user.

## The shape of a good run

```bash
factor sandbox ./project \
  --gpu 24gb-class \
  -i python \
  --setup "pip install -q -r requirements.txt" \
  --local-volume ./results \
  --stream
```

- `--stream` keeps the terminal attached and prints the run's output. Run it
  in the foreground and read what it prints; that output **is** the log of
  this run.
- `--local-volume ./results` copies everything the program writes to
  `/factory/out` into `./results` when the run ends. No separate `pull`.
- The exit code of `factor sandbox` is the program's exit code.

Program arguments go after `--`:

```bash
factor sandbox train.py --gpu 24gb-class --stream -- --epochs 50 --lr 3e-4
```

### Choosing `--gpu`

Ask for **memory, not a model**: a class lets the cheapest free card that fits
win, and most work needs enough VRAM rather than a particular chip. A class
runs on the smallest card size of at least that much memory: `16gb-class` is an
RTX A4000, `24gb-class` and `48gb-class` an RTX A6000 or L40 (48 GB), and
`80gb-class` an A100 or H100.

| Ask | When |
| --- | --- |
| `--gpu 16gb-class` or `24gb-class` | Default for most single-GPU work: simulations, small models, renders, inference. |
| `--gpu 48gb-class`, `80gb-class` | Large models. Check the price in `factor hardware` and the account's limit first. |
| `--gpu 24gb-class:2` | Two cards on one machine. |
| `--gpu a6000`, `--gpu h100` | Only when the user named the card or the work depends on that architecture. |

If no zone is given the CLI picks the nearest one and, when nothing suitable is
free there, places the run wherever a card is free. Only pass `--zone` when the
user cares where it runs.

## Dependencies: image first, setup for the rest

The machine's disk is reset between runs, so **`--setup` runs again on every
run**, warm starts included. Keep it short, and pick an image that already has
the heavy parts (`factor images` lists them):

| Image | Has |
| --- | --- |
| `python` (default) | Python 3.12, pip, small. GPU work through wheels that bundle CUDA (torch, jax, `cupy-cuda12x`). |
| `pytorch` | PyTorch with CUDA 12.4 on Ubuntu; large, but nothing to install for torch. |
| `cuda` | The CUDA toolkit (nvcc), for compiling kernels or libraries that link system CUDA. |

Each `--setup` value is **one shell command line**, run as root before the
program: `&&`, quotes and pipes work. Repeat the flag for several steps:

```bash
--setup "apt-get update -qq && apt-get install -y -qq ffmpeg" \
--setup "pip install -q cupy-cuda12x matplotlib"
```

Setup runs as root; the program runs as an unprivileged user. Install
system packages in setup, not from the program.

Every machine has a 100 GB system disk; most also have a large fast disk at
`/scratch` for datasets and checkpoints during a run. `/scratch` is wiped when
the machine stops - results still go to `/factory/out`.

## Rules that decide whether the run is useful

**1. Results go to `/factory/out`.** Nothing written anywhere else survives
the run. Make the code write there (and create the directory if it writes
into a subfolder):

```python
OUT = os.environ.get("FACTORY_OUT_DIR", "/factory/out")
os.makedirs(OUT, exist_ok=True)
```

**2. Output must be flushed.** A buffered stdout shows nothing until the
process exits — which looks exactly like a hung run. Use `print(..., flush=True)`
or `python -u`.

**3. There is no display.** Plotting libraries must render off-screen: set
`MPLBACKEND=Agg` (or `matplotlib.use("Agg")`) before importing pyplot.

**4. Test the logic small first.** A bug found on the GPU costs a machine
start. For anything non-trivial, run a few steps locally on CPU (or with a
tiny grid) before the real run.

## Iterating: edit, run again, on the warm machine

The loop Factor is built for. Run once, read the output, change the code, and
run **exactly the same command** again:

```bash
factor sandbox train.py --gpu 24gb-class --stream   # first run: rents a machine
# edit train.py
factor sandbox train.py --gpu 24gb-class --stream   # ✓ Reusing sandbox … - warm start.
```

Within 10 minutes of the last run finishing, the second run lands on the same
machine: the image is already pulled, the new code is uploaded, and only the
program restarts. No new machine, no boot, no image pull.

- **Keep the command identical** apart from the code. A different `--gpu`,
  image, `--setup`, `--env` or `--zone` cannot reuse the machine and rents a
  new one. Secrets (`--secret`) and the code itself may change.
- **Do not add `--fresh` or `--delete-on-finish`** while iterating: both throw
  the warm machine away.
- **Do not wait it out.** Run again as soon as the change is made; after 10
  minutes the machine is suspended and the next run takes minutes again.
- Each new run's output follows the previous runs' in `factor logs`; with
  `--stream` you see only the new one.

## When a run fails

The command's own `--stream` output is the first place to look: the
traceback is at the end of it, just before `Workload exited N`.

- **Fix the code and run the same command again**, right away, so it lands on
  the warm machine (see *Iterating* above). The new code is uploaded — a warm
  start does **not** run stale code. `--fresh` is only for when the machine
  itself is suspect.
- `factor logs <id>` prints a sandbox's whole history, earlier runs
  included. The current run starts at its last `fetching source` line.
- Setup failed? The output says `setup step N exited M; the workload will
  not run`, with that step's output above it.
- For anything stranger, use the factor-debug skill.

## Long runs

```bash
id=$(factor sandbox train.py --gpu 24gb-class --detach --json | jq -r .id)
factor logs "$id" -f
factor pull "$id" ./results
```

When you are done iterating, `factor stop <id>` suspends the sandbox at once
instead of leaving it warm for 10 billed minutes.

Start **one** run at a time per task. Before starting another, check nothing
of yours is still running (`factor ls`). A second run started "just in case"
is a second machine billing.

## Secrets

Never put a key in code, a manifest, or a command argument. Store it once,
from **stdin**:

```bash
printf '%s' "$HF_TOKEN" | factor secret set HF_TOKEN
factor sandbox train.py --gpu 24gb-class --secret HF_TOKEN
```

An argument lands in shell history and in the process list for every other
user on the machine.

## Cost

Billed per second, from ready to exit; a suspended sandbox costs nothing.
Before something long or wide, tell the user roughly what it will cost —
`factor hardware` gives the hourly rate. Suggest a spend cap if they have
none: `factor billing spend-cap 200`.

## What not to do

- Do not run `factor rm` on something you did not start in this task, or
  without saying so. It destroys the machine and its disk, with no undo.
  Removing a duplicate you started yourself is fine — say that you did.
- Do not run `factor auth login` — it needs a browser and a human.
- Do not guess an id. Take it from the command's output or `factor ls`.
