Serving Engines

SGLang

SGLang is a structured generation language and runtime that achieves state-of-the-art LLM serving throughput. RadixAttention for KV-cache reuse, constrained decoding, and a Python DSL for complex multi-call programs.

RadixAttention
KV reuse
3–5×
vs vLLM on prefix
JSON/Regex
Constrained gen

Table of Contents

SECTION 01

What makes SGLang different

SGLang (Structured Generation Language) addresses two bottlenecks that standard LLM serving leaves on the table: redundant KV-cache computation and inefficient structured output generation.

RadixAttention: SGLang stores the KV-cache in a radix tree indexed by token sequence. When multiple requests share a common prefix (e.g. the same system prompt), the prefix's KV values are computed once and reused across all requests. This gives 3–5× throughput improvement on workloads with shared prefixes — common in agentic pipelines and batch classification tasks.

Compressed finite-state machine: For structured outputs (JSON, regex), SGLang pre-computes which tokens are valid at each generation step and masks the logits. This is faster than regex matching post-hoc and guarantees structural validity.

SECTION 02

Deploying the SGLang server

pip install "sglang[all]"

# Start server with Llama 3 8B
python -m sglang.launch_server   --model-path meta-llama/Meta-Llama-3-8B-Instruct   --port 30000   --mem-fraction-static 0.85

# Multi-GPU with tensor parallelism
python -m sglang.launch_server   --model-path meta-llama/Meta-Llama-3-70B-Instruct   --tp 4   --port 30000

The server exposes an OpenAI-compatible API at http://localhost:30000/v1, so existing clients work unchanged.

import openai
client = openai.OpenAI(base_url="http://localhost:30000/v1", api_key="EMPTY")
resp = client.chat.completions.create(
    model="default",
    messages=[{"role": "user", "content": "What is 2+2?"}],
)
print(resp.choices[0].message.content)
SECTION 03

RadixAttention and prefix caching

Every request's token sequence is stored as a path in a radix tree. When a new request arrives, SGLang finds the longest common prefix with cached sequences and reuses those KV values. Only the novel suffix needs computation.

# Example: all these requests share a 500-token system prompt
# Without prefix caching: each pays ~500 tokens of prefill
# With RadixAttention: first request pays 500 tokens, rest pay ~0 for prefix

requests = [
    [system_prompt, "Classify this email as spam/not-spam: " + email_1],
    [system_prompt, "Classify this email as spam/not-spam: " + email_2],
    [system_prompt, "Classify this email as spam/not-spam: " + email_3],
]
# SGLang detects the shared prefix and caches it after the first request

RadixAttention is most effective for: batch inference with shared prompts, multi-turn conversations (prefix grows with history), RAG pipelines where the retrieved context is reused, and agentic loops with fixed tool descriptions.

SECTION 04

Structured generation

SGLang guarantees structurally valid outputs via constrained decoding. At each generation step, the server computes the set of valid next tokens given the current output and the constraint, then masks invalid tokens to zero probability before sampling.

import sglang as sgl

@sgl.function
def extract_entity(s, text):
    s += sgl.system("You are an entity extractor. Return JSON only.")
    s += sgl.user(f"Extract person, org, location from: {text}")
    # Constrain output to valid JSON matching this schema
    s += sgl.assistant(
        sgl.gen("result", max_tokens=200,
                json_schema={
                    "type": "object",
                    "properties": {
                        "person": {"type": "array", "items": {"type": "string"}},
                        "org":    {"type": "array", "items": {"type": "string"}},
                        "location": {"type": "array", "items": {"type": "string"}},
                    }
                })
    )
    return s["result"]

result = extract_entity.run(text="Tim Cook announced Apple's new product in Cupertino.")
print(result)  # Always valid JSON
SECTION 05

The SGLang Python DSL

SGLang includes a Python DSL for writing multi-call LLM programs. Programs are compiled into optimised execution graphs that automatically parallelise independent calls and share KV-cache across dependent calls.

import sglang as sgl

@sgl.function
def multi_turn_qa(s, question):
    s += sgl.system("You are a helpful assistant.")
    s += sgl.user(question)
    s += sgl.assistant(sgl.gen("answer1", max_tokens=100))
    s += sgl.user("Now give a shorter version.")
    s += sgl.assistant(sgl.gen("answer2", max_tokens=50))

# Batch execution — runs requests concurrently, sharing prefix KV-cache
questions = ["Explain transformers", "What is RAG?", "What is LoRA?"]
states = sgl.function.run_batch(
    [{"question": q} for q in questions],
    progress_bar=True,
)
for q, s in zip(questions, states):
    print(f"Q: {q}")
    print(f"Full: {s['answer1']}")
    print(f"Short: {s['answer2']}")
SECTION 06

Benchmarks

SGLang typically outperforms vLLM and TGI on workloads with shared prefixes. On pure throughput benchmarks without shared prefixes, performance is roughly comparable to vLLM (within 10–20%).

Key scenarios where SGLang excels: batch document processing (all requests share a long instruction prefix), RAG (documents are reused across queries), multi-turn agents (conversation history prefix grows and is reused), and structured generation (JSON/regex constraints are applied efficiently).

SGLang is maintained by the LMSYS team (same group behind Chatbot Arena) and is production-used at scale. It also supports speculative decoding with an eagle-style draft model for additional speedups.

SECTION 07

Gotchas

Memory configuration: --mem-fraction-static controls what fraction of GPU memory is pre-allocated for KV-cache. If too high, the process OOMs loading the model. Default 0.9 is aggressive; start at 0.85.

Chunked prefill: For very long inputs, SGLang chunks prefill into smaller pieces to avoid GPU memory spikes. Enable with --chunked-prefill-size 4096.

Prefix caching requires deterministic tokenisation: If you format prompts differently between requests (extra spaces, different BOS handling), the cache won't hit. Use a consistent prompt template function.

DSL vs REST API: The Python DSL is powerful but adds a dependency on the SGLang Python client. For simple deployments, the OpenAI-compatible REST API is lower friction.

SGLang vs. Other Inference Frameworks

SGLang (Structured Generation Language) is a high-performance LLM inference framework that combines a Python DSL for defining structured generation programs with a runtime that efficiently executes them through KV cache reuse and speculative execution. It is designed for workloads that generate structured outputs and benefit from prompt prefix sharing across requests.

FrameworkKV Cache ReuseStructured OutputThroughput FocusBest For
SGLangRadixAttentionNative (constrained decoding)Very highStructured gen, shared prefixes
vLLMPagedAttentionVia outlinesHighGeneral serving
TGIFlash AttentionGuided decodingHighHF model serving
Ollamallama.cppJSON modeMediumLocal deployment

SGLang's RadixAttention enables automatic KV cache reuse across requests that share prompt prefixes. When multiple requests begin with the same system prompt or few-shot examples, SGLang computes the KV cache for the shared prefix once and reuses it across all concurrent requests, dramatically reducing both compute cost and latency. This is particularly beneficial for applications like RAG pipelines, where the system prompt and retrieved documents are constant across many user queries differing only in the final question.

Constrained decoding in SGLang enforces structural constraints on the generated output at the token level, guaranteeing valid JSON, valid regular expression matches, or valid format adherence without post-hoc parsing and retry logic. The constraint is applied at each decoding step by masking tokens that would violate the constraint, ensuring every generated token advances toward a valid output. This eliminates the significant latency and cost overhead of regenerating outputs that fail validation in unconstrained generation approaches.

SGLang's forking primitive enables parallel sampling within a single program execution — the program forks at a specified point, multiple continuations are generated in parallel sharing the prefix KV cache, and results are collected when all forks complete. This is more efficient than making multiple separate API calls with the same prompt because the shared prefix computation happens only once. Applications like best-of-N sampling, diversity sampling for creative tasks, and generating multiple candidate answers for self-consistency voting all benefit from forking's shared prefix efficiency.

SGLang's backend can be deployed as a REST API server compatible with the OpenAI API format, enabling drop-in replacement of OpenAI API calls in existing applications with locally hosted models. The server handles request batching, continuous batching of incoming requests, and speculative decoding optimizations transparently — applications only need to change the base URL configuration. This OpenAI API compatibility is standard across the major open-source inference backends (vLLM, SGLang, TGI) and has become the de facto standard for LLM service interfaces.

Production deployment of SGLang benefits from hardware-specific configuration tuning. The tensor parallelism degree should equal the number of GPUs on a single server (rather than across servers) to minimize communication overhead in the critical path. The chunked prefill feature processes long prompts in smaller chunks to prevent new requests from experiencing high queuing latency while a long prompt is being processed. These configuration choices significantly affect the latency distribution experienced by users in mixed-traffic production environments.

Benchmark comparison between SGLang and vLLM on identical hardware consistently shows SGLang's prefix caching advantage on workloads with high prompt sharing. For general serving workloads with diverse prompts and minimal shared prefixes, vLLM and SGLang perform comparably. The performance gap in favor of SGLang emerges on RAG workloads where the same retrieved documents appear in many requests, agent workloads with long shared system prompts, and multi-turn chat applications where conversation history grows progressively within the context window.