Skip to content

First endpoint

Serve Qwen/Qwen3-0.6B with vLLM on one H100, wait until the model answers, send one chat request, and delete the endpoint.

View example files ↗: serve.py · Related reference: Endpoints →

An endpoint bills while it runs

From create until it is stopped or deleted. Closing the process does neither. This script deletes the endpoint in finally, also when a step fails or you press Ctrl+C.

examples/inference/vllm-endpoint/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()
uv run python examples/inference/vllm-endpoint/serve.py

What happens

  1. ns.Endpoint(...) describes the endpoint: what to serve, and on what machine. Token auth is the default. Nothing contacts Nebius yet.
  2. create() returns once Nebius has accepted the endpoint. It generates a random bearer token, keeps it in endpoint.token, and prints a notice.
  3. wait(probe="/v1/models") waits for RUNNING with a managed https:// URL, then until /v1/models answers. RUNNING alone means the tunnel is up, not that vLLM has loaded the model. Pulling the image and loading the model take a few minutes.
  4. The script sends one request to /v1/chat/completions with Authorization: Bearer <token>, prints the reply, and deletes the endpoint.

Endpoint states

stateDiagram-v2
    %% states: endpoint
    direction LR
    state "PROVISIONING<br>IMAGE_PULLING<br>STARTING" as Starting
    [*] --> Starting: create()
    Starting --> RUNNING
    Starting --> ERROR: no capacity
    RUNNING --> STOPPING: stop()
    STOPPING --> STOPPED
    RUNNING --> STOPPED: spot preemption
    STOPPED --> Starting: start()
    RUNNING --> DELETING: delete()
    STOPPED --> DELETING: delete()
    ERROR --> DELETING: delete()

RUNNING bills for compute. A STOPPED endpoint is not billed for compute or its disk, though mounted volumes still bill, and it keeps its id and URL. An endpoint in ERROR is not billed; delete it when you are done.

On a GPU, an endpoint runs on spot by default: a preemption moves it to STOPPED, and start() runs it again. Pass pricing="on-demand" for an endpoint that must stay up.

Next

Serve a model with vLLM covers the YAML recipe, calling the endpoint yourself, reloading it by id, and stop and start.