TL;DR
Many LLM applications call the model several times inside one program, with branches, loops and fixed output formats. SGLang is a language embedded in Python for writing such programs, and a runtime that runs them faster. Its runtime reuses the attention cache that calls share, and decodes output that must follow a format in bigger steps. On the workloads the paper tests it reaches up to 6.4 times the throughput of earlier systems.
Builds on: KV Cache, Structured Output, State Machine, Chain-of-Thought Prompting · Leads to: prefix-aware serving of multi-call programs; compare the memory-side design in Efficient Memory Management for LLM Serving with PagedAttention (vLLM).
Why it mattered
Most of what people build on top of language models is a program, not a single chat message. An agent loop calls the model, runs a tool, and calls again. A retrieval pipeline (Retrieval-Augmented Generation) asks the model to pick documents and then to answer from them. A reasoning method such as tree of thoughts makes many parallel attempts and then merges them. Each of these programs sends many prompts that share long common text: a system message, a set of few-shot examples, the same document, the same chat history.
Serving engines of the time treated each request as independent, so the attention cache for the shared text was computed again and thrown away. SGLang treats the whole program as the unit of work and keeps the shared cache between calls. The paper reports that its runtime served open-weight models on Chatbot Arena, where after one month the RadixAttention cache hit rate was 52.4 percent for LLaVA-Next-34B and 74.1 percent for Vicuna-33B.
The problem
Three things made these programs slow or clumsy.
First, writing them was tedious. Model output is not deterministic, so programmers do string building, prompt tuning, parsing of whatever text came back, and manual handling of parallel calls. The paper's essay-judging example would take about 2.1 times as many lines in an ordinary chat-completion style API.
Second, running them wasted work. An inference engine such as vLLM is general: it does not know that ten prompts share a 500-token prefix. The cache for that prefix (see KV Cache) is freed when a request ends, so the next request recomputes it. The paper's recurring test is a 5-shot MMLU benchmark, where every question carries the same five examples.
Third, forcing output to match a format was slow. A common method compiles the format into a finite automaton (see State Machine) and, at each step, masks out every token that would break the format. This decides one token per forward pass, even when the format says the next several characters are fixed.
How it works
Here is the runtime path for one small program, traced step by step. The numbers are invented and small on purpose. To keep the arithmetic readable, pretend each word is one token.
flowchart LR P["Python program with SGLang primitives"] --> I["Interpreter: one stream per prompt"] I --> R["Runtime: radix tree of cached tokens"] R --> S["Cache-aware scheduler"] S --> G["Model forward passes"] G --> R
-
The program builds a prompt. Suppose the program first appends a shared five-word instruction and then forks into two branches. Branch A appends "cats are" and branch B appends "cats eat". The interpreter does not wait for either branch. It sends each prompt to the runtime as an asynchronous stream. Check: both branches start from the same five-word prefix.
-
The runtime looks for the longest cached prefix. The runtime keeps a radix tree whose paths are token sequences and whose nodes hold the KV cache for those tokens. At first the tree is empty, so branch A's seven tokens are all computed and stored as one edge. Check: seven tokens computed, seven tokens stored.
-
Branch B reuses what A stored. Branch B's prompt is the same five-token prefix plus two new tokens. The tree matches the first five tokens, so only the two new tokens need a forward pass. The tree splits its edge after the shared prefix, so the shared part is one node with two children. Check: 5 of 7 tokens are cached, so the cache hit rate is 5/7, about 71 percent.
-
A request that matches less gets a lower rate. Suppose a third request starts with the same first word and then differs, so it matches one token of two. Its hit rate is 1/2. Check: the hit rate is always cached tokens divided by all prompt tokens, so it can be low even when some sharing exists.
-
The scheduler orders waiting requests by shared prefix. When many requests wait, the runtime runs first those with the longest matched prefix. That keeps related requests together so their cached prefix is still in memory when they run.
-
Memory fills up and the cache evicts. When GPU memory is short, the runtime evicts the least recently used leaf first, so a common ancestor survives until its children are gone. A node that a running request still uses is never evicted, because each node keeps a count of running users. Check: a node with a count above zero is not evictable.
-
Constrained output is decoded in bigger steps. Suppose the format requires the next characters to be exactly the text
"name":and the tokenizer would split that into three tokens. A token-by-token constraint engine takes three forward passes. SGLang's compressed finite state machine sees that each of those automaton states allows only one next token, and merges them into one edge. The three tokens then go through one forward pass. Check: 3 passes became 1 for this fixed stretch. -
Calls to an API-only model are merged speculatively. The model cannot be patched, so SGLang works at the program level. Suppose the program asks for a name and then a job, each stopping at a newline. The first call can keep generating a few tokens past its stop point. If the continuation already contains the text "job:" that the program needs next, the interpreter reuses it and skips one API call. Check: one call's input tokens are paid once instead of twice.
The math
For one request with p prompt tokens, of which c are found already cached in the radix tree as a prefix, the hit rate is h = c / p. Only the p minus c uncached tokens need a forward pass during prefill. A batch's hit rate is the same ratio taken over the total of all its prompts.
Step 3 in symbols: the shared prefix
Let the tokens of a request be t_1 through t_p. The tree lookup finds the largest integer k such that tokens t_1 through t_k match a path from the root. Then c = k. In step 3 above, p = 7 and k = 5, so h = 5/7.
Step 5 in symbols: the scheduling theorem
The paper states this result for an offline batch, with cache size at least the length of the longest request. Visiting the radix tree in depth-first order (go down one branch fully before the next) achieves the optimal cache hit rate. The paper's longest-shared-prefix-first ordering is the same as depth-first order. The result is stated and proved in the paper's appendix. The online case, where requests keep arriving, is only approximated.
Step 6 in symbols: eviction
Each node n keeps a counter r_n of running requests that use it. A node is evictable only when r_n = 0 and it is a leaf. Among evictable leaves, the runtime removes the one with the oldest last-use time. Because leaves go first, an ancestor shared by many requests is removed only after all of its descendants are gone.
Step 7 in symbols: forward passes
If a constrained stretch has m tokens and each of its automaton states allows exactly one next token, a token-by-token decoder needs m forward passes for that stretch. Compressing the stretch into one edge needs one. The saving is m minus 1 passes.
Results
- End to end. Against Guidance, vLLM and LMQL on Llama-2 7B and 70B and Mixtral-8x7B models, the paper reports up to 6.4 times higher throughput and up to 3.7 times lower latency. Tests ran on NVIDIA A10G and A100 GPUs. Different workloads gain different amounts: few-shot MMLU gains from reusing the example block, JSON decoding gains from the compressed automaton.
- Multimodal. On LLaVA image and video models, the paper's Table 2 reports 1.15 against 0.18 images per second for LLaVA-v1.5-7B, about 6.4 times the throughput of the author's original implementation.
- Ablations. A higher cache hit rate leads to larger batches, higher throughput and lower latency. Turning off the cache, the tree structure, the cache-aware scheduler or the frontend hints each lowers performance.
- Cost of the cache. On a benchmark with no reuse, managing the tree took 0.2 seconds out of 74.3 seconds, under 0.3 percent.
- Constrained decoding. The compressed automaton raises JSON decoding throughput by 1.6 times. Recomputing its preprocessing for every request would make throughput 2.4 times lower, so the paper preprocesses it once per format.
- Limits. Greedy cache-aware scheduling can starve requests whose prefixes are rarely shared, which the paper leaves as future work. Extending the cache across DRAM and disk is listed as future work. Merging forced tokens can distort the probabilities of the choices within the merged span, a point the paper raises as a future direction. Gains depend on sharing: a single request with no shared text gains nothing from the radix tree.
Reading guide
The paper has a short introduction, a programming model, three runtime sections, an evaluation, and appendices. Read it in this order.
- Section 1, Introduction. Needs LLM Agent for the idea of multi-call programs, and Language Model for the basic model.
- Section 2, Programming Model. Needs Structured Output for the regex argument, and Chain-of-Thought Prompting for the branch-solve-merge example. Table 1 is a short comparison of three systems.
- Section 3, RadixAttention. Needs KV Cache, Radix Tree and Cache Eviction Policy. Read the theorem statement and skip its proof on a first pass.
- Section 4, Compressed finite state machine. Needs State Machine and Text Tokenization. The key figure is Figure 4.
- Section 5, API speculative execution. Needs nothing new beyond the program model. Compare it with Speculative Decoding, which is a different technique.
- Section 6, Evaluation. Needs Measuring Massive Multitask Language Understanding, Tree of Thoughts: Deliberate Problem Solving with Large Language Models, ReAct Prompting, Self-Consistency Improves Chain of Thought Reasoning in Language Models, Mixtral of Experts and Visual Instruction Tuning (LLaVA) for the benchmarks, and Vision-Language Model for the multimodal results.
- Appendix A. Expands on the KV cache. It needs KV Cache and Model Parallelism.
Check yourself
QuestionIn the walkthrough, branch B has 7 prompt tokens and 5 match a cached prefix. What is its hit rate, and how many tokens need a forward pass?Show answer
The hit rate is 5/7, about 71 percent. Two tokens need a forward pass, the two that do not match.
QuestionA batch of 10 prompts each has 200 tokens, and all share the same first 150 tokens. The first prompt computes everything and the others reuse the shared part. How many tokens are computed in total?Show answer
The first prompt computes 200 tokens. Each of the other 9 computes 200 minus 150, which is 50. Total: 200 + 9 times 50 = 650 tokens.
QuestionWhy does the radix tree evict leaves before their ancestors, instead of evicting whichever node is least recently used?Show answer
A leaf holds the end of one sequence, while an ancestor holds text that many sequences may still share. Evicting an ancestor first would destroy cache that several branches could reuse. Removing leaves first lets shared ancestors survive until their descendants are gone.
QuestionAn output format fixes 8 tokens in a row, followed by 12 free tokens. How many forward passes does the compressed finite state machine need, compared with token-by-token masking?Show answer
Compressed: 1 pass for the fixed run plus 12 for the free tokens, so 13. Token-by-token: 8 plus 12, so 20. The compressed method saves 7 passes.
QuestionWhy can the speculative method help with an API-only model, when it cannot change how the model decodes?Show answer
Because the program often knows what text comes next. If the first call keeps generating past its stop point and the continuation already contains the text the next step needs, the interpreter reuses it and does not pay for a second call's input tokens. The model is unchanged; only the calls the program makes are merged.