If you are transitioning from using managed APIs (like OpenAI or Gemini) to self-hosting open-weight models (like Llama 3 or DeepSeek), vLLM is the industry standard inference engine you must learn.
[!NOTE] Prerequisites: This guide focuses on vLLM architecture and configuration. For a specific cloud deployment walkthrough, see our guide on Deploying DeepSeek R1 on AWS with vLLM.
1. What is vLLM?
vLLM is a high-throughput, memory-efficient inference and serving engine for Large Language Models (LLMs). Developed initially at UC Berkeley, it is now the backbone of almost every major AI infrastructure provider.
It provides an OpenAI-compatible HTTP server out-of-the-box, meaning you can swap your api_base URL from OpenAI to your local vLLM instance without changing any of your application code.
2. Why it matters
Serving LLMs in production is bottlenecked by memory, not compute. Specifically, the Key-Value (KV) cache grows linearly with sequence length and can fragment GPU VRAM, leading to Out-Of-Memory (OOM) errors.
- Massive Throughput: Thanks to continuous batching, vLLM can process 20-40x more requests per second than naive HuggingFace Transformers implementations.
- Zero Memory Waste: Its PagedAttention algorithm completely eliminates KV cache fragmentation.
- OpenAI Compatibility: Drop-in replacement for OpenAI endpoints.
3. Architecture Diagram
Below is a visualization of how vLLM handles concurrent requests using PagedAttention:
Architecture showing continuous batching and the paging of KV cache blocks across GPU VRAM.
4. Installation & Setup
vLLM is distributed as a Python package, but the safest and easiest way to deploy it is via the official Docker image to avoid CUDA version conflicts.
# Pull the official vLLM Docker image
docker pull vllm/vllm-openai:latest
Before deploying, you must know how much VRAM your chosen model requires. Use our free GPU VRAM Calculator to estimate your memory footprint.
5. Code Examples
Deploying a Llama 3 API Server
Here is the standard Docker run command to spin up an OpenAI-compatible API server hosting an 8-billion parameter model on a single GPU.
docker run --gpus all \
-v ~/.cache/huggingface:/root/.cache/huggingface \
-p 8000:8000 \
--ipc=host \
vllm/vllm-openai:latest \
--model meta-llama/Meta-Llama-3-8B-Instruct \
--dtype bfloat16 \
--max-model-len 8192
Querying your Local vLLM Server
Once the server is running on port 8000, you can query it using the standard OpenAI Python SDK:
from openai import OpenAI
client = OpenAI(
base_url="http://localhost:8000/v1",
api_key="vllm-local" # vLLM ignores this by default
)
completion = client.chat.completions.create(
model="meta-llama/Meta-Llama-3-8B-Instruct",
messages=[
{"role": "user", "content": "Explain PagedAttention."}
]
)
print(completion.choices[0].message.content)
6. Best Practices
- Quantization: Use AWQ or GPTQ quantized models (e.g., 4-bit) if you are constrained on VRAM. Add
--quantization awqto your launch command. - GPU Memory Utilization: vLLM reserves 90% of your GPU VRAM by default for the KV cache. If you are running other processes on the same GPU, lower this by setting
--gpu-memory-utilization 0.5. - Tensor Parallelism: If a model doesn’t fit on one GPU, split it across multiple GPUs by adding
--tensor-parallel-size 2(or 4, 8).
7. Common Errors & Troubleshooting
Error: CUDA out of memory
Cause: The model weights plus the KV cache exceed your GPU’s physical VRAM.
Fix: Reduce --max-model-len (e.g., from 32000 to 8192) or reduce --gpu-memory-utilization. See our complete vLLM OOM Troubleshooting Guide.
Error: ValueError: The model's max seq len is larger than the maximum capacity
Cause: You are trying to use a context window that vLLM cannot allocate enough KV cache blocks for.
Fix: Lower --max-model-len to force a smaller context window limit.
8. Benchmarks & Comparisons
How does vLLM compare to other local inference engines?
| Engine | Best Use Case | Key Feature |
|---|---|---|
| vLLM | Production Cloud APIs | PagedAttention, High Throughput |
| Ollama | Local Developer Testing | Simple CLI, Modelfiles |
| llama.cpp | CPU/Mac Inference | GGUF Quantization, Apple Silicon |
| TensorRT-LLM | Extreme NVIDIA Optimization | Highest possible throughput on Hopper |
9. Related Developer Tools
- GPU VRAM Calculator: Estimate exactly how much GPU memory your model requires for weights, KV cache, and context length.
- API Payload Builder: Test your local vLLM endpoint by constructing complex ChatML payloads.
10. FAQ
Can vLLM run on my MacBook? No. While there are experimental branches, vLLM is heavily optimized for NVIDIA (CUDA) and AMD (ROCm) GPUs. For MacBooks, use Ollama or llama.cpp.
Does vLLM support LoRA adapters?
Yes. You can load multiple distinct LoRA adapters dynamically at runtime without taking a massive VRAM penalty using the --enable-lora flag.
