Skip to content

vast-ai/vast-cli / vastai

vastai

Flagged

Vast.ai CLI to manage GPU instances, volumes, serverless endpoints, and billing.

FileKindSize
SKILL.mdskill md18.6 KB
__init__.pyscript1.8 KB
_base.pyscript1.6 KB
api/__init__.pyscript31 B
api/auth.pyscript15.3 KB
api/billing.pyscript10.4 KB
api/client.pyscript9.1 KB
api/clusters.pyscript4.5 KB
api/deployments.pyscript1.4 KB
api/endpoints.pyscript12.0 KB
api/instances.pyscript17.1 KB
api/keys.pyscript4.3 KB
api/machines.pyscript7.4 KB
api/metrics.pyscript2.6 KB
api/offers.pyscript21.2 KB
api/price_increase.pyscript4.0 KB
api/query.pyscript12.2 KB
api/storage.pyscript14.5 KB
api/teams.pyscript4.4 KB
async_/__init__.pyscript79 B
async_/client.pyscript9.6 KB
async_/results.pyscript1.3 KB
cli/__init__.pyscript25 B
cli/commands/__init__.pyscript757 B
cli/commands/auth.pyscript42.8 KB
cli/commands/benchmarks.pyscript42.0 KB
cli/commands/billing.pyscript30.4 KB
cli/commands/clusters.pyscript7.8 KB
cli/commands/deployments.pyscript7.8 KB
cli/commands/endpoints.pyscript17.3 KB
cli/commands/instances.pyscript55.4 KB
cli/commands/keys.pyscript7.9 KB
cli/commands/machines.pyscript78.4 KB
cli/commands/metrics.pyscript12.7 KB
cli/commands/misc.pyscript11.0 KB
cli/commands/offers.pyscript30.0 KB
cli/commands/price_increase.pyscript14.5 KB
cli/commands/repl.pyscript719 B
cli/commands/storage.pyscript38.9 KB
cli/commands/teams.pyscript6.4 KB
cli/commands/uninstall.pyscript2.4 KB
cli/commands/update.pyscript3.1 KB
cli/display.pyscript20.1 KB
cli/main.pyscript11.5 KB
cli/parser.pyscript18.1 KB
cli/repl/__init__.pyscript318 B
cli/repl/bridge.pyscript7.3 KB
cli/repl/catalog.pyscript6.3 KB
cli/repl/completion.pyscript8.2 KB
cli/repl/session.pyscript7.3 KB
cli/self_test/__init__.pyscript46 B
cli/self_test/machine_diagnostics.pyscript21.0 KB
cli/self_test/runtime_diagnostics.pyscript18.6 KB
cli/self_test/support_bundle.pyscript11.0 KB
cli/selfupdate.pyscript14.0 KB
cli/util.pyscript26.3 KB
cli/utils.pyscript577 B
data/__init__.pyscript303 B
data/deployment.pyscript3.5 KB
data/endpoint.pyscript2.1 KB
data/instance.pyscript3.6 KB
data/offer.pyscript4.4 KB
data/query.pyscript12.8 KB
data/workergroup.pyscript1.4 KB
pdf/__init__.pyscript0 B
pdf/vast.ai-logo-50pct.pngbinary7.4 KB
pdf/vast.ai-logo.pngbinary10.1 KB
pdf/vast_pdf.pyscript19.4 KB
pdf/watcher.shexecutablescript390 B
sdk.pyscript52.2 KB
serverless/__init__.pyscript193 B
serverless/client/__init__.pyscript346 B
serverless/client/client.pyscript36.7 KB
serverless/client/connection.pyscript11.5 KB
serverless/client/endpoint.pyscript6.9 KB
serverless/client/managed.pyscript7.3 KB
serverless/client/request_status.pyscript371 B
serverless/client/session.pyscript2.8 KB
serverless/client/worker.pyscript1.3 KB
serverless/remote/__init__.pyscript245 B
serverless/remote/base.pyscript6.3 KB
serverless/remote/deploy.pyscript12.6 KB
serverless/remote/serialization.pyscript3.7 KB
serverless/remote/serve.pyscript8.6 KB
serverless/remote/serve_deployment.pyscript6.3 KB
serverless/remote/utils.pyscript9.7 KB
serverless/server/__init__.pyscript0 B
serverless/server/lib/__init__.pyscript0 B
serverless/server/lib/backend.pyscript43.3 KB
serverless/server/lib/data_types.pyscript10.6 KB
serverless/server/lib/metrics.pyscript12.2 KB
serverless/server/lib/server.pyscript3.2 KB
serverless/server/worker.pyscript17.1 KB
sync/__init__.pyscript76 B
sync/client.pyscript4.9 KB
sync/results.pyscript1.2 KB
utils.pyscript12.2 KB

api/client.py

View on GitHub
"""HTTP client for the Vast.ai API."""

import os
import re
import json
import sys
import shlex
import time
import requests
from urllib.parse import quote_plus, urlsplit
from typing import Dict, Optional

from vastai.utils import VERSION

# Emoji support
_HAS_EMOJI = sys.stdout.encoding and 'utf' in sys.stdout.encoding.lower()
INFO = "\u2139\ufe0f" if _HAS_EMOJI else "[i]"

server_url_default = os.getenv("VAST_URL") or "https://console.vast.ai"

# Our own environments redirect between hosts here; see VastSession.same_service.
VAST_DOMAIN = "vast.ai"

# Status codes that indicate a transient server-side issue worth retrying.
# 429 = rate limited; 502/503/504 = gateway/upstream errors that usually clear.
_RETRYABLE_STATUS = {429, 502, 503, 504}

# Longest server Retry-After we wait out inline; a longer one is returned to the caller.
_MAX_RETRY_AFTER_SECONDS = 60

# Transport-level exceptions worth retrying (network hiccups, slow peers).
# Other requests.RequestException subclasses (e.g. InvalidURL, TooManyRedirects)
# propagate immediately — retrying them would just burn time.
_RETRYABLE_EXC = (
    requests.exceptions.ConnectionError,
    requests.exceptions.Timeout,
)

# Default per-request timeout. Conservative enough for slow operations like
# log retrieval and instance creation; callers can override per-call.
_DEFAULT_TIMEOUT_SECONDS = 120


def as_curl_command(prep) -> str:
    """Render a prepared request as a runnable, one-flag-per-line curl command."""
    parts = ["curl"]
    if prep.method != "GET" or prep.body:
        parts.append(f"-X {prep.method}")
    auth = prep.headers.get("Authorization")
    if auth:
        parts.append("-H " + shlex.quote(f"Authorization: {auth}"))
    if prep.body:
        body = prep.body.decode() if isinstance(prep.body, bytes) else prep.body
        parts.append("-H " + shlex.quote("Content-Type: application/json"))
        parts.append("-d " + shlex.quote(body))
    parts.append(shlex.quote(prep.url))
    return " \\\n  ".join(parts)


class VastSession(requests.Session):
    """A session that keeps our auth header across redirects within Vast's domain."""

    def __init__(self, server_url: str):
        super().__init__()
        self._origin = urlsplit(server_url)

    def rebuild_auth(self, prepared_request, response):
        if not self.same_service(urlsplit(prepared_request.url)):
            super().rebuild_auth(prepared_request, response)

    def same_service(self, target) -> bool:
        """Whether a redirect target is still the service we were pointed at.

        Same scheme and port, and either the identical host or another host
        under vast.ai, which is where our own environments redirect
        (candidate.vast.ai -> candidate-server.vast.ai). Everything else needs
        an exact host, so a custom --url never widens the trust boundary.
        """
        origin = self._origin
        if target.scheme != origin.scheme or port_of(target) != port_of(origin):
            return False
        a, b = (origin.hostname or "").lower(), (target.hostname or "").lower()
        return bool(a) and (a == b or (in_vast_domain(a) and in_vast_domain(b)))


def is_search_quota_exceeded(r) -> bool:
    """Whether ``r`` is a daily search quota 429.

    The quota only resets at UTC midnight, so retrying it just burns requests.
    """
    if r.status_code != 429:
        return False
    try:
        return r.json().get("error") == "search_quota_exceeded"
    except (ValueError, AttributeError):
        return False


def retry_delay(r, backoff: float) -> Optional[float]:
    """Seconds to wait before retrying ``r``, or None when it should not be retried.

    Honors the server's ``Retry-After`` (rate limit token refill) and falls back to
    ``backoff`` when it is absent or not a number. A search quota 429 or a wait over
    ``_MAX_RETRY_AFTER_SECONDS`` is not retried.
    """
    if is_search_quota_exceeded(r):
        return None
    try:
        wait = float(r.headers["Retry-After"])
    except (KeyError, TypeError, ValueError):
        return backoff
    return wait if wait <= _MAX_RETRY_AFTER_SECONDS else None


def port_of(parts) -> Optional[int]:
    return parts.port or {"https": 443, "http": 80}.get(parts.scheme)


def in_vast_domain(host: str) -> bool:
    return host == VAST_DOMAIN or host.endswith("." + VAST_DOMAIN)


class VastClient:
    """HTTP client for Vast.ai API requests."""

    def __init__(self, api_key=None, server_url=None, retry=3, explain=False, curl=False,
                 timeout=_DEFAULT_TIMEOUT_SECONDS, client_type="sdk"):
        self.api_key = api_key
        self.server_url = server_url or server_url_default
        self.retry = retry
        self.explain = explain
        self.curl = curl
        self.timeout = timeout
        self.user_agent = f"vastai-{client_type}/{VERSION}"

    def _build_url(self, subpath: str, query_args: Optional[Dict] = None) -> str:
        """Build full API URL from subpath and optional query args."""
        if query_args is None:
            query_args = {}
        if not re.match(r"^/api/v(\d)+/", subpath):
            subpath = "/api/v0" + subpath

        if query_args:
            query_json = "&".join(
                "{x}={y}".format(x=x, y=quote_plus(y if isinstance(y, str) else json.dumps(y)))
                for x, y in query_args.items()
            )
            result = self.server_url + subpath + "?" + query_json
        else:
            result = self.server_url + subpath

        if self.explain:
            print("query args:")
            print(query_args)
            print("")
            print(f"base: {self.server_url + subpath + '?'} + query: ")
            print(result)
            print("")
        return result

    def _build_headers(self) -> Dict:
        """Build request headers with auth."""
        result = {"User-Agent": self.user_agent}
        if self.api_key is not None:
            result["Authorization"] = "Bearer " + self.api_key
        return result

    def _request(self, method: str, url: str, headers: Dict, json_data=None,
                 timeout: Optional[float] = None) -> requests.Response:
        """Execute HTTP request with retry/timeout/exception handling.

        Retries are attempted on:
          - ``_RETRYABLE_STATUS`` codes (429, 502, 503, 504), waiting per ``retry_delay``
          - ``_RETRYABLE_EXC`` transport exceptions (ConnectionError, Timeout)

        After exhausting retries, the last response is returned (for status-code
        retries) or the last exception is re-raised (for transport exceptions).
        Non-retryable ``requests`` exceptions propagate immediately.
        """
        effective_timeout = timeout if timeout is not None else self.timeout
        t = 0.15
        r = None
        for i in range(0, self.retry):
            req = requests.Request(method=method, url=url, headers=headers, json=json_data)
            session = VastSession(self.server_url)
            prep = session.prepare_request(req)
            if self.explain:
                print(f"\n{INFO}  Prepared Request:")
                print(f"{prep.method} {prep.url}")
                print(f"Headers: {json.dumps(headers, indent=1)}")
                print(f"Body: {json.dumps(json_data, indent=1)}" + "\n" + "_" * 100 + "\n")

            if self.curl:
                print("\n" + as_curl_command(prep) + "\n")
                sys.exit(0)

            try:
                r = session.send(prep, timeout=effective_timeout)
            except _RETRYABLE_EXC:
                if i == self.retry - 1:
                    raise
                time.sleep(t)
                t *= 1.5
                continue

            delay = retry_delay(r, t) if r.status_code in _RETRYABLE_STATUS else None
            if delay is not None and i < self.retry - 1:
                time.sleep(delay)
                t *= 1.5
                continue
            break
        return r

    def get(self, subpath: str, query_args: Optional[Dict] = None, json_data=None,
            timeout: Optional[float] = None) -> requests.Response:
        url = self._build_url(subpath, query_args)
        headers = self._build_headers()
        return self._request('GET', url, headers, json_data, timeout=timeout)

    def post(self, subpath: str, query_args: Optional[Dict] = None, json_data=None,
             timeout: Optional[float] = None) -> requests.Response:
        url = self._build_url(subpath, query_args)
        headers = self._build_headers()
        return self._request('POST', url, headers, json_data if json_data is not None else {}, timeout=timeout)

    def put(self, subpath: str, query_args: Optional[Dict] = None, json_data=None,
            timeout: Optional[float] = None) -> requests.Response:
        url = self._build_url(subpath, query_args)
        headers = self._build_headers()
        return self._request('PUT', url, headers, json_data if json_data is not None else {}, timeout=timeout)

    def delete(self, subpath: str, query_args: Optional[Dict] = None, json_data=None,
               timeout: Optional[float] = None) -> requests.Response:
        url = self._build_url(subpath, query_args)
        headers = self._build_headers()
        return self._request('DELETE', url, headers, json_data if json_data is not None else {}, timeout=timeout)
slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack."Stars: 198kUpdated Claude CodeCodexVerifiedInstall
ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt.Stars: 194kUpdated Claude CodeCodexVerifiedInstall