$npx -y skills add vast-ai/vast-cli --skill vastai_sdkVast.ai Python SDK — high-level API for GPU instances, volumes, serverless endpoints, and billing.
| 1 | # Vast.ai Python SDK (`vastai` / `vastai_sdk`) |
| 2 | |
| 3 | The `vastai` package provides a Python SDK for managing GPU instances, volumes, serverless endpoints, and billing on Vast.ai. The `vastai_sdk` package is a backward-compatibility shim that re-exports `vastai`. |
| 4 | |
| 5 | ## Installation |
| 6 | |
| 7 | ```bash |
| 8 | pip install vastai |
| 9 | ``` |
| 10 | |
| 11 | For serverless and async support: |
| 12 | ```bash |
| 13 | pip install "vastai[serverless]" |
| 14 | ``` |
| 15 | |
| 16 | ## Authentication |
| 17 | |
| 18 | The SDK reads the API key from `~/.vast_api_key` by default. You can also pass it explicitly: |
| 19 | |
| 20 | ```python |
| 21 | from vastai import VastAI |
| 22 | vast = VastAI() # reads ~/.vast_api_key |
| 23 | vast = VastAI(api_key="YOUR_API_KEY") # explicit key |
| 24 | ``` |
| 25 | |
| 26 | Get your API key from https://console.vast.ai/manage-keys/ |
| 27 | |
| 28 | ## Backward Compatibility |
| 29 | |
| 30 | The old `vastai_sdk` import still works: |
| 31 | |
| 32 | ```python |
| 33 | from vastai_sdk import VastAI # equivalent to: from vastai import VastAI |
| 34 | ``` |
| 35 | |
| 36 | ## VastAI Class (High-Level SDK) |
| 37 | |
| 38 | ```python |
| 39 | from vastai import VastAI |
| 40 | vast = VastAI(api_key=None, server_url=None, retry=3, raw=False, quiet=False) |
| 41 | ``` |
| 42 | |
| 43 | ### Instance Management |
| 44 | |
| 45 | ```python |
| 46 | # List all your instances |
| 47 | instances = vast.show_instances() |
| 48 | |
| 49 | # Get a single instance |
| 50 | instance = vast.show_instance(id=12345) |
| 51 | |
| 52 | # Search GPU offers |
| 53 | offers = vast.search_offers(query='gpu_name=RTX_4090 num_gpus>=4 reliability>0.99') |
| 54 | |
| 55 | # Create an instance from an offer |
| 56 | result = vast.create_instance(id=<offer_id>, image="pytorch/pytorch:latest", disk=50) |
| 57 | |
| 58 | # Lifecycle |
| 59 | vast.start_instance(id=12345) |
| 60 | vast.stop_instance(id=12345) |
| 61 | vast.reboot_instance(id=12345) |
| 62 | vast.destroy_instance(id=12345) |
| 63 | |
| 64 | # Label an instance |
| 65 | vast.label_instance(id=12345, label="my-training-run") |
| 66 | |
| 67 | # Get SSH connection string |
| 68 | ssh_url = vast.ssh_url(id=12345) # returns "ssh -p PORT user@host" |
| 69 | scp_url = vast.scp_url(id=12345) # returns scp-compatible URL |
| 70 | ``` |
| 71 | |
| 72 | ### Interruptible (spot) rentals |
| 73 | |
| 74 | Interruptible (spot) instances are priced below on-demand instances, but can be interrupted at any time by another user with a lower bid. Note: `vast.search_offers(type='bid', ...)` exposes `min_bid`, but `vast.create_instance(...)` defaults to **on-demand at `dph_total`** unless you pass `bid_price=<floor>`. Always pass `bid_price` after a `type='bid'` search, otherwise the instance will be rented as an on-demand instance/price instead of as an interruptible. |
| 75 | |
| 76 | When outbid, the instance moves to `stopped` (not destroyed) and storage charges continue. Resume by raising the bid via `vast.change_bid(id=..., price=...)`. |
| 77 | |
| 78 | ### Search |
| 79 | |
| 80 | ```python |
| 81 | # Search GPU offers (use help(vast.search_offers) for full query syntax) |
| 82 | offers = vast.search_offers(query='gpu_name=RTX_3090 num_gpus>=2') |
| 83 | |
| 84 | # Search volume offers |
| 85 | volumes = vast.search_volumes(query='...') |
| 86 | |
| 87 | # Search network volumes |
| 88 | net_vols = vast.search_network_volumes() |
| 89 | |
| 90 | # Search templates |
| 91 | templates = vast.search_templates() |
| 92 | |
| 93 | # Search invoices |
| 94 | invoices = vast.search_invoices() |
| 95 | ``` |
| 96 | |
| 97 | ### Serverless Deployments |
| 98 | |
| 99 | ```python |
| 100 | # List all deployments |
| 101 | deployments = vast.show_deployments() |
| 102 | |
| 103 | # Get a deployment |
| 104 | deployment = vast.show_deployment(id=42) |
| 105 | |
| 106 | # Delete a deployment |
| 107 | vast.delete_deployment(id=42) |
| 108 | ``` |
| 109 | |
| 110 | ### Machine Management (Hosting) |
| 111 | |
| 112 | ```python |
| 113 | machines = vast.show_machines() |
| 114 | machine = vast.show_machine(id=10) |
| 115 | vast.list_machine(id=10, price_gpu=0.30) |
| 116 | vast.unlist_machine(id=10) |
| 117 | ``` |
| 118 | |
| 119 | ### SSH Keys |
| 120 | |
| 121 | ```python |
| 122 | keys = vast.show_ssh_keys() |
| 123 | vast.create_ssh_key(ssh_key="ssh-rsa AAAA...") |
| 124 | vast.delete_ssh_key(id=5) |
| 125 | ``` |
| 126 | |
| 127 | ### Team Management |
| 128 | |
| 129 | ```python |
| 130 | members = vast.show_members() |
| 131 | vast.invite_member(email="user@example.com", role="developer") |
| 132 | vast.remove_member(id=7) |
| 133 | ``` |
| 134 | |
| 135 | ## SyncClient (Low-Level Sync) |
| 136 | |
| 137 | `SyncClient` provides typed, synchronous access to GPU offers and instances. |
| 138 | |
| 139 | ```python |
| 140 | from vastai import SyncClient |
| 141 | |
| 142 | client = SyncClient(api_key="YOUR_API_KEY") # or reads ~/.vast_api_key |
| 143 | |
| 144 | # Search offers with structured filters |
| 145 | offers = client.search( |
| 146 | num_gpus=2, |
| 147 | gpu_name="RTX_4090", |
| 148 | min_reliability=0.99, |
| 149 | max_dph_total=2.0, |
| 150 | ) |
| 151 | |
| 152 | # Create an instance |
| 153 | instance = client.create_instance( |
| 154 | offer_id=<id>, |
| 155 | image="pytorch/pytorch:latest", |
| 156 | disk_gb=50, |
| 157 | ) |
| 158 | |
| 159 | # List your instances |
| 160 | instances = client.show_instances() # returns list[SyncInstance] |
| 161 | |
| 162 | # Destroy an instance |
| 163 | client.destroy_instance(instance_or_id=12345) |
| 164 | ``` |
| 165 | |
| 166 | ## AsyncClient (Low-Level Async) |
| 167 | |
| 168 | `AsyncClient` provides async access to GPU offers and instances. Use as an async context manager. |
| 169 | |
| 170 | ```python |
| 171 | import asyncio |
| 172 | from vastai import AsyncClient |
| 173 | |
| 174 | async def main(): |
| 175 | async with AsyncClient(api_key="YOUR_API_KEY") as client: |
| 176 | # Search offers |
| 177 | offers = await client.search(num_gpus=1, gpu_name="A100") |
| 178 | |
| 179 | # Create instance |
| 180 | instance = await client.create_instance(offer_id=<id>, image="ubuntu:22.04") |
| 181 | |
| 182 | # List instances |
| 183 | instances = await client.show_instances() # |