Skip to content

๐Ÿš€ How LLM Serving Works: vLLM & SGLang โ€‹

Notes from the lecture How LLM Serving Works: An Introduction to vLLM and SGLang by Dr. Sreedath Panat (Vizuara).

๐Ÿงญ The whole lecture in one line

A GPU is very fast at maths but slow at moving data. vLLM and SGLang are clever "managers" that let one GPU serve many users at once without wasting memory. Result: the same 1 million users cost about 6ร— less.

We follow one example the whole way, so the numbers stay simple:

ThingValue
๐Ÿค– ModelLlama 3.1 8B (8 billion numbers called weights)
๐Ÿ–ฅ๏ธ GPUOne NVIDIA H100 (80 GB memory)
๐Ÿ’ต GPU rentabout $2.5 per hour

1. ๐Ÿ’ธ Why serving matters more than training โ€‹

Think of a restaurant ๐Ÿฝ๏ธ:

  • Training = building the kitchen. Very expensive, but you pay once.
  • Serving (inference) = cooking every meal for every customer. Cheap per meal, but you pay every day, forever.

Flat line = training (one-time). Rising line = serving (adds up every month). Sooner or later, serving costs more.

That's why so many companies work on making inference cheaper and faster.

๐Ÿ“Š How much work is "1 million users"? โ€‹

And this is a light user. People using coding agents easily burn 200,000 tokens a day.


2. ๐Ÿงฐ What are vLLM and SGLang? โ€‹

A trained model is just a big file of numbers. Someone has to actually run it: multiply the numbers with your input, layer by layer, to get the next word. That "someone" is an inference engine.

vLLMSGLang
Full nameNo official one (people say "virtual")Started as Structured Generation Language
Came fromUC BerkeleyAlso has a UC Berkeley team
Famous ideaPagedAttentionRadixAttention
Used forServing open models (Llama, Mistral, DeepSeekโ€ฆ) to many usersSame

In one sentence: vLLM and SGLang are engines that run an LLM for thousands of users at the same time, efficiently.

๐Ÿ†š What about llama.cpp? That's for one person on one device (your laptop or phone). vLLM/SGLang are for many people on a server.


3. ๐Ÿ–ฅ๏ธ Inside a GPU: the warehouse and the workbench โ€‹

A GPU has two kinds of memory. The easiest way to picture it:

  • The warehouse (VRAM) is big, so the whole model lives there.
  • The workbench (SRAM) is where the maths happens, but it is tiny: 30 MB. Even one layer of the model (~500 MB) doesn't fit!
  • So the GPU keeps carrying small pieces of the model from the warehouse to the workbench, doing the maths, and sending the result back.

Why does Llama 8B take 16 GB? โ€‹

Big frontier models may have trillions of weights, so they need thousands of GB. That's why we use a small model in this example.

โšก The big surprise: maths is fast, carrying is slow โ€‹

To make one new token, the GPU must carry all 16 GB of weights to the workbench once.

StepCalculationTime
๐Ÿšš Carry weights16 GB รท 3.35 TB/sโ‰ˆ 4.8 ms
โš™๏ธ Do the maths16 billion operations รท 989 trillion per secondโ‰ˆ 0.016 ms

๐Ÿข Carrying takes about 300ร— longer than the maths. We say serving is memory-bound: the road is the bottleneck, not the workers.

So one user alone gets about 1 รท 4.8 ms โ‰ˆ 200 tokens per second, and the cores sit mostly idle. Keep this in mind; it's the key to everything that follows.


4. ๐Ÿ“–โœ๏ธ Prefill vs decode: reading vs writing โ€‹

An LLM writes one token at a time, then adds it to the input and goes again:

There are two phases, and they behave very differently:

๐Ÿ“– Prefillโœ๏ธ Decode
WhatReading your questionWriting the answer
HowAll tokens at onceOne token at a time
WhyYou already gave all the wordsCan't write word 5 before word 4 exists
Feels likeVery fastThe "typing" you watch

๐Ÿ’ก Try it: paste a huge document into ChatGPT and ask a question. The answer starts almost instantly, because the whole document is "read" in parallel during prefill.

โฑ๏ธ What the user actually feels โ€‹

That's why apps stream words as they come: you only "feel" the wait until the first token (TTFT), not the whole answer.

Typical speeds: hundreds of tokens/s on a server GPU is good. A small model on a phone gets about 18โ€“30 tokens/s.


5. ๐Ÿ—‚๏ธ The KV cache: the model's notebook โ€‹

A tiny bit of attention (no scary maths) โ€‹

Each word is turned into three vectors (lists of numbers):

VectorSimple meaning
Q (query)"What am I looking for?"
K (key)"What do I contain?" (a label)
V (value)"Here is my actual information"

To make the next word, the newest word's Q is compared with the K of every earlier word. Those scores decide how much of each earlier word's V to mix in.

โ“ Why save K and V, but not Q? โ€‹

  • Q is only needed for the newest word. Once used, throw it away. ๐Ÿ—‘๏ธ
  • K and V of every old word are needed again every single round.

So we save K and V in a notebook = the KV cache. Without it, the model would re-read the whole conversation for every new word.

๐Ÿงฎ How big is the notebook? โ€‹

For Llama 3.1 8B, per token:

Why only 8 KV heads? Llama uses grouped-query attention: 4 query heads share one set of keys and values, which shrinks the cache 4ร—.

Context sizeKV cache for one user
1 token128 KB
2,500 tokens (~4 pages)โ‰ˆ 320 MB

๐Ÿง  How many users fit on one H100? โ€‹

54 GB รท 0.32 GB per user โ‰ˆ 160 users (at 2,500 tokens each).

โš ๏ธ If users want longer chats (more pages), each user's notebook grows and fewer users fit. This is the core problem vLLM and SGLang attack.


6. ๐Ÿš Batching: fill the van โ€‹

Remember: carrying weights is the slow part. So once the weights are on the workbench, use them for everyone!

It's like a delivery van ๐Ÿš: the drive costs the same whether it carries 1 parcel or 100. So fill it up.

Users per GPUTokens per second (whole GPU)
One user only1~200
Batched64โ€“128~8,000

Each user waits only a little longer, but the GPU does 40ร— more work. Each user also gets their own next token, even if one is on word 2 and another on word 2,342.


7. ๐Ÿ—๏ธ What's inside one inference engine โ€‹

A company has many GPUs. Each one runs a copy (replica) of the model with vLLM or SGLang.

๐ŸŽฏ Cache-aware routing โ€‹

You asked a question and Replica 1 answered. Now you ask a follow-up. Where should it go?

A smart router remembers where your notebook lives and sends you back there. This is cache-aware routing.

๐Ÿงฑ Who does what? โ€‹

  • Inference engineer: works on the vLLM/SGLang layer (scheduling, memory).
  • Kernel engineer: works one layer below (making the maths on the cores fast).

8. ๐Ÿ“„ Trick #1: PagedAttention (vLLM) โ€‹

The problem: booking seats you never use โ€‹

Old servers reserved a big, continuous chunk of memory per user, sized for the longest possible answer. But nobody knows how long an answer will be.

  • "What's the capital of France? One word." โ†’ needs a tiny notebook.
  • "Explain this 5-page document." โ†’ needs a huge one.

The fix: small pages, placed anywhere โ€‹

Like a computer's operating system (virtual memory), split memory into small pages. In this example a page holds 16 tokens โ‰ˆ 2 MB.

  • Get a new page only when the last one is full.
  • So each user has at most one half-empty page. Almost no waste!
  • When a user leaves, their pages are freed and given to someone new. โ™ป๏ธ

Result in the lecture's example: about 164 chats fit on one H100.


9. ๐Ÿ”„ Trick #2: Continuous batching โ€‹

โŒ Static batching: wait for the slowest โ€‹

Like a bus ๐ŸšŒ that only leaves when every passenger has reached the last stop:

โœ… Continuous batching: fill a seat the moment it's free โ€‹

The engine checks after every token step: "Who finished? Who is waiting? Swap them in." No idle seats.


10. ๐ŸŒณ Trick #3: RadixAttention / prefix caching (SGLang) โ€‹

The idea: don't redo the same work โ€‹

A company chatbot gives every user the same long system prompt. Why compute its KV cache again for every user?

  • If users share the same beginning (prefix), compute its K and V once and reuse it.
  • SGLang stores these shared beginnings in a tree (a radix tree), so any matching start can be found quickly.
  • Huge win for coding agents: the giant project prompt stays the same while you only ask "make this button blue".

๐Ÿค” "But LLMs are stateless!"

True: the model still uses all the old keys and values for every new token. RadixAttention just means they're reused, not recomputed.


11. ๐Ÿ’ฐ The full cost math: naive vs vLLM/SGLang โ€‹

Same goal: 10 billion tokens per day on H100s at $2.5/hour.

Notice: each step is slower (16 ms vs 6.2 ms), but it makes 8ร— more tokens per step. That's batching paying off.

๐Ÿข Simple server๐Ÿš€ vLLM / SGLang
Users at once16128
Tokens per second per GPU~1,300~8,000
Tokens per hour~4.7 M~29 M
Cost per 1 M tokens~$0.54~$0.09
Tokens per GPU per day~112 M~690 M
GPUs for 10 B tokens/day~90~15
Yearly GPU bill~$2.0 M~$0.33 M

๐ŸŽ‰ About 6ร— cheaper, same model, same GPUs, same answers. Only the software changed.

๐Ÿงฎ Show the full arithmetic

Simple server

  • 1,300 tok/s ร— 3,600 s = 4.68 M tokens/hour โ†’ $2.5 รท 4.68 = $0.53 per M tokens
  • 1,300 ร— 86,400 s = 112 M tokens/day โ†’ 10 B รท 112 M โ‰ˆ 89 โ†’ 90 GPUs
  • 90 ร— $2.5 ร— 24 ร— 365 โ‰ˆ $1.97 M/year

vLLM / SGLang

  • 8,000 tok/s ร— 3,600 = 28.8 M tokens/hour โ†’ $2.5 รท 28.8 = $0.087 per M tokens
  • 8,000 ร— 86,400 = 691 M tokens/day โ†’ 10 B รท 691 M โ‰ˆ 14.5 โ†’ 15 GPUs
  • 15 ร— $2.5 ร— 24 ร— 365 โ‰ˆ $0.33 M/year

The lecturer noted these are rough, back-of-the-envelope numbers. A simple server could squeeze in a few more users, and vLLM could fit ~256. Both sides would shift, but the gap stays about the same.


12. ๐Ÿ”ญ Beyond this lecture โ€‹

vLLM and SGLang do much more than these three tricks, for example:

  • Quantization: store weights in fewer bits, so there's less to carry. ๐Ÿšš
  • Speculative decoding: guess several tokens cheaply, then check them in one go.

๐Ÿ‘‰ For a deeper dive into these and more, see my Inference Optimization notes.


โœ… Cheat sheet (remember these) โ€‹

๐Ÿ“Œ 10 things to remember

  1. Serving costs more than training over a model's life.
  2. Llama 8B = 8 B weights ร— 2 bytes = 16 GB.
  3. GPU = big slow VRAM (80 GB) + tiny fast SRAM (~30 MB).
  4. Carrying data is the bottleneck, not maths (4.8 ms vs 0.016 ms).
  5. Prefill reads the question in parallel; decode writes one token at a time.
  6. KV cache saves K and V (not Q) so old tokens aren't recomputed. ~128 KB/token for Llama 8B.
  7. Batching = load weights once, serve many users.
  8. PagedAttention = small pages placed anywhere โ†’ ~96% memory used.
  9. Continuous batching = a finished user's seat is refilled right away.
  10. RadixAttention = shared prompt beginnings computed once and reused.

<<>> with โ™ฅ๏ธ by S@Nchit