Skip to content

Serve a model with vLLM

Serves Qwen/Qwen3-0.6B with vLLM's OpenAI-compatible server on one H100 endpoint behind a bearer token, sends one chat request, and deletes the endpoint.

View example files ↗ · Related reference: Endpoints →

What this example does

  • serve.py creates the endpoint, waits until /v1/models answers, sends one request to /v1/chat/completions, prints the reply, and deletes the endpoint in finally, also when a step fails or you press Ctrl-C.
  • endpoint.yaml is the same spec as YAML.
  • The endpoint uses token auth, the default: create() generates a random token, keeps it in endpoint.token, and callers send Authorization: Bearer <token>.

Requirements

Runtime and compute

Machine gpu="h100", spot (the default on a GPU). A preemption stops the endpoint; start() runs it again
Port 8000, served at a managed https:// URL
Disk 500Gi
Image vllm/vllm-openai:v0.19.1

An endpoint bills for compute from create until it is stopped or deleted. Closing the process does neither.

Run it

uv run python examples/inference/vllm-endpoint/serve.py

The same spec from YAML:

import nebius_serverless as ns

spec = ns.EndpointSpec.from_yaml("examples/inference/vllm-endpoint/endpoint.yaml")
print(ns.Endpoint(spec).dry_run())  # the create request; nothing is created

Expected output

create() prints the endpoint id and a notice that a token was generated. wait(probe="/v1/models") prints each state, then the URL under RUNNING, then the probe's answers until the model responds. Pulling the image and loading the model take a few minutes. The script then prints the model's reply, and delete() prints that deletion was accepted.

Call it yourself

This client does not wrap inference. Send HTTP to endpoint.url with the token:

import urllib.request

request = urllib.request.Request(
    endpoint.url.rstrip("/") + "/v1/models",
    headers={"Authorization": f"Bearer {endpoint.token}"},
)
with urllib.request.urlopen(request) as response:
    print(response.status)

Keep it, stop it, start it again

Save endpoint.id to come back later. endpoints.get fetches the endpoint and creates nothing; reveal_token() fetches its token:

endpoint = ns.endpoints.get(saved_id)
token = endpoint.reveal_token()

A stopped endpoint is not billed for compute or its disk; mounted volumes still bill. It comes back at the same URL:

endpoint.stop()
endpoint.wait(until="stopped")
endpoint.start()
endpoint.wait(probe="/v1/models")  # the model loads again

How to adapt it

  • Serve another model: change --model in the command, and MODEL in serve.py.
  • Spot pricing: pricing="spot". A preempted spot endpoint goes to STOPPED; start() resumes it.
  • Keep the token in MysteryBox for production: token_secret="mbsec-…", whose payload has AUTH_TOKEN. Clients then read the token from MysteryBox.
  • auth="none" makes a public URL that anyone who has it can call, at your cost. Use it only on purpose.

Troubleshooting

  • The endpoint ends in ERROR before RUNNING. Usually there was no capacity for the machine. An endpoint in ERROR is not billed; display() shows the reason. Delete it, then retry later or in another region.
  • RUNNING, but requests fail. RUNNING with a URL means the tunnel is up, not that vLLM has loaded the model. Wait with probe=, as serve.py does.
  • AccessError from the probe. The endpoint answered 401 or 403: the handle has no token or the wrong one. Call reveal_token() or pass token= to wait.
  • WaitTimeout after 60 minutes. The endpoint is unchanged and may still be billing. Call wait() again, or delete().

Source

serve.py
"""Serve a small model with vLLM on an endpoint, call it, then delete it.

``gpu="h100"`` with token auth. Uses the active Nebius profile. The same
recipe as YAML is ``endpoint.yaml`` in this folder.

The endpoint bills for compute while it runs. This script deletes it in
``finally``, including when a step fails or you press Ctrl-C.
"""

import json
import urllib.request

import nebius_serverless as ns

MODEL = "Qwen/Qwen3-0.6B"


def main() -> None:
    """Create the endpoint, wait for the model, send one request, delete."""

    # Describe what should serve, then create it once. Omit command to keep
    # the image entrypoint. The generated token is endpoint.token.
    endpoint = ns.Endpoint(
        command=[
            "bash",
            "-c",
            "python3 -m vllm.entrypoints.openai.api_server "
            f"--model {MODEL} --host 0.0.0.0 --port 8000",
        ],
        image="vllm/vllm-openai:v0.19.1",
        gpu="h100",
        port=8000,
        disk="500Gi",
        name="qwen-tiny",
    ).create()
    try:
        # RUNNING with a URL, then /v1/models answers: the model is loaded.
        endpoint.wait(probe="/v1/models")
        # ERROR and STOPPED end the wait as data. An ERROR record can still
        # list the URL, so check the state, not the URL.
        if endpoint.status != "RUNNING":
            raise SystemExit(f"{endpoint.id} ended {endpoint.status}")
        print(_chat(endpoint, "Say hello in five words."))
    finally:
        endpoint.delete()


def _chat(endpoint: ns.Endpoint, prompt: str) -> str:
    assert endpoint.url is not None
    body = {
        "model": MODEL,
        "messages": [{"role": "user", "content": prompt}],
        "max_tokens": 64,
    }
    request = urllib.request.Request(
        endpoint.url.rstrip("/") + "/v1/chat/completions",
        data=json.dumps(body).encode(),
        headers={
            "Authorization": f"Bearer {endpoint.token}",
            "Content-Type": "application/json",
        },
    )
    with urllib.request.urlopen(request, timeout=60) as response:
        payload = json.load(response)
    return payload["choices"][0]["message"]["content"]


if __name__ == "__main__":
    main()
endpoint.yaml
schema_version: 1
kind: endpoint
image: vllm/vllm-openai:v0.19.1
command:
  - bash
  - -c
  - python3 -m vllm.entrypoints.openai.api_server --model Qwen/Qwen3-0.6B --host 0.0.0.0 --port 8000
gpu: h100
port: 8000
disk: 500Gi
name: qwen-tiny