Skip to content
mentoor

Topic 2 of 6 · 4 min read

Serving a model with vLLM

Goal: Write a vLLM serve configuration for a model on two GPUs with a fixed context limit.

One vllm serve command starts an OpenAI-compatible API, and four settings decide whether it starts at all: the model, the context limit, the memory share and the GPU split.

Check yourself · 1 of 3

What does --max-model-len 16384 do in a vLLM command?

Choose one answer

Why it matters

Most serving failures happen at startup or on the first long request, with an out-of-memory error. If you know what each flag controls, you can read the error and fix the right one. The server speaks the OpenAI API, so existing apps and SDKs switch to it by changing a base URL.

The settings that matter

  • The model. A Hugging Face model id or a local path. A 70B model in 16-bit weights needs roughly 140 GB for weights alone (2 bytes per parameter), so it does not fit on one 80 GB GPU.
  • --max-model-len. The longest prompt plus answer the server accepts. A smaller value reserves less KV cache per request, so more users fit. Set it to what your product needs, not to the model's maximum.
  • --gpu-memory-utilization. The share of each GPU the engine may claim, such as 0.90. Weights, activations and KV cache all come out of it. Leave headroom if another process uses the GPU.
  • --tensor-parallel-size. Splits each layer across GPUs. Set it to the number of GPUs the model needs, such as 2.
  • --quantization or a pre-quantized model. AWQ or GPTQ weights use about a quarter of the memory of 16-bit weights, at some quality cost you should test.
  • --api-key. Requires a bearer token on requests.

Before and after

Weak: it works on the test box and fails in production.

vllm serve meta-llama/Llama-3.3-70B-Instruct

On two 80 GB GPUs this fails: the weights exceed one GPU, and the default context length asks for a very large KV cache.

Fixed:

vllm serve meta-llama/Llama-3.3-70B-Instruct \
  --tensor-parallel-size 2 \
  --max-model-len 16384 \
  --gpu-memory-utilization 0.90 \
  --api-key "$VLLM_API_KEY"

Then test it with curl http://localhost:8000/v1/models and one chat request using the same bearer token.

What goes wrong

  • Out-of-memory at startup. Lower --max-model-len, raise the tensor-parallel size, or use a quantized model. Names and defaults change between releases, so check vllm serve --help for your version.
  • Memory share too high. At 0.98 a monitoring agent or a second process on the GPU can push it over the edge.
  • Binding to the public internet. The server listens on all interfaces by default. Put it behind a firewall or gateway, and always set a key.
  • Testing only with short prompts. The server starts fine and fails on the first 12,000-token request.

Examples

A quantized model on one GPU

vllm serve Qwen/Qwen2.5-32B-Instruct-AWQ \
  --max-model-len 8192 \
  --gpu-memory-utilization 0.92 \
  --api-key "$VLLM_API_KEY"

AWQ weights shrink a 32B model to roughly a quarter of its 16-bit size, so it can fit on one 48 GB or 80 GB card. The 8,192 limit keeps KV cache small. Test quality on your own prompts, because quantization costs some accuracy.

Calling it like OpenAI

from openai import OpenAI

client = OpenAI(base_url="http://gpu-box:8000/v1", api_key="redacted")
reply = client.chat.completions.create(
    model="meta-llama/Llama-3.3-70B-Instruct",
    messages=[{"role": "user", "content": "Summarise our refund policy."}],
)
print(reply.choices[0].message.content)

Only base_url, api_key and the model name changed from a hosted-API call.

Key points

  • vllm serve starts an OpenAI-compatible server, so existing OpenAI SDK code needs only a new base URL.
  • --max-model-len sets the longest prompt plus answer; a lower value frees KV cache for more concurrent users.
  • --gpu-memory-utilization caps the share of each GPU the engine claims; leave headroom if anything else uses the GPU.
  • --tensor-parallel-size splits a model that does not fit on one GPU across several.
  • Test with a long prompt and a key before real traffic, and read vllm serve --help for your installed version.

Common mistakes

  • Leaving --max-model-len at the model's maximum when the product never sends more than a few thousand tokens.
  • Setting --gpu-memory-utilization to the maximum on a GPU shared with other processes.
  • Starting the server with no --api-key on a reachable network.
  • Declaring success after one short test prompt and never trying a prompt near the context limit.

Check yourself · 2 of 3

A 70B model in 16-bit weights is about 140 GB. You have two 80 GB GPUs. Which flag lets vLLM load it?

Choose one answer

Check yourself · 3 of 3

vLLM runs fine for a week, then fails on the first request carrying a 14,000-token document. Which cause is most likely?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic