Vast.ai CLI & Python SDK

repository·master·Indexed 42 days ago

https://github.com/vast-ai/vast-cli

A toolset for managing GPU cloud resources on Vast.ai. It includes a command-line interface (CLI) for terminal users, a Python SDK for programmatic resource management, and a serverless client for high-performance model inference.

Tokens
46.2K
Snippets
174
Records
215
Agent score
52%

What's inside Vast.ai

  1. Understand Instance Status values

    master

    Monitoring the actual_status of an instance is critical for managing costs and automation.

    actual_statusMeaning
    nullProvisioning
    createdInstance created, not yet provisioned
    loadingImage downloading / container starting
    runningActive — GPU charges apply
    stoppedHalted — disk charges only
    frozenPaused with memory — GPU charges apply
    exitedContainer process exited unexpectedly
    rebootingRestarting (transient)
    unknownNo recent heartbeat from host
    offlineHost disconnected from Vast servers

    Cost Warning: Storage charges begin at creation. GPU charges only begin once the status reaches running.

  2. Manage Vast CLI installation via environment variables

    master

    The Vast CLI installer attempts to modify your shell configuration (e.g., .bashrc, .zshrc) to add ~/.local/bin to your PATH and enable tab completion.

    • Disable PATH/Completion edits: Use the --no-modify-path flag during installation.
    • Non-interactive environments: The installer will not attempt to write to shell configuration files in non-interactive environments (like CI/CD pipelines).
    • Precedence: The installer adds a line to your shell configuration that sources <data-root>/env.sh. This ensures the managed CLI takes precedence over any pip-installed version of vastai on your PATH.
  3. Understand the Vast CLI installation and file layout

    master

    The Vast CLI uses a curl | bash installer that follows the XDG Base Directory Specification. This ensures that the program, configuration, cache, and state are stored in standard, overridable locations.

    On-disk layout:

    • Program binaries/venv: ~/.local/share/vastai/current/ (the single active virtual environment)
    • Binary symlink: ~/.local/bin/vastai (for easy access from your PATH)
    • Configuration: ~/.config/vastai
    • Cache: ~/.cache/vastai
    • Update-check state: ~/.local/state/vastai (used for persistent state like update throttle timestamps)

    Each root directory can be independently overridden using standard XDG_* environment variables.

  4. Understand what the documentation verification script checks

    master

    The verification script performs two levels of checks:

    1. Inventory Check (Always runs)

    • CLI commands missing docs: Commands found in vastai --help that lack a corresponding cli/reference/<command>.mdx file.
    • Stale CLI docs: MDX files for CLI commands that no longer exist.
    • SDK methods missing docs: Public methods on the VastAI class that lack a corresponding sdk/python/reference/<method>.mdx file.
    • Stale SDK docs: MDX files for SDK methods that no longer exist.

    2. Parameter Validation (Requires --check-params flag)

    • Undocumented flags/params: Flags in --help or method parameters not mentioned in the MDX page.
    • Stale flags/params: Parameters documented in MDX that no longer exist in the CLI/SDK.

    Naming Conventions

    The script matches entities using these conversions:

    • CLI commands: kebab-case (e.g., show-instances) matches doc filenames like show-instances.mdx.
    • SDK methods: snake_case (e.g., show_instances) matches doc filenames like show-instances.mdx.
    • Subcommands: For two-level commands like vastai show instances, the script expects the flattened kebab-case filename show-instances.mdx.
  5. Rent interruptible (spot) instances

    master

    Interruptible (spot) instances are cheaper but can be reclaimed by other users.

    Critical Note: When using vast.search_offers(type='bid', ...) to find spot offers, you must pass a bid_price to vast.create_instance(...). If you omit bid_price, the SDK defaults to renting the instance at the on-demand price (dph_total) instead of the interruptible price.

    If you are outbid, the instance moves to a stopped state (it is not destroyed) and storage charges continue. You can resume the instance by increasing your bid using vast.change_bid(id=..., price=...).

  6. Update the OpenAPI specification

    master

    To update the API documentation, follow these steps in the vast-ai/docs repository:

    1. Edit the specific endpoint file located at api-reference/openapi/yaml/<endpoint>.yaml.
    2. From the root of the vast-ai/docs repository, run the build command to regenerate the main api-reference/openapi.yaml file.
    3. Submit a Pull Request in the vast-ai/docs repository. CI will verify the spec, and Mintlify will deploy the changes to docs.vast.ai upon merging.

    For complete instructions, refer to the docs repo README.

    npm run build-openapi
  7. Quick start: Deploy a GPU instance

    master

    Follow these steps to authenticate, find an offer, and launch a GPU instance:

    1. Authenticate: Set your API key (obtain one at https://console.vast.ai/manage-keys/).
    2. Verify: Check your balance and auth status.
    3. SSH Setup: Register your public SSH key before creating an instance.
    4. Search: Find suitable GPU offers using query syntax.
    5. Create: Launch an instance using an OFFER_ID from the search results. Use --image pytorch/pytorch:@vastai-automatic-tag to automatically select the correct image tag.
    6. Connect: Poll the instance status until it is running, then retrieve the SSH URL.
    7. Cleanup: Destroy the instance to stop billing.
    # Authenticate
    vastai set api-key <YOUR_API_KEY>
    
    # Verify auth + check balance
    vastai show user
    
    # Register SSH key (do BEFORE create)
    vastai create ssh-key ~/.ssh/id_ed25519.pub
    
    # Search for offers (e.g., RTX 4090)
    vastai search offers 'gpu_name=RTX_4090 num_gpus=1 verified=true direct_port_count>=1 rentable=true' -o 'dlperf_usd-'
    
    # Create instance (replace <OFFER_ID> with ID from search)
    vastai create instance <OFFER_ID> --image pytorch/pytorch:@vastai-automatic-tag --disk 20 --ssh --direct
    
    # Poll until actual_status == "running"
    vastai show instance <INSTANCE_ID>
    
    # Get SSH connection string
    vastai ssh-url <INSTANCE_ID>
    
    # Upload files
    vastai copy local:./data/ <INSTANCE_ID>:/workspace/
    
    # Clean up (stops all billing; -y skips confirmation)
    vastai destroy instance <INSTANCE_ID> -y
  8. Uninstall the managed Vast CLI

    master

    To remove the managed installation, delete the program directory and the binary symlink. Note that your configuration (like API keys) and cache will be preserved.

    Default Uninstall Command:

    rm -rf "$XDG_DATA_HOME/vastai" ~/.local/bin/vastai

    If you used custom override variables during installation, ensure you use the corresponding paths for removal (e.g., if you set VASTAI_INSTALL_DIR, use that path instead of $XDG_DATA_HOME/vastai).

  9. Authenticate with the Vast.ai SDK

    master

    The SDK automatically looks for an API key in ~/.vast_api_key. You can obtain a key from the Vast.ai Console.

    To provide a key explicitly in your code, pass it to the VastAI constructor.

    from vastai import VastAI
    
    # Uses ~/.vast_api_key
    vast = VastAI()
    
    # Explicitly providing the key
    vast = VastAI(api_key="YOUR_API_KEY")