FDEInterviews logo
NeurIPS 2024

SGLang: Efficient Execution of Structured Language Model Programs

A serving system for programs that call a model many times: it caches KV across requests in a radix tree, decodes fixed JSON text in one step, and ran agent workloads up to 6.4× faster.

Breakdown by FDE Interviews · 11 min read · Updated

About the original · 2023
AUTHORS
Zheng, Yin, Xie, Sun, Huang, Yu, Cao, Kozyrakis, Stoica, Gonzalez, Barrett, Sheng
FROM
Stanford University, UC Berkeley and others
PUBLISHED
Dec 2023 · NeurIPS 2024
CITED BY
1,656Semantic Scholar · Oct 2026
IN THIS BREAKDOWN · 10 SECTIONS

You can read 24% of this breakdown.

01

The paper in one minute

Many real applications do not send one prompt and get one answer. An agent loops through reasoning and tool calls, each time resending a growing history. A few-shot classifier sends the same examples with every question. Self-consistency samples many answers to one prompt. A tree-of-thought program branches. A JSON extractor forces the model to produce a fixed structure with a few variable fields. The authors call these "language model programs", and they share two properties: many calls with overlapping prompts, and structured outputs.

SGLang, from a Stanford and Berkeley team (Lianmin Zheng, Ying Sheng, Ion Stoica, Joseph Gonzalez and others), is a serving runtime plus a small Python-embedded language for writing such programs. Its main runtime idea is RadixAttention. Instead of throwing away a request's KV cache when it finishes, SGLang keeps it in a radix tree keyed by token sequences. A new request walks the tree, reuses the longest cached prefix, and computes only the rest. When memory fills, the least recently used leaves are evicted. A cache-aware scheduler orders waiting requests to maximize prefix sharing, which avoids thrashing the cache.

The second idea is a compressed finite-state machine for constrained decoding. When output must match a regular expression, such as a JSON schema, standard engines mask disallowed tokens and decode one token at a time, even through stretches where only one continuation is possible, like {"summary": ". SGLang merges those forced stretches so they are processed in a single forward pass. For API-only models, an "API speculative execution" trick lets one call generate past a stop condition to satisfy a later call too, saving input-token costs.

Across agents, tree-of-thought, few-shot benchmarks, JSON decoding, retrieval pipelines and multi-turn chat, SGLang reached up to 6.4× the throughput of vLLM, Guidance and LMQL. On Chatbot Arena, where it served open models for a month, 74.1% of Vicuna-33B's prompt tokens came from cache, cutting time to first token by 1.7×.

THE IDEA, VISUALLY
REUSE KV CACHE ACROSS REQUESTS, AUTOMATICALLY system prompt chat A, turn 1 chat B, turn 1 5 few-shot examples chat A, turn 2 Q1 Q2 Q3 evicted (LRU) Each edge is a token sequence. A new request walks the tree, reuses the longest cached prefix and computes only the rest. Scheduling the queue longest-shared-prefix-first keeps hot branches in memory (depth-first order is optimal offline). Tree upkeep: 0.2 s of a 74.3 s run with no reuse at all (<0.3%) IN PRODUCTION AND IN BENCHMARKS share of prompt tokens served from cache, Chatbot Arena, one month Vicuna-33B 74.1% LLaVA-Next-34B 52.4% first-token latency for Vicuna-33B: 1.7× lower up to 6.4× throughput vs vLLM, Guidance, LMQL 1.6× JSON decoding from a compressed FSM 96% of the optimal cache hit rate, on average ~3× lower input-token cost with API speculation
RadixAttention treats the KV cache as a shared, evictable cache keyed by token prefixes. On a live chatbot service, most prompt tokens came from cache.
Research notes: the problem, approach and results
6.4×

higher throughput than existing systems on LM-program workloads

74.1%

of prompt tokens served from cache for Vicuna-33B over a month on Chatbot Arena

<0.3%

overhead of maintaining the radix tree on a workload with no reuse at all

THE PROBLEM

Agents, few-shot prompts, multi-turn chat and JSON extraction call a model many times with overlapping prompts and fixed output formats, but serving engines discarded KV caches after each request and decoded one token at a time even for fixed text.

THE IDEA

Keep finished requests' KV caches in a radix tree for automatic prefix reuse with LRU eviction and cache-aware scheduling; compress constrained-decoding state machines so fixed spans decode in one step; add a small Python language for these programs.

THE RESULT

Up to 6.4× throughput over vLLM, Guidance and LMQL on agent, reasoning, few-shot, JSON, RAG and chat workloads; 74.1% of Vicuna-33B prompt tokens served from cache on Chatbot Arena, with 1.7× lower first-token latency.

02

Why it matters to an FDE

Enterprise deployments are mostly language model programs: agents, retrieval pipelines with fixed instructions, extraction to schemas, few-shot classifiers. This paper shows how much cost hides in recomputing the same prefixes, and how to stop paying it.

Prompt order decides cache reuse. Reuse only works on identical prefixes. Put stable content first, such as system instructions, tool definitions and few-shot examples, and variable content (the user's question, retrieved documents) after. A timestamp or user name at the top of a prompt can silently defeat caching for everything below it.

Agents benefit most. Each agent step resends the full history; with prefix caching, each step pays only for the new tokens. That changes both latency and the GPU count needed for an agent workload.

Structured output can be faster, not just safer. Constrained decoding is often treated as a cost; with compressed state machines, the fixed parts of a schema are almost free. If a customer needs JSON, use the engine's structured-output support rather than parsing free text and retrying.

Hit rate is a metric to track. In production, cache hit rate directly affects throughput and first-token latency. Monitor it, and investigate drops after prompt changes.

The ideas are now everywhere. Automatic prefix caching and fast constrained decoding exist in vLLM and other engines too; the paper's baseline was an older vLLM without them. Choose an engine by measuring on the customer's workload, not by this paper's numbers alone.

THE FULL BREAKDOWN

Understand the mechanism. Try it on a real problem.

Unlock this breakdown and 49 more papers.

Premium opens every paper in full, plus every answer, concept, course and guide on the site. One payment for six months of access.

6 months · One payment · No auto-renewal

Get Premium · ₹2,000 / $25
  • ◇1 architecture diagram
  • </>1 runnable code experiment
  • ▦2 results tables
  • ?5 interview questions with answers
  1. 03Key takeaways

    RadixAttention, cache-aware scheduling, compressed FSMs, the language, and the misreadings to avoid

    Free account
  2. 04How it works

    The radix tree, scheduling, constrained decoding compression and API speculation in detail

    Premium
  3. 05The numbers that matter

    Results across workloads, ablations, the production deployment, and the caveats

    Premium
  4. 06Try it yourself

    Build a radix-tree KV cache with LRU eviction and see how request order changes the hit rate

    Premium
  5. 07Where it breaks

    Where prefix caching and cache-aware scheduling stop helping, and what the authors left open

    Premium
  6. 08Using it on a deployment

    Getting prefix reuse and fast structured output in a customer's serving stack

    Premium
  7. 09Interview angles

    Five questions this paper prepares you for, with answer hints

    Premium
Back to the paper library →