vastai
Load this before any Vast.ai task that is more than "rent the cheapest GPU that fits, run one command, get the result back" — that narrower case is already covered by the built-in vastai_run tool (searches live offers, rents one, uploads files_in, runs command, downloads files_out, destroys the instance; pass bid_price for spot, volume_id for an existing volume).
Reach beyond vastai_run — via the bash tool and the vastai CLI — for anything it doesn't do: browsing offer markets interactively, changing a bid, managing volumes, reserved instances, serverless endpoints, VM instances, or attaching an SSH key to an already-running container.
Prerequisites
VAST_API_KEY (Settings -> Academic Harness API keys, or env) — same credential vastai_run uses. Get it at https://cloud.vast.ai/manage-keys/.
The vastai CLI (and Python SDK) in one package. Install it yourself if it's missing — don't ask the user to do it:
if ! command -v vastai >/dev/null 2>&1; then
uv tool install vastai # or: python3 -m pip install --user vastai
# one-off alternative: uvx vastai ...
fi
vastai --version
Prefer uv tool install / pip --user over the curl -fsSL https://vast.ai/install.sh | bash installer so nothing needs root.
SSH keys must be registered before an instance exists. vastai_run registers a temporary per-call key and removes it afterwards; a CLI-driven instance needs a key already on the account:
vastai show ssh-keys
vastai create ssh-key ~/.ssh/id_ed25519.pub # only if absent
Working with the CLI
The CLI moves faster than any static reference — treat vastai <command> --help as authoritative:
vastai show user --raw # auth check + credit balance
vastai search offers 'gpu_name=RTX_4090 num_gpus=1 verified=true rentable=true' \
-o 'dph_total' --limit 5 --raw # offers; CLI gpu_ram is GB, REST is MB
vastai create instance <OFFER_ID> \
--image vastai/pytorch:@vastai-automatic-tag \
--disk 50 --ssh --direct --label agent-run-01 --raw
vastai show instance <ID> --raw # poll actual_status
vastai ssh-url <ID> # ssh://root@<host>:<port>
vastai execute <ID> "nvidia-smi"
vastai logs <ID> --tail 100
vastai copy local:./src/ <ID>:/workspace/src/ # never copy to / or /root
vastai stop instance <ID> # GPU billing stops; disk billing continues
vastai destroy instance <ID> -y # permanent; -y is mandatory for agents
Pitfalls that break agent runs
- Spot pricing: searching with
--type bid returns min_bid, but create instance charges on-demand unless --bid_price <price> is passed. vastai_run sends price for you when bid_price is set.
- Terminal states: once
actual_status is exited, unknown, or offline it never reaches running. Destroy and retry another offer instead of polling forever.
- Destroy, don't stop:
stop keeps accruing storage charges; destroy is the only teardown that stops all billing.
- Never write to
/ or /root: copying there corrupts .ssh/authorized_keys permissions and locks you out. Use /workspace/....
onstart is capped at 4,048 characters; use vastai copy afterwards for anything longer.
- Env visibility: variables passed at create time reach the container entrypoint, not interactive SSH login shells; prepend
env >> /etc/environment in onstart if a later SSH command needs them.
- Runtypes:
ssh_direct (port 22) and jupyter_direct (8080 + 22) replace the image entrypoint; args preserves it but disables SSH. vastai_run uses ssh_direct.
REST API notes
vastai_run uses the REST API directly (https://console.vast.ai/api/v0, Bearer auth):
- Search offers:
POST /bundles/ (filters such as gpu_name, num_gpus, gpu_ram in MB, dph_total; order: [["dph_total","asc"]]).
- Create:
PUT /asks/{offer_id}/ → {"success":true,"new_contract":<instance_id>}. env is a Docker-flag string, not an object.
- Status/destroy:
GET/DELETE /instances/{id}/; SSH host and port come back as ssh_host / ssh_port.
- SSH keys:
POST /ssh with {"ssh_key": "..."}, then DELETE /ssh/{id}.
Choosing a lane
- One-shot GPU command, cheapest matching offer, automatic teardown:
vastai_run.
- Spot/interruptible or a pinned offer:
vastai_run with bid_price / offer_id.
- Persistent data:
vastai_run with a pre-created volume_id.
- Bidding changes, multi-instance fleets, serverless, VMs, or interactive market browsing:
vastai CLI via bash.
Always destroy what you created and report the offer's dph_total and the estimated cost, as vastai_run does.
1---2name: vastai3description: "Use Vast.ai for cost-optimized GPU marketplace compute beyond what the vastai_run tool covers — bidding, spot instances, volumes, CLI/API control, multi-GPU offers."4metadata:5 source: https://skillmd.com/skills/raktim-mondol/vastai6---78# vastai910Load this before any Vast.ai task that is more than "rent the cheapest GPU that fits, run one command, get the result back" — that narrower case is already covered by the built-in `vastai_run` tool (searches live offers, rents one, uploads `files_in`, runs `command`, downloads `files_out`, destroys the instance; pass `bid_price` for spot, `volume_id` for an existing volume).1112Reach beyond `vastai_run` — via the `bash` tool and the `vastai` CLI — for anything it doesn't do: browsing offer markets interactively, changing a bid, managing volumes, reserved instances, serverless endpoints, VM instances, or attaching an SSH key to an already-running container.1314## Prerequisites1516- `VAST_API_KEY` (Settings -> Academic Harness API keys, or env) — same credential `vastai_run` uses. Get it at <https://cloud.vast.ai/manage-keys/>.17- The `vastai` CLI (and Python SDK) in one package. **Install it yourself if it's missing** — don't ask the user to do it:1819 ```bash20 if ! command -v vastai >/dev/null 2>&1; then21 uv tool install vastai # or: python3 -m pip install --user vastai22 # one-off alternative: uvx vastai ...23 fi24 vastai --version25 ```2627 Prefer `uv tool install` / `pip --user` over the `curl -fsSL https://vast.ai/install.sh | bash` installer so nothing needs root.28- SSH keys must be registered **before** an instance exists. `vastai_run` registers a temporary per-call key and removes it afterwards; a CLI-driven instance needs a key already on the account:2930 ```bash31 vastai show ssh-keys32 vastai create ssh-key ~/.ssh/id_ed25519.pub # only if absent33 ```3435## Working with the CLI3637The CLI moves faster than any static reference — treat `vastai <command> --help` as authoritative:3839```bash40vastai show user --raw # auth check + credit balance41vastai search offers 'gpu_name=RTX_4090 num_gpus=1 verified=true rentable=true' \42 -o 'dph_total' --limit 5 --raw # offers; CLI gpu_ram is GB, REST is MB43vastai create instance <OFFER_ID> \44 --image vastai/pytorch:@vastai-automatic-tag \45 --disk 50 --ssh --direct --label agent-run-01 --raw46vastai show instance <ID> --raw # poll actual_status47vastai ssh-url <ID> # ssh://root@<host>:<port>48vastai execute <ID> "nvidia-smi"49vastai logs <ID> --tail 10050vastai copy local:./src/ <ID>:/workspace/src/ # never copy to / or /root51vastai stop instance <ID> # GPU billing stops; disk billing continues52vastai destroy instance <ID> -y # permanent; -y is mandatory for agents53```5455## Pitfalls that break agent runs5657- **Spot pricing**: searching with `--type bid` returns `min_bid`, but `create instance` charges on-demand unless `--bid_price <price>` is passed. `vastai_run` sends `price` for you when `bid_price` is set.58- **Terminal states**: once `actual_status` is `exited`, `unknown`, or `offline` it never reaches `running`. Destroy and retry another offer instead of polling forever.59- **Destroy, don't stop**: `stop` keeps accruing storage charges; `destroy` is the only teardown that stops all billing.60- **Never write to `/` or `/root`**: copying there corrupts `.ssh/authorized_keys` permissions and locks you out. Use `/workspace/...`.61- **`onstart` is capped at 4,048 characters**; use `vastai copy` afterwards for anything longer.62- **Env visibility**: variables passed at create time reach the container entrypoint, not interactive SSH login shells; prepend `env >> /etc/environment` in `onstart` if a later SSH command needs them.63- **Runtypes**: `ssh_direct` (port 22) and `jupyter_direct` (8080 + 22) replace the image entrypoint; `args` preserves it but disables SSH. `vastai_run` uses `ssh_direct`.6465## REST API notes6667`vastai_run` uses the REST API directly (`https://console.vast.ai/api/v0`, Bearer auth):6869- Search offers: `POST /bundles/` (filters such as `gpu_name`, `num_gpus`, `gpu_ram` in MB, `dph_total`; `order: [["dph_total","asc"]]`).70- Create: `PUT /asks/{offer_id}/` → `{"success":true,"new_contract":<instance_id>}`. `env` is a Docker-flag **string**, not an object.71- Status/destroy: `GET`/`DELETE /instances/{id}/`; SSH host and port come back as `ssh_host` / `ssh_port`.72- SSH keys: `POST /ssh` with `{"ssh_key": "..."}`, then `DELETE /ssh/{id}`.7374## Choosing a lane7576- One-shot GPU command, cheapest matching offer, automatic teardown: `vastai_run`.77- Spot/interruptible or a pinned offer: `vastai_run` with `bid_price` / `offer_id`.78- Persistent data: `vastai_run` with a pre-created `volume_id`.79- Bidding changes, multi-instance fleets, serverless, VMs, or interactive market browsing: `vastai` CLI via `bash`.8081Always destroy what you created and report the offer's `dph_total` and the estimated cost, as `vastai_run` does.