Phase 03/Anatomy of a serving system/vLLM
PHASE 03 · Anatomy of a serving system

vLLM

0%
8 h6 subtopics2 lessons · 1 required
In plain English

vLLM is the most widely used open-source program for running language models as a service. If the model is the chef, vLLM is the complete franchise kitchen design that thousands of companies have copied: it includes the order-taking counter (an OpenAI-style API), a clever pantry system that wastes almost no fridge space (PagedAttention), and a head waiter who keeps slotting new orders in while others are still cooking (continuous batching). In this topic you install it, run a small model on a free GPU, read the startup logs line by line, and learn which knobs change speed and memory.

Why it matters

vLLM is the reference point for the whole field; SGLang, TensorRT-LLM and TGI are all compared against it. Most inference engineering job postings mention it by name, and its source code (Phase 6) is the best place to see every optimization from Phases 3 to 5 implemented for real.

Hiring signal

Skills this topic builds, with the number of the 46 analysed job postings that ask for each. Companies asking: Anthropic, Anyscale, Baseten, Cerebras, Cohere, CoreWeave, Crusoe, Databricks, ElevenLabs, Fireworks, Hugging Face, Liquid AI, Modal, nvidia, OpenAI, Perplexity, Poolside, Thinking Machines, Together AI, xAI.See all skills →

Learning objectives

  • Start a vLLM OpenAI-compatible server for a small model and query it with the openai client.
  • Interpret vLLM startup logs including KV cache size, max concurrency and CUDA graph capture.
  • Tune gpu-memory-utilization, max-model-len, max-num-seqs and max-num-batched-tokens and predict their effect.
  • Use the offline LLM class for batch generation and explain how it differs from the server.
  • Read the Prometheus /metrics endpoint and name the five most useful metrics.

Key concepts

LLM class (offline)
Python API that runs a batch of prompts to completion in-process; no HTTP, used for evaluation and batch jobs.
AsyncLLMEngine / EngineCore
The engine loop that pulls scheduled requests and runs one step (prefill or decode) per iteration.
gpu-memory-utilization
Fraction of GPU memory vLLM may claim for weights plus KV cache; what is left after weights becomes KV blocks.
max-model-len
Maximum context length per sequence; caps how many KV blocks one request can ever need.
max-num-seqs
Maximum number of sequences in one decode batch; a direct cap on concurrency.
max-num-batched-tokens
Token budget per engine step; controls how much prefill work can be mixed into a step.
Chunked prefill
Splitting a long prompt's prefill across steps so running decodes are not stalled.
Prefix caching
Reusing KV blocks for prompts that share a prefix (system prompts, few-shot examples) via block hashes.

Subtopics

  1. 1. Installing and launching

    pip install vllm, vllm serve <model>, dtype and quantization flags, and what a healthy startup log looks like.

  2. 2. Reading the startup log

    Weight load time, available KV cache memory, number of GPU blocks, maximum concurrency estimate, CUDA graph capture.

  3. 3. Engine arguments that matter

    gpu-memory-utilization, max-model-len, max-num-seqs, max-num-batched-tokens, enable-prefix-caching, enable-chunked-prefill.

  4. 4. Offline versus online inference

    LLM.generate for batch jobs versus the OpenAI server for interactive traffic; SamplingParams in both.

  5. 5. Metrics and observability

    The /metrics Prometheus endpoint with running/waiting requests, KV cache usage, TTFT and per-token latency histograms.

  6. 6. V1 engine architecture

    The 2025 re-architecture with a separate EngineCore process, zero-overhead prefix caching and a simpler scheduler.

Resources

Hands-on lessons

L1

Serve Qwen2.5-0.5B with vLLM on a T4 and read the logs

Colab T4 · freenotebook90 minrequired

Goal · A running vLLM OpenAI-compatible server on Colab, queried with the openai client, with every startup log line explained in your own words.

  1. 1In a Colab T4 runtime run pip install vllm openai; check nvidia-smi shows about 15 GB free.
  2. 2Launch in the background with nohup vllm serve Qwen/Qwen2.5-0.5B-Instruct --dtype half --max-model-len 4096 --gpu-memory-utilization 0.85 --port 8000 and tail the log.
  3. 3Copy the log lines for weight loading time, KV cache memory, number of GPU blocks and maximum concurrency into a markdown cell and explain each.
  4. 4Verify the KV block count against your p3-llm-serving-architecture-l2 formula (block size 16 tokens).
  5. 5Query /v1/chat/completions with the openai client using stream=True and print tokens as they arrive.
  6. 6Restart with --max-model-len 1024 and then --gpu-memory-utilization 0.6; record how the block count and max concurrency change.
  7. 7Curl http://localhost:8000/metrics and list vllm:num_requests_running, vllm:gpu_cache_usage_perc and the TTFT histogram.
Deliverable
A notebook with the annotated startup log, three configurations in a table, and a saved /metrics dump.
Check
Your computed KV bytes per token matches the log-derived value within 5 percent; lowering gpu-memory-utilization lowers the block count proportionally.
Fallback
If no GPU is available, run vLLM's CPU backend in Docker (docs.vllm.ai CPU installation) or read the same log lines from a saved log in a vLLM GitHub issue and annotate those.
L2

Offline batch generation with the LLM class

Colab T4 · freenotebook45 minoptional

Goal · Measure how total time for 64 prompts changes with max_num_seqs in offline mode.

  1. 1from vllm import LLM, SamplingParams; load Qwen/Qwen2.5-0.5B-Instruct with max_model_len 2048.
  2. 2Generate 64 short prompts with max_tokens 128 and time llm.generate.
  3. 3Repeat with max_num_seqs set to 1, 8 and 64 (re-create the LLM each time).
  4. 4Compute output tokens per second for each setting and plot.
Deliverable
A plot of tokens per second versus max_num_seqs.
Check
Throughput at max_num_seqs 64 is at least 5x the throughput at 1.

Build your own

Build your own vLLM client benchmark + request tracer

PythonColab T4 · free~6 h

A CLI that launches a real vLLM server on a T4, scrapes its scheduler metrics at 100 ms and aligns them with per-request token timelines so you can see admission and preemption happen.

The real thing: docs.vllm.ai/en/latest/design/metrics.html ↗

How you know a stage passes

One CLI, trace.py, with subcommands launch, scrape, run, join, report. Each stage is verified two ways. On Colab, you run the subcommand against a live `vllm serve Qwen/Qwen2.5-0.5B-Instruct` and check the printed numbers. Offline, tests/test_trace.py runs on CPU against recorded CSV fixtures you commit after the first Colab run, so `pytest -q` passes on your Mac without a GPU.

Stages, in order. Each one leaves you with something that runs.0/6
  1. 01launch and parse the startup log
    trace.py launch starts vLLM with chosen engine args, waits for /health and parses the startup log into a JSON summary (model, dtype, max_model_len, num GPU blocks, max_num_seqs).
    test · The printed JSON has a num_gpu_blocks field whose value equals the number in the "GPU KV cache size" log line, and the command exits 0 within 5 minutes on a T4.
  2. 02a metrics scraper
    trace.py scrape polls /metrics every 100 ms and writes a CSV of timestamp, num_requests_running, num_requests_waiting, gpu_cache_usage_perc and num_preemptions_total.
    test · A 5 second idle scrape yields at least 45 rows with running and waiting equal to 0 on every row, and the CSV parses with pandas without NaNs.
  3. 03a per-request tracer
    trace.py run sends N streaming requests through the openai client and records send time, TTFT, every inter-token timestamp and finish time per request.
    test · For 10 requests the TTFT and ITL columns are populated for all rows, and TTFT plus the sum of ITLs is within 5 percent of the measured end-to-end time for every request.
  4. 04join requests with scheduler state
    trace.py join merges the request timeline with the scrape to label each request's admission iteration (the moment it moves from waiting to running).
    test · A burst of 32 requests against a server started with max-num-seqs 8 shows running peaking at exactly 8 and waiting peaking at 24, and every request's admission time is after its send time.
  5. 05catch preemptions in the act
    With gpu-memory-utilization set low and long outputs, the tracer detects increments of num_preemptions_total and flags the requests whose ITL spikes at that moment.
    test · num_preemptions_total ends above 0, and at least one request is flagged with an ITL gap greater than 5x its median ITL within 200 ms of a preemption counter increment.
  6. 06the report
    trace.py report produces a Gantt-style timeline PNG of requests with running and waiting overlaid, plus a markdown summary of TTFT and ITL percentiles and preemption count.
    test · Running report twice on the same CSVs produces byte-identical summary.md with p50 and p99 TTFT, and timeline.png exists and is larger than 20 KB.

No GPU? Record the CSVs once in a Colab session, commit them as fixtures, and develop stages 4 to 6 entirely on CPU against the fixtures.

Knowledge check

QYou raise --max-model-len from 4k to 32k and the server still starts, but a warning says max concurrency dropped. Why?

max-model-len does not change total KV memory, but vLLM reports concurrency as total blocks divided by blocks needed for one max-length sequence, so the estimate falls.

QWhat does --gpu-memory-utilization 0.9 actually reserve?

90 percent of total GPU memory for vLLM as a whole; weights are subtracted first and the remainder (minus activation workspace) is carved into KV blocks.

QWhen would you use the offline LLM class rather than vllm serve?

For batch jobs like evaluation or dataset labelling where all prompts are known up front and no streaming or HTTP is needed.

QWhat is the difference between max-num-seqs and max-num-batched-tokens?

max-num-seqs caps how many sequences are in a step; max-num-batched-tokens caps how many tokens (mostly prefill) are processed in that step.

Completion

  • All 6 subtopics marked done
  • All 1 required lessons done
  • All subtopics marked completed.
  • Lesson l1 notebook saved with the annotated log and three-configuration table.
  • Can explain each of the six key engine arguments to a colleague without notes.
Topic progress is derived; it reaches 100% when both checks above pass.Next recommended topic →