Education › Inference › Stage 4: Hands-on model serving

Serving engines: vLLM, SGLang, TensorRT-LLM

What each engine optimises, how to launch them locally, and how to choose.

Intermediate ~35 min read Module 7 of 15

You will rarely write an inference engine, but you will choose, deploy, tune and operate one. Three open-source engines dominate datacenter LLM serving: vLLM, SGLang and NVIDIA TensorRT-LLM. All three expose an OpenAI-compatible API and share the same core ideas, but they differ in where they shine. This lesson gets each one running locally, explains what each optimises, and gives you a way to choose that is grounded in your own benchmarks rather than someone else's chart.

After this module you can
  • Launch vLLM, SGLang and TensorRT-LLM as OpenAI-compatible servers
  • Explain the features all modern engines share and where they differ
  • Choose an engine based on hardware, workload shape and operational needs
  • Run the same client against any engine by changing only the base URL
  • Place lighter local runtimes such as llama.cpp correctly for development

What every modern engine does

The engines have converged on the same toolkit, so learn it once. Continuous batching adds and removes requests at every step. Paged KV-cache management allocates cache in blocks. Prefix caching reuses cache for shared prompt prefixes. Chunked prefill keeps long prompts from stalling decode. CUDA graphs and fused kernels cut launch overhead. Quantization (FP8 and 4-bit formats), tensor and pipeline parallelism, speculative decoding, LoRA adapters, structured output and tool-call parsing are standard features.

Because all three speak the OpenAI API, your application, gateway and benchmark client do not change when you switch engines; only the base URL and model name do. That makes trying several engines on your own workload cheap, and it is the right way to decide.

The differences that remain are in defaults, kernels and focus: which models and GPUs get optimised first, how aggressively each engine schedules prefill against decode, how its prefix cache is indexed, how it exposes metrics, and how often it releases. Those differences can swing results by large margins on one workload and vanish on another, which is why the comparison below ends with a benchmark rather than a verdict.

vLLM

vLLM is the broad, community default: the widest model and hardware support (NVIDIA, AMD, Intel, TPUs and more through plugins), PagedAttention, a large feature set and a big ecosystem that includes KServe runtimes, a production-stack Helm chart and integration in distributed frameworks such as Dynamo and llm-d. Pick it when you want the safest general choice. The open-source track's vLLM lesson covers its flags and metrics in depth.

vLLM on port 8000
bash
export MODEL_ID=YOUR_ORG/YOUR_MODEL
uv pip install vllm
vllm serve "$MODEL_ID" --port 8000 --max-model-len 8192

SGLang

SGLang is a high-performance engine whose signature is RadixAttention: the KV cache is kept in a radix tree keyed by token prefixes, so any request that shares a prefix with earlier work, a multi-turn conversation, an agent re-sending its scratchpad, few-shot templates, reuses it automatically. It is strong for agentic and multi-turn workloads, structured generation, and large mixture-of-experts models, and it is widely used for reinforcement-learning rollouts.

SGLang (its docs use port 30000)
bash
uv pip install --prerelease=allow sglang
sglang serve "$MODEL_ID" --host 0.0.0.0 --port 30000

TensorRT-LLM

TensorRT-LLM is NVIDIA's engine, with kernels tuned for each NVIDIA GPU generation, early support for new hardware features and low-precision formats, and in-flight batching. Older versions required compiling a per-model engine ahead of time; current releases default to a PyTorch-based backend that loads checkpoints directly. trtllm-serve gives an OpenAI-compatible server, trtllm-bench benchmarks, and it also runs behind Triton or Dynamo. Pick it when you are all-in on NVIDIA and want the most from the newest GPUs.

TensorRT-LLM with two-way tensor parallelism
bash
trtllm-serve "$MODEL_ID" --tp_size 2 --host 0.0.0.0 --port 8000
Note

For laptops and CPUs, llama.cpp and tools built on it run quantized GGUF models without a datacenter GPU. They are excellent for development and edge devices, not a substitute for a batching datacenter engine under heavy concurrent load.

Choosing, and the same client for all

If you needStart with
Broadest model and hardware support, biggest ecosystemvLLM
Heavy prefix reuse: multi-turn chat, agents, structured generationSGLang
Maximum performance on the newest NVIDIA GPUs and formatsTensorRT-LLM
Laptop, CPU or edge devicellama.cpp-based runtimes

Treat the table as a starting hypothesis. Performance rankings change with every release, model and GPU. Run your real prompt and output length distribution through each candidate at your target latency, compare goodput per GPU, and weigh operational factors: container images, metrics, upgrade cadence, model support for what you will serve next year.

One client, any engine: only the base URL changes
python
from openai import OpenAI

for base in ["http://localhost:8000/v1", "http://localhost:30000/v1"]:
    client = OpenAI(base_url=base, api_key="not-needed-locally")
    reply = client.chat.completions.create(
        model=client.models.list().data[0].id,
        messages=[{"role": "user", "content": "One sentence on KV caches."}],
        max_tokens=60,
    )
    print(base, reply.choices[0].message.content)
Hands-on practice

Two engines, one workload

  1. On a single-GPU machine, start vLLM on port 8000 with a small open model and --max-model-len 8192.
  2. Run the client above against it and confirm it answers.
  3. Stop vLLM, start SGLang on port 30000 with the same model, and rerun the client unchanged except for the URL.
  4. Write a 20-turn conversation script that re-sends the growing history each turn and record TTFT per turn on each engine.
  5. Note memory used after startup on each engine with nvidia-smi and the KV-cache capacity each logs.
  6. Write down which engine you would pick for this workload and why, citing your numbers.
Cheat sheet

Serving engines: vLLM, SGLang, TensorRT-LLM — at a glance

Main things to focus on

  • All three engines share continuous batching, paged cache, prefix caching, chunked prefill and quantization.
  • All speak the OpenAI API: switching engines means changing a URL.
  • vLLM: broadest support and ecosystem. SGLang: RadixAttention prefix reuse. TensorRT-LLM: NVIDIA-tuned.
  • Decide with your own workload, at your latency target, per GPU.
  • llama.cpp for laptops and edge, not for high-concurrency serving.
  • Weigh operations (images, metrics, upgrades), not just speed.

Launch

vllm serve MODEL --port 8000vLLM OpenAI-compatible server
sglang serve MODEL --port 30000SGLang server
trtllm-serve MODEL --tp_size 2 --port 8000TensorRT-LLM server
--tensor-parallel-size / --tp_sizeSplit across GPUs (vLLM / TRT-LLM)

OpenAI-compatible endpoints

GET /v1/modelsWhat the server serves
POST /v1/chat/completionsChat
POST /v1/completionsRaw text completion
OpenAI(base_url=URL)Point any OpenAI client at the engine

Shared features

Continuous batchingBatch re-formed every step
Prefix caching / RadixAttentionReuse cache for shared prefixes
Chunked prefillMix long prefills with decode
Speculative decodingDraft tokens cheaply, verify in one pass
Multi-LoRAMany fine-tunes on one base model

Benchmark tools

vllm bench servevLLM's serving benchmark
trtllm-benchTensorRT-LLM benchmark
aiperfEngine-neutral benchmark against any OpenAI-compatible URL

Common pitfalls

  • Choosing an engine from a vendor chart measured on someone else's workload.
  • Comparing engines at different max context lengths or precisions.
  • Ignoring operational fit: images, metrics, upgrade cadence, model support.
  • Using a laptop runtime for a high-concurrency production endpoint.
  • Tying application code to one engine's non-standard API.
Quiz

Check your understanding

5 questions · 4 to pass · answers are explained as you go. Your best score is saved on this device only.

Progress and quiz scores are saved in this browser only. Back up or restore on the hub.

Was this lesson useful? Tell me what to improve →