Sub topic 9
Serving an OpenAI-Compatible API with `vllm serve`
Assume you know basic command-line use, Python, GPU memory, and HTTP APIs. You also need a GPU with enough memory for the model and its context window.
The vllm serve command starts an HTTP server around an open-weight model. The server exposes endpoints that follow the OpenAI API format, so many existing clients can call it by changing only the base URL and model name.
Start the server
Install vLLM in an environment that can access your GPU. Then start a model:
vllm serve Qwen/Qwen2.5-1.5B-Instruct \
--max-model-len 4096 \
--gpu-memory-utilization 0.85 \
--port 8000The first argument is a Hugging Face model identifier. vLLM downloads the model if it is not already cached. --max-model-len limits the total number of input and output tokens in one request. --gpu-memory-utilization sets the fraction of GPU memory vLLM may use for model weights, the KV cache, and runtime buffers. --port selects the TCP port for the service.
The process stays in the foreground and prints startup logs. Wait until it reports that the server is ready before sending requests.
flowchart LR
A[OpenAI client] -->|HTTP JSON request| B[vLLM server]
B --> C[Tokenizer and chat template]
C --> D[GPU model execution]
D --> B
B -->|OpenAI-compatible response| ACall the API
The OpenAI Python client accepts a base_url. Point it at vLLM's /v1 path:
from openai import OpenAI
client = OpenAI(
base_url="http://localhost:8000/v1",
api_key="EMPTY",
)
response = client.chat.completions.create(
model="Qwen/Qwen2.5-1.5B-Instruct",
messages=[
{"role": "user", "content": "What is continuous batching?"}
],
temperature=0.2,
max_tokens=150,
)
print(response.choices[0].message.content)The api_key value is required by the client library, but this local server does not authenticate it by default. Do not treat EMPTY as secure authentication when exposing the service beyond a trusted machine.
The model value must match the model name known by the server. If you serve a local directory, use that directory or configure a served model name according to your vLLM version. A mismatch commonly produces a “model not found” response.
You can also test the endpoint directly:
curl http://localhost:8000/v1/modelsThis request lists the model identifier that clients should send.
Choose server settings
Start with a smaller context length when GPU memory is limited. A larger context reserves more KV-cache capacity per request and can reduce concurrency. Later chapters cover quantization, multi-GPU execution, and prefix caching.
Exercise
Which two client settings must usually change when moving an OpenAI client from a hosted service to a local vLLM server?
Show answer
Change base_url to the vLLM server's /v1 endpoint and set model to the identifier served by vLLM.