关于 vastai
vastai 是管理 Vast.ai GPU 租用资源的命令行工具,适用于需要配置和运维计算资源的用户及脚本。它涵盖实例搜索与生命周期管理,以及存储、部署、账户管理和账单查询。
主要功能
- 01搜索并筛选 GPU 报价
- 02创建和管理实例
- 03管理卷与模板
- 04传输文件并执行命令
- 05管理端点与账单
适用场景
- 01按硬件、地区或价格查找 GPU 租用资源
- 02配置并监控计算实例
- 03在本地与云存储间传输数据集
- 04查看账户使用情况和费用
安装
复制下面的命令,在终端中运行。
npx skills add https:/ / github.com/ vast-ai/ vast-cli --skill vastai安装后在 Claude Code 中输入/vastai确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
__init__.py_base.pyapi/__init__.pyauth.pybilling.pyclient.pyclusters.pydeployments.pyendpoints.pyinstances.pykeys.pymachines.pymetrics.pyoffers.pyprice_increase.pyquery.pystorage.pyteams.pyasync_/__init__.pyclient.pyresults.pycli/__init__.pycli/commands/__init__.pyauth.pybenchmarks.pybilling.pyclusters.pydeployments.pyendpoints.pyinstances.py详细介绍
vastai
在 Vast.ai 上管理 GPU 实例、模板、卷、无服务器端点、SSH 密钥和账单。
命令为
vastai(小写)。机器可读的 JSON 输出始终使用--raw。
安装
# Linux/macOS (recommended, no Python required)
curl -fsSL https://vast.ai/install.sh | bash
# or via PyPI (Windows, or if you'd rather use pip)
pip install vastai
快速开始
vastai set api-key <YOUR_API_KEY> # Authenticate (one-time); Create API Key in account at https://console.vast.ai/manage-keys/
vastai show user # Verify auth + check balance
vastai create ssh-key ~/.ssh/id_ed25519.pub # Register SSH key (do BEFORE create)
vastai search offers 'gpu_name=RTX_4090 num_gpus=1 verified=true direct_port_count>=1 rentable=true' -o 'dlperf_usd-'
# Note the offer ID from the output
vastai create instance <OFFER_ID> --image pytorch/pytorch:@vastai-automatic-tag --disk 20 --ssh --direct
# Automatically grab appropriate image tag; Response: {"success": true, "new_contract": <INSTANCE_ID>}
vastai show instance <INSTANCE_ID> # Poll until actual_status == "running" (see Instance status values below)
vastai ssh-url <INSTANCE_ID> # Get SSH connection string
vastai copy local:./data/ <INSTANCE_ID>:/workspace/ # Upload files
vastai destroy instance <INSTANCE_ID> -y # Clean up (stops all billing; -y skips confirmation)
API 密钥:https://console.vast.ai/manage-keys/
全局标志
所有命令均可使用:
--api-key KEY Override stored API key
--raw Output machine-readable JSON (agents should always use this)
--full Print full results (don't page with less)
--explain Show underlying API calls (useful for debugging)
--curl Show equivalent curl command
--no-color Disable colored output
--url URL Override server REST API URL
--retry RETRY Set retry limit for API calls
--version Show CLI version
查询语法
搜索命令接受筛选表达式。运算符:=、!=、>、>=、<、<=、in、notin。
# Examples
'gpu_name=RTX_4090 num_gpus=1' # Exact match + numeric
'gpu_ram>=48 reliability>0.95' # Greater-than filters
'geolocation=EU dph_total<=2.0' # Region + price cap
常用筛选字段:num_gpus、gpu_name、gpu_ram、cpu_ram、disk_space、reliability、compute_cap、inet_up、inet_down、dph_total、geolocation、direct_port_count、verified、rentable
常用排序字段:score(默认值 — 综合性价比)、dlperf_usd(每美元的 DL 性能)、dph_total(价格)、num_gpus、reliability
命令
实例
vastai show instances # List all your instances (fetches every page, no prompts)
vastai show instances --status running loading # Filter by status
vastai show instances --gpu-name 'RTX 4090' # Filter by GPU
vastai show instances --label training # Filter by label
vastai show instances --order-by start_date desc # Sort by column
vastai show instances --cols id,status,gpu,dph # Custom columns
vastai show instances --raw # Flat JSON list of all instances (scripting)
vastai show instance <id> # Poll single instance (use for status checks)
vastai create instance <offer-id> --image pytorch/pytorch:2.4.0-cuda12.4-cudnn9-runtime --disk 20 --ssh --direct
# Response includes "new_contract": <id> — that is your instance ID
vastai launch instance --gpu-name RTX_4090 --num-gpus 1 --image pytorch/pytorch
vastai start instance <id> # Start stopped instance
vastai stop instance <id> # Stop (preserves disk, no GPU charges)
vastai reboot instance <id> # Stop + start
vastai destroy instance <id> -y # Delete permanently (irreversible; -y required for non-interactive use)
vastai destroy instances <id1> <id2> -y # Batch delete (-y skips confirmation prompt)
vastai label instance <id> --label "training-run-1" # Tag instance
vastai update instance <id> # Recreate from updated template
vastai prepay instance <id> # Deposit credits into reserved instance
vastai recycle instance <id> # Destroy + recreate
推荐的 Vast.ai 镜像(使用 @vastai-automatic-tag 自动为机器选择合适的标签;在 https://vast.ai/model-library 浏览预配置模型):
vastai/base-image:@vastai-automatic-tag # Minimal Ubuntu base
vastai/pytorch:@vastai-automatic-tag # PyTorch + CUDA
vastai/linux-desktop:@vastai-automatic-tag # Linux desktop (VNC/RDP)
# vLLM — set model via env vars with huggingface model example
vastai create instance <id> --image vastai/vllm:@vastai-automatic-tag --disk 40 --ssh --direct \
--env '-e MODEL_NAME=Qwen/Qwen2.5-3B-Instruct -e HF_TOKEN=hf_xxx'
# ComfyUI — set model checkpoint via env vars with huggingface model example
vastai create instance <id> --image vastai/comfy:@vastai-automatic-tag --disk 40 --ssh --direct \
--env '-e CHECKPOINT_MODEL=black-forest-labs/FLUX.1-schnell -e HF_TOKEN=hf_xxx'
create instance 标志:
--image IMAGE— Docker 镜像--disk DISK— 本地磁盘,单位为 GB--ssh/--jupyter— 连接类型--direct— 更快的直连--label LABEL— 实例标签--env ENV— 环境变量和端口映射,例如'-e TZ=UTC -p 8080:8080'--onstart FILE— 启动脚本文件的路径--onstart-cmd CMD— 内联字符串形式的启动脚本(较长的脚本请使用--onstart,或通过 gzip+base64 编码)--bid_price PRICE— 可中断(竞价)价格,单位为美元/小时--template_hash HASH— 根据模板创建--create-volume ID— 挂载新卷--link-volume ID— 挂载现有卷--cancel-unavail— 无可用机器时失败(而非创建已停止的实例)
实例状态值:
actual_status | 含义 |
|---|---|
null | 正在配置 |
created | 实例已创建,尚未配置 |
loading | 正在下载镜像 / 启动容器 |
running | 活跃 — 收取 GPU 费用 |
stopped | 已停止 — 仅收取磁盘费用 |
frozen | 已暂停并保留内存 — 收取 GPU 费用 |
exited | 容器进程意外退出 |
rebooting | 正在重启(临时状态) |
unknown | 主机没有最近的心跳信号 |
offline | 主机已断开与 Vast 服务器的连接 |
**轮询循环警告:**如果
actual_status变为exited、unknown或offline,它将永远无法达到running。务必添加超时和错误分支,否则脚本会无限循环,同时持续产生磁盘费用。销毁该实例并使用其他报价重试。
**费用:**存储费用从创建时开始计收。GPU 费用从状态达到
running时开始计收。
可中断(竞价)租用
可中断(竞价)实例的价格低于按需实例,但随时可能被出价更低的其他用户中断。注意:search offers --type bid 会显示 min_bid,但 create instance 默认使用 dph_total 按需创建,除非传入 --bid_price <floor>。在使用 --type bid 搜索后,务必传入 --bid_price,否则租用的将是按需实例 / 按需价格,而不是可中断实例。
被超过出价后,实例会变为 stopped(而非销毁),存储费用仍会继续计收。可以通过 update instance --bid_price 提高出价来恢复实例。
搜索
vastai search offers # Default: verified, on-demand, sorted by score
vastai search offers 'gpu_name=RTX_4090 num_gpus=1 verified=true direct_port_count>=1' -o 'dlperf_usd-'
vastai search offers 'num_gpus>=4 reliability>0.99' -o 'num_gpus-'
vastai search offers --type bid # Interruptible (spot) pricing
vastai search offers --type reserved # Reserved pricing
vastai search offers -n 'gpu_name=H100_SXM' # No default filters
vastai search volumes # Search volume offers
vastai search templates "pytorch" # Search templates
vastai search benchmarks # Search benchmarks
vastai search invoices # Search invoice history
search offers 标志:--type on-demand|reserved|bid、--order/-o FIELD[-]、--limit、--storage GB、--no-default/-n
SSH 和文件传输
vastai ssh-url <id> # Get ssh:// connection URL
vastai scp-url <id> # Get scp:// URL
vastai attach ssh <id> "ssh-ed25519 AAAA..." # Attach SSH key to instance
vastai detach ssh <id> <ssh_key_id> # Remove SSH key (ssh_key_id from show ssh-keys)
vastai show ssh-keys # List account SSH keys
vastai create ssh-key ~/.ssh/id_ed25519.pub # Add SSH key from file (do BEFORE create instance)
vastai create ssh-key # Generate new key if you don't have one
vastai create ssh-key "ssh-ed25519 AAAA..." # Add SSH key inline
vastai delete ssh-key <id> # Remove SSH key from account
vastai update ssh-key <id> "ssh-ed25519 AAAA..." # Update SSH key value
注意:ssh-url 会返回连接字符串,不会打开交互式会话。使用 $(vastai ssh-url <id>) 提取 URL,或解析 --raw JSON 输出。
文件复制
vastai copy <src> <dst> # Copy between instance and local
vastai copy local:./data/ <id>:/workspace/data/ # Local → instance (preferred format)
vastai copy <id>:/workspace/results/ local:./results/ # Instance → local
vastai copy <id-a>:/workspace/ <id-b>:/workspace/ # Instance → instance
vastai copy s3.101:/data/ C.<id>:/workspace/ # Cloud service → instance (s3.<connection-id>)
vastai copy V.1234:/file C.<id>:/workspace/ # Volume → instance
vastai copy V.1234:/file s3.101:/workspace/ # Volume → cloud service
# Legacy format also works: vastai copy 12345:./data ./local-data
vastai cloud copy --src ./data --dst s3://bucket/path \
--instance 12345 --connection <conn-id> \
--transfer "Instance To Cloud" # To cloud storage (add connection in UI on settings page)
vastai cancel copy <dst-id> # Cancel in-progress copy
云端复制标志: --src、--dst、--instance、--connection、--transfer
位置格式: [instance_id:]path(旧版)、C.instance_id:path、V.volume_id:path、cloud_service[.id]:path、local:path。卷复制目前仅支持复制到其他卷、实例或云服务,不支持复制到本地。切勿复制到 /root 或 /——这会破坏实例上的 ssh 权限,并导致后续复制失败。
日志与执行
vastai logs <id> # Container logs (last 1000 lines)
vastai logs <id> --tail 100 # Last 100 lines
vastai logs <id> --filter "error" # Grep filter
vastai execute <id> "nvidia-smi" # Run command on instance
vastai execute <id> "ls /workspace" --schedule DAILY # Scheduled execution
卷
vastai search volumes # Search available volume offers
vastai show volumes # List your volumes
vastai create volume <offer_id> [-s SIZE] [-n NAME] # Create volume (offer_id from search volumes)
vastai clone volume <source_id> <dest_id> [-s SIZE] # Clone volume (dest_id from search volumes)
vastai delete volume <id> # Delete volume
vastai create network-volume ... # Create network volume
vastai list network-volume # List network volumes
vastai take snapshot <instance_id> --repo REPO --docker_login_user USER --docker_login_pass PASS # Snapshot instance to volume
无服务器与部署
vastai show endpoints # List endpoint groups
vastai create endpoint --name "my-ep" ... # Create endpoint
vastai update endpoint <id> ... # Update endpoint
vastai delete endpoint <id> # Delete endpoint
vastai get endpt-logs <id> # Endpoint logs
vastai show workergroups # List worker groups
vastai create workergroup --name "wg" ... # Create worker group
vastai update workergroup <id> ... # Update worker group
vastai update workers <id> # Rolling update of workers
vastai delete workergroup <id> # Delete worker group
vastai get wrkgrp-logs <id> # Worker group logs
vastai show deployments # List deployments
vastai show deployment <id> # Deployment details
vastai show deployment-versions <id> # Version history
vastai delete deployment <id> # Delete deployment
vastai show scheduled-jobs # List scheduled jobs
vastai delete scheduled-job <id> # Delete scheduled job
模板
vastai search templates "pytorch" # Search templates
vastai create template --name "x" --image "img" # Create template
vastai update template <hash_id> ... # Update template (edits create a new version)
vastai delete template <id> # Delete template
账户与 API 密钥
vastai set api-key <key> # Save API key locally
vastai show api-key <id> # Show a specific key
vastai show api-keys # List all your API keys
vastai create api-key --name "ci" --permissions '{...}' # Create restricted key
vastai delete api-key <id> # Delete key
vastai reset api-key # Reset main key (get new from console)
vastai show user # Account info, credit balance
vastai show audit-logs # Account action history
vastai show connections # Cloud storage connections
vastai show ipaddrs # IP address history
账单
vastai show invoices-v1 # Charges/invoices (paginated)
vastai show invoices-v1 --charges # Charges only
vastai show invoices-v1 --invoices # Invoices only
vastai show invoices-v1 --start-date 2026-01-01 --end-date 2026-02-01
vastai show invoices-v1 --limit 50 --latest-first
vastai show deposit <id> # Reserved instance deposit info
团队
vastai create team --team-name "myteam" # Create team; your key stays on your personal account
vastai show members # List team members
vastai invite member --email [email protected] # Invite member
vastai remove member <id> # Remove member
vastai create team-role --name "viewer" ... # Create role
vastai show team-role <id> # Role details
vastai show team-roles # List roles
vastai update team-role <id> ... # Update role
vastai remove team-role <id> # Remove role
vastai destroy team # Delete team
环境变量
vastai show env-vars # List user env vars
vastai create env-var KEY val # Create env var
vastai update env-var KEY newval # Update env var
vastai delete env-var KEY # Delete env var
机器管理(主机)
vastai show machines # List your machines
vastai list machine <id> # List/unlist machine on marketplace
vastai show machine <id> # Machine details
vastai cleanup machine <id> # Clean up machine state
vastai schedule maint <id> ... # Schedule maintenance window
vastai cancel maint <id> # Cancel scheduled maintenance
vastai show maints # List maintenance windows
vastai unlist machine <id> # Remove from marketplace
常见错误
| 错误 | 原因 | 修复方法 |
|---|---|---|
401 Unauthorized | API 密钥无效或已过期 | vastai set api-key <new-key> |
Insufficient credits | 账户余额过低 | 在 https://cloud.vast.ai/billing/ 添加额度 |
No offers found | 筛选条件过于严格 | 放宽筛选条件,尝试 --no-default/-n |
Permission denied | 未附加 SSH 密钥 | 在 create instance 前运行 vastai create ssh-key |
Connection refused | 实例尚未运行 | 轮询 show instance <id>,直到 actual_status == "running" |
destroy instance 时卡住 | 确认提示正在等待输入 | 添加 -y 标志:vastai destroy instance <id> -y |
URL
控制台
https://console.vast.ai/instances/ # Your instances
https://console.vast.ai/create/ # Search GPU offers
https://console.vast.ai/manage-keys/ # Create and manage API keys
https://cloud.vast.ai/billing/ # Billing
API
https://console.vast.ai/api/v0/instances/ # Instances endpoint
https://console.vast.ai/api/v0/asks/ # Offers search
实例端口
访问实例上公开的端口:
ssh://root@<ssh_host>:<ssh_port> # SSH (from vastai ssh-url)
直接连接(创建时使用了 --direct):实例 JSON 中的 <direct_port_end> 字段。
文档
在此获取完整的文档索引:https://docs.vast.ai/llms.txt