Ankit Patel
WritingPapersLabsAboutContact
Paper Reviews··3 min read·28 views

SGLang RadixAttention: Put the Prompt Timestamp Last

SGLang & RadixAttention · Zheng et al., NeurIPS 2024

Giving a language model the current timestamp is reasonable. Putting that changing timestamp on line one of a long system prompt is expensive.

That is the first practical note I'd bring an application team before walking through Zheng et al.'s SGLang paper. Serving optimizations make far more sense once engineers see how a one-line prompt edit silently invalidates GPU cache reuse across every request.

Exact prefix matching in RadixAttention

Before generating its first output token, a Transformer runs prefill over the prompt and builds key-value (KV) tensors for every input position. Retaining those KV tensors lets subsequent requests skip prefill across any shared prefix.

SGLang organizes retained KV blocks in a radix tree indexed by token sequences, a mechanism the authors call RadixAttention. Each incoming prompt walks the radix tree until a token diverges, then computes prefill only on the suffix that follows. Where vLLM's PagedAttention shares physical KV pages across concurrent sequences and reclaims them when a request finishes, RadixAttention retains finished KV pages in GPU memory under an LRU eviction policy so subsequent calls reuse them.

Prefix matching stops at the very first differing token.

Prepend a per-second timestamp or request UUID at token ten, and two requests sharing a 4,000-token policy and tool block miss the cache across everything underneath it. Place stable system instructions, tool schemas, and few-shot examples at the front, push dynamic user context and timestamps to the tail (while preserving role boundaries around untrusted input), and prefill reuse jumps immediately.

Comparing the two prompt layouts side by side shows why RadixAttention's prefix match halts at token four whenever current_utc sits on line one:

# Misses cache at token 4: dynamic current_utc precedes static prefix
uncached_prompt = (
    f"current_utc={current_utc}\n{STATIC_INSTRUCTIONS}\n{FEW_SHOT_EXAMPLES}\n{turn_input}"
)
# Preserves full prefix cache hit: static instructions and few-shot stay first
cached_prompt = (
    f"{STATIC_INSTRUCTIONS}\n{FEW_SHOT_EXAMPLES}\ncurrent_utc={current_utc}\n{turn_input}"
)

With a changing time first, two prompts differ before their shared instructions. With shared instructions first, the common prefix is reusable.
With a changing time first, two prompts differ before their shared instructions. With shared instructions first, the common prefix is reusable.

Using a calendar date instead of a per-second clock softens the penalty during the day, though midnight still flushes the shared prefix across the fleet at once.

Cache-aware routing and starvation limits

Ordering tokens inside the prompt solves only half the problem because the KV tree lives in worker GPU memory. Round-robin load balancing across a multi-node cluster scatters multi-turn sessions and shared-prefix workloads onto replicas that end up recomputing identical prefixes independently.

SGLang's December 2024 v0.4 cache-aware router maintains an approximate prefix tree per worker and trades cache affinity against queue depth, reporting up to 1.9x throughput on balanced long-prefix benchmarks.

Inside a single worker, SGLang prioritizes queued requests with longer matched prefixes to maximize cache hits before LRU eviction reclaims GPU memory. As the authors note in their limitations discussion, greedy cache-first scheduling can starve cold-prefix requests under heavy load unless bounded by a maximum wait-age fairness cap. Tracking p99 time-to-first-token segmented by feature path exposes starvation that aggregate cache-hit rates hide.

Separating prompt reuse from constrained decoding

Across its benchmark suite, SGLang reports up to 6.4x higher throughput than baseline serving setups. That headline combines three distinct wins: RadixAttention prefix reuse across multi-turn and agentic calls, interpreter parallelism, and compressed finite-state machines that jump multiple deterministic schema tokens in a single decoding step.

Multi-token schema jumps accelerate rigid JSON keys and brackets without speeding up free-form reasoning text inside string values, whereas prefix caching cuts prefill latency regardless of output format. Whenever a team rearranges prompt templates for cache hits, evaluate prefill token savings alongside p95 queue wait and output quality. Have you audited your production prompt templates to see where early dynamic fields break prefix caching?

Continue in Paper Reviews