Prompt Caching for Open Weight LLMs with SGLang RadixAttention
In this blog we explain how we explain the prompt caching for self hosted LLM using SGLang's RadixAttention and quantify the benefits
Introduction
In this blog we explain how to implement prompt caching for self-hosted open weight LLMs. On engines like SGLang, we can leverage RadixAttention, which reuses the KV state for any shared prefix, skipping prefill compute entirely.
We quantified the improvements on AWS with DeepSeek-R1-Distill-Qwen-1.5B and -7B on a single A10G GPU. The improvement metrics are given below:
- Time-to-first-token (TTFT) dropped 87–90% on a cache hit for a 2K-word shared prefix, and up to 96.5% at an 8K-word prefix.
- ~99.9% of prefill tokens were eliminated — 2,870 of 2,871 prompt tokens were served from cache.
- At an 80% cache-hit rate, that translates to an estimated ~72% lower cost per request for the 7B model.
The GitHub repo for the Amazon SageMaker notebooks used for the experiment is given at the end of the blog.
How does the prompt cache work?
The "prompt cache" supported by LLMs essentially has three components:
- A mechanism to keep the key/value (KV) tensors computed for a prefix so they can be reused.
- Routing to make sure that the subsequent requests reach the same replica that holds those tensors.
- A billing construct to meter the reused tokens and charge a discounted rate for them.
Out of the above three components, (1) is the core of the prompt caching solution and that entirely belongs to the inference engine like vLLM/SGLang.
Why input tokens are expensive?
Typically, an LLM inference has two phases:
- Prefill phase wherein the model processes the entire input prompt in one forward pass and builds a KV cache for every token. Cost scales with prompt length.
- Decode phase the model generates output tokens one at a time, reading the KV cache. Cost scales with output length.
In typical agentic systems, the prompt consists of system instructions, tool schemas, few-shot examples and the user query. In most agentic systems, except the user query all other components remain static across conversations. The total token length for system instructions, tool schemas and few-shot examples range from 2K tokens (for simple chat agents) to 20K tokens (for complex use cases like voice agents).
Instead of pre-filling the KV cache every time for these static tokens, we could cache this prefix so that we can skip the prefilling step and compute only the new tokens for the dynamic user query. Prefix cache thus reduces the TTFT (Time to First Token), increases the throughput per GPU and reduces cost per request.
Prefix caching in open-source inference engines
Two open-weight engines implement this today:
- SGLang → RadixAttention has KV-cache blocks stored in a radix tree keyed by token prefix. Any request that shares a prefix walks the tree, finds the longest cached match and reuses those blocks. The feature is automatically on by default. It also supports cache-aware routing so requests with the same prefix hit the same worker.
- vLLM → Automatic Prefix Caching (APC) uses hash-keyed KV blocks.
In the self-managed LLM deployment model, we can use it to lower TTFT and get higher GPU throughput that reduces the overall cost per token.
Approach and experiment design
We designed the experiment to quantify the benefits of prefix caching. This section details the experiment. We used a smaller model and a relatively larger model for the experiment:
-
deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B(small)
-
deepseek-ai/DeepSeek-R1-Distill-Qwen-7B(larger)
Both are reasoning distillations of DeepSeek-R1 into the Qwen2.5 architecture.
Serving stack: We used SGLang v0.5.14 from the official
lmsysorg/sglang:latest Docker image (a self-consistent CUDA + PyTorch + prebuilt-kernel environment) with RadixAttention enabled (default).Hardware: a single AWS g5.xlarge — NVIDIA A10G, 24 GB, driver 595.71, Ubuntu 24.04.
Method. For each request we send a streaming
/generate call and measured:- TTFT — clock time to the first streamed token.
-
cached_tokens— reported by SGLang'smeta_info.
We compare two conditions on the same server:
- Cold — flush the cache (
POST /flush_cache) so the prefix is prefilled from scratch.
- Warm — send the identical prompt again so the prefix is served from the radix cache.
We fix the decode length at 32 tokens so TTFT is dominated by prefill, take the median of 5 warm repetitions, and increase the shared-prefix length from 256 to 8,192 words.
Results
Cache hit vs. miss (2,871-token prompt)
Model TTFT cold TTFT warm Reduction Prefill tokens (warm)DeepSeek-R1-Distill-Qwen-1.5B 190.6 ms 23.7 ms 87.6% 1 of 2,871 (2,870 cached)DeepSeek-R1-Distill-Qwen-7B 692.3 ms 69.3 ms 90.0% 1 of 2,871 (2,870 cached)On a warm hit the engine reports
cached_tokens = 2,870 out of 2,871. Only the single new boundary token is recomputed. The entire shared prefix is served from the KV cache. That's a ~99.9% reduction in prefill work, and it shows up directly as an ~8–10× lower TTFT.The benefit grows with prefix length
Prefix (words) 1.5B cold 1.5B warm 1.5B reduction 7B cold 7B warm 7B reduction256 34.8 ms 22.5 ms 35.3% 136.0 ms 68.6 ms 49.6%512 66.3 ms 22.7 ms 65.8% 210.2 ms 68.6 ms 67.4%1024 92.7 ms 22.8 ms 75.4% 366.9 ms 68.6 ms 81.3%2048 190.4 ms 24.0 ms 87.4% 691.6 ms 69.2 ms 90.0%4096 364.1 ms 28.7 ms 92.1% 1,372.4 ms 70.0 ms 94.9%8192 564.3 ms 33.9 ms 94.0% 2,035.9 ms 70.5 ms 96.5%The evident pattern is as follows:
- Warm TTFT stays nearly flat (~22–34 ms for the 1.5B, ~69–71 ms for the 7B).
- Cold TTFT grows with prefix length because of the token re-computation.
The longer your shared context, the more you save. This is the ideal profile for agentic AI systems where system instructions, tool schemas, few-shot examples are static and the dynamic values are minimal.
Figure 1 — TTFT reduction vs prefix length.

Figure 2 — TTFT cold vs warm.

Bigger models save more in absolute terms
At an 8K-word prefix the 7B model saves ~1,965 ms per request versus ~530 ms for the 1.5B. Prefill cost scales with model size, so the larger the model, the more painful a cache miss, and the bigger the payoff from caching.
The cost model
We proxy GPU-seconds per request by TTFT (prefill-dominated at a fixed 32-token decode) and price a g5.xlarge at $1.006/hr on-demand.
Model GPU-sec cold GPU-sec warm All-cold $/1M req $/1M req @ 80% hit SavingsDeepSeek-R1-Distill-Qwen-1.5B 0.1906 0.0237 $53.27 $15.95 ~$37.3 (70%)DeepSeek-R1-Distill-Qwen-7B 0.6923 0.0693 $193.46 $54.18 ~$139 (72%)At an 80% cache-hit rate we are running at roughly 30% of the all-cold cost. The savings scale with both your hit rate and the fraction of your prompt that's shared.
Figure 3 — Cost vs cache-hit rate.

Key best practices to note
- Put stable content first, variable content last. Caching is prefix-based, so structure determines your hit rate. An ideal structure is as follows: System prompt → tools → few-shot → retrieved context → user query. A single dynamic token near the top (a timestamp, a request ID, a per-user name) invalidates everything after it.
- Mind the TTL/eviction. SGLang evicts with LRU; a prefix survives only while it's warm. High-traffic shared prefixes stay cached for free; rare ones may need pinning or a longer TTL.
- Route by prefix. With multiple replicas, send requests sharing a prefix to the same worker (SGLang's cache-aware router) or you will miss the cache.
Running it on Amazon SageMaker
The companion notebook does exactly this: deploy DeepSeek-R1-Distill-Qwen-1.5B on an
ml.g5.xlarge endpoint, benchmark cold vs. warm latency, and tear the endpoint down.When to still use a managed cache
Self-hosting captures these savings directly, but it comes with management overhead. Managed prompt caching with Amazon Bedrock wins when you want zero ops burden, elastic scaling and an SLA. Self-hosting with SGLang wins when you have steady, high-volume workloads with large shared prefixes, want full control over routing and eviction, or run models the managed platforms don't offer caching for.
Conclusion
In this blog we quantified the benefits of KV/prefix reuse that is built into SGLang and vLLM, on by default, and delivers 87–96% lower TTFT and ~72% lower cost on real DeepSeek models. If you self-host, you're likely leaving this on the table today. Turn it on, structure your prompts prefix-first, and measure it with the harness below.
Reproduce it
- Benchmark harness (GPU host):
sglang_prompt_caching_benchmark.ipynb
- SageMaker endpoint notebook:
sagemaker_studio_prompt_caching.ipynb
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article