Scenario
A request ID that opens the prompt
RolesInference EngineeringLLM Product EngineeringAgent Systems Engineering
- Engine
- SGLang 0.5.22
- Model
- Qwen/Qwen3-8B
- Hardware
- one L4 with 24 GB of memory, and a GB10 as the second machine
Situation
An agent product sends every request through a relay. For tracing, the relay puts a new first line in the system prompt, in the form Request id <hex>., with a new value on each call. The system prompt itself is 2,999 tokens long and never changes. The team serves Qwen/Qwen3-8B with SGLang on an L4, with --enable-cache-report on.
Users report that the first token takes about two seconds on every turn, even when they repeat a question. The team doubles the memory fraction, tries a different eviction policy and turns on hierarchical caching. The wait does not move. The metrics endpoint reports a cache hit rate near zero.
Questions
- The first listing shows five requests. What does the cached token count tell you about where the match stopped?
- How does the radix tree match a prompt, and why can nothing after the first difference match?
- In the third listing the id comes last in the prompt. Predict the cached token count before you read it, then explain the difference.
- Which three places can confirm a hit or a miss on a running server, from the closest to the request to the most general?
- The team tuned the cache tiers and the eviction policy first. Why did none of those settings help, and in which order do you change prompts and server flags?
Commit to a cause after each listing. Write it down before you read the next one.
Evidence
five requests whose system prompt starts with a request id
Read cached_tokens on every line, then ttft_ms.
{"case": "id-at-start", "round": 1, "ttft_ms": 2171.2, "prompt_tokens": 3032, "cached_tokens": 3}
{"case": "id-at-start", "round": 2, "ttft_ms": 2161.4, "prompt_tokens": 3034, "cached_tokens": 6}
{"case": "id-at-start", "round": 3, "ttft_ms": 2159.2, "prompt_tokens": 3031, "cached_tokens": 6}
{"case": "id-at-start", "round": 4, "ttft_ms": 2161.8, "prompt_tokens": 3031, "cached_tokens": 6}
{"case": "id-at-start", "round": 5, "ttft_ms": 2160.6, "prompt_tokens": 3033, "cached_tokens": 6}Source: manuals/sglang-101/capture/gpu/out-l4/cache-experiment.txt, lines 6-10
the same five requests with a stable system prompt
The first round fills the cache, so read the four rounds after it.
{"case": "stable-system-prompt", "round": 1, "ttft_ms": 2143.9, "prompt_tokens": 2999, "cached_tokens": 3}
{"case": "stable-system-prompt", "round": 2, "ttft_ms": 82.8, "prompt_tokens": 2999, "cached_tokens": 2998}
{"case": "stable-system-prompt", "round": 3, "ttft_ms": 85.1, "prompt_tokens": 2999, "cached_tokens": 2998}
{"case": "stable-system-prompt", "round": 4, "ttft_ms": 82.2, "prompt_tokens": 2999, "cached_tokens": 2998}
{"case": "stable-system-prompt", "round": 5, "ttft_ms": 80.7, "prompt_tokens": 2999, "cached_tokens": 2998}Source: manuals/sglang-101/capture/gpu/out-l4/cache-experiment.txt, lines 1-5
the same request id moved to the end of the system prompt
Compare cached_tokens with prompt_tokens on each line.
{"case": "id-at-end", "round": 1, "ttft_ms": 98.2, "prompt_tokens": 3033, "cached_tokens": 2983}
{"case": "id-at-end", "round": 2, "ttft_ms": 98.4, "prompt_tokens": 3032, "cached_tokens": 2987}
{"case": "id-at-end", "round": 3, "ttft_ms": 99.7, "prompt_tokens": 3033, "cached_tokens": 2986}
{"case": "id-at-end", "round": 4, "ttft_ms": 97.2, "prompt_tokens": 3030, "cached_tokens": 2985}
{"case": "id-at-end", "round": 5, "ttft_ms": 93.1, "prompt_tokens": 3032, "cached_tokens": 2987}Source: manuals/sglang-101/capture/gpu/out-l4/cache-experiment.txt, lines 11-15
the scheduler log while the three forms went through
Follow the cached-token field through the stable prompt, the id at the start, and the id at the end.
[2026-10-17 14:43:50] Prefill batch, #new-seq: 1, #new-token: 1, #cached-token: 2998, token usage: 0.13, #running-req: 0, #queue-req: 0, #pending-token: 0, cuda graph: False, input throughput (token/s): 0.99
[2026-10-17 14:43:50] INFO: 127.0.0.1:59136 - "POST /v1/chat/completions HTTP/1.1" 200 OK
[2026-10-17 14:43:51] Decode batch, #running-req: 1, #token: 3010, token usage: 0.13, cuda graph: True, gen throughput (token/s): 14.48, #queue-req: 0
[2026-10-17 14:43:53] Prefill batch, #new-seq: 1, #new-token: 3029, #cached-token: 3, token usage: 0.13, #running-req: 0, #queue-req: 0, #pending-token: 0, cuda graph: True, input throughput (token/s): 972.29
[2026-10-17 14:43:53] INFO: 127.0.0.1:59142 - "POST /v1/chat/completions HTTP/1.1" 200 OK
[2026-10-17 14:43:57] Prefill batch, #new-seq: 1, #new-token: 3028, #cached-token: 6, token usage: 0.13, #running-req: 0, #queue-req: 0, #pending-token: 0, cuda graph: True, input throughput (token/s): 966.95
[2026-10-17 14:43:57] INFO: 127.0.0.1:51474 - "POST /v1/chat/completions HTTP/1.1" 200 OK
[2026-10-17 14:44:00] Prefill batch, #new-seq: 1, #new-token: 3025, #cached-token: 6, token usage: 0.13, #running-req: 0, #queue-req: 0, #pending-token: 0, cuda graph: True, input throughput (token/s): 966.73
[2026-10-17 14:44:00] INFO: 127.0.0.1:51480 - "POST /v1/chat/completions HTTP/1.1" 200 OK
[2026-10-17 14:44:00] Decode batch, #running-req: 1, #token: 3037, token usage: 0.13, cuda graph: True, gen throughput (token/s): 4.41, #queue-req: 0
[2026-10-17 14:44:03] Prefill batch, #new-seq: 1, #new-token: 3025, #cached-token: 6, token usage: 0.13, #running-req: 0, #queue-req: 0, #pending-token: 0, cuda graph: True, input throughput (token/s): 965.89
[2026-10-17 14:44:03] INFO: 127.0.0.1:51494 - "POST /v1/chat/completions HTTP/1.1" 200 OK
[2026-10-17 14:44:06] Prefill batch, #new-seq: 1, #new-token: 3027, #cached-token: 6, token usage: 0.13, #running-req: 0, #queue-req: 0, #pending-token: 0, cuda graph: True, input throughput (token/s): 966.89
[2026-10-17 14:44:06] INFO: 127.0.0.1:51506 - "POST /v1/chat/completions HTTP/1.1" 200 OK
[2026-10-17 14:44:07] Decode batch, #running-req: 1, #token: 0, token usage: 0.00, cuda graph: True, gen throughput (token/s): 5.79, #queue-req: 0
[2026-10-17 14:44:07] Prefill batch, #new-seq: 1, #new-token: 50, #cached-token: 2983, token usage: 0.13, #running-req: 0, #queue-req: 0, #pending-token: 0, cuda graph: True, input throughput (token/s): 46.77
[2026-10-17 14:44:07] INFO: 127.0.0.1:41460 - "POST /v1/chat/completions HTTP/1.1" 200 OK
[2026-10-17 14:44:08] Prefill batch, #new-seq: 1, #new-token: 45, #cached-token: 2987, token usage: 0.13, #running-req: 0, #queue-req: 0, #pending-token: 0, cuda graph: True, input throughput (token/s): 42.41Source: manuals/sglang-101/capture/gpu/out-l4/server-default.log, lines 62-79
the summary of the same experiment on a GB10 with a larger model
The times differ from the L4, the cached-token counts do not.
{"summary": "stable-system-prompt", "median_ttft_ms": 44.75, "min_ttft_ms": 44.2, "max_ttft_ms": 48.4, "median_cached_tokens": 2998.0, "prompt_tokens": 2999}
{"summary": "id-at-start", "median_ttft_ms": 632.2, "min_ttft_ms": 631.0, "max_ttft_ms": 645.2, "median_cached_tokens": 6, "prompt_tokens": 3030}
{"summary": "id-at-end", "median_ttft_ms": 143.9, "min_ttft_ms": 139.5, "max_ttft_ms": 148.8, "median_cached_tokens": 2985, "prompt_tokens": 3032}Source: manuals/sglang-101/capture/gpu/out-gb10/cache-experiment.txt, lines 16-18
The answer
Show the walkthrough
Read the first listing one field at a time. Each request sends about 3,030 prompt tokens and the server reports cached_tokens of 3 or 6. The first token takes 2,159.2 to 2,171.2 ms on every round. So each request pays the full prefill of the system prompt, and the only tokens that the cache found are the few that come before the random hex digits.
The second listing is the control. The same question after a stable system prompt reports 2,998 cached tokens of 2,999 from the second round on, and the first token arrives in 80.7 to 85.1 ms. The first round shows 3 cached tokens and 2,143.9 ms, because it filled the cache. So the server and the model can serve this prompt about twenty-five times faster when the prompt matches.
The third listing moves the same random id to the end of the system prompt. The count rises to 2,983 to 2,987 cached tokens. The first token takes 93.1 to 99.7 ms. The tokens are almost the same in all three forms. Only their order differs. The radix tree compares tokens from the first one and stops at the first difference. Nothing after a difference can match, because the attention values of every later token depend on every earlier token. A cache that reused the later tokens would return wrong answers.
The scheduler log shows the same story from the server side. The stable rounds log #cached-token: 2998 with one new token. Then five prefill batches log about 3,025 to 3,029 new tokens with 3 or 6 cached. Then the id-at-end rounds log 45 to 50 new tokens with 2,983 to 2,987 cached. The last listing repeats the experiment on a GB10 with a mixture-of-experts model. The times are different, 632.2 ms against 44.75 ms, and the cached token counts are the same: 6 against 2,998.
Show the resolution
The relay put a changing value at the start of the only part of the prompt that every request shares. One edit fixed it. Move the trace id out of the system prompt and send it as request metadata, or put it at the end. In our measurement on the L4 the median first token fell from 2,161.4 ms with the id at the start to 98.2 ms with the id at the end, and the median cached token count rose from 6 to 2,986 of about 3,030.
Prove the fix from three places. Start the server with --enable-cache-report and read cached_tokens in the usage block of each response. Read #cached-token on each Prefill batch line of the scheduler log. Read sglang:cache_hit_rate from /metrics. On vLLM, read vllm:prefix_cache_hits_total and vllm:prefix_cache_queries_total. The cache settings that the team changed first were not the problem, because no eviction policy or host tier can match tokens that differ from the start.
Show the wrong turns
“Raise the memory fraction so the cache holds more.”
The pool was not full. The misses came from the prompt order, so a larger pool holds more blocks that nothing matches.
“Turn on hierarchical caching.”
A host tier keeps evicted prefixes, and nothing was evicted. The prefix never matched in the first place.
“The hit rate is low because the questions differ.”
The question is about 30 tokens at the end of a prompt of 2,999 tokens. A changing question costs about 30 tokens of prefill, not 2,999.
“Hash the system prompt and route by it.”
Routing matters when several servers split the traffic, but this was one server, and the prompt differed on every request before routing could help.
Show the general rule
Treat the prompt layout as part of the serving system. Put the content that never changes first, the content that changes per session next, and the content that changes per request last. Keep ids, timestamps, hashes and random seeds out of the system prompt, and keep tool definitions and JSON keys in a fixed order. Before you change any cache flag, read the cached token count on your real requests. A prompt fix can recover a hit rate that no server flag can.
Taught in
The SGLang manual measures the three prompt forms in the prefix-friendly prompts page and explains the tree in the RadixCache page, the vLLM manual gives the same rule in its prefix cache page, and the RadixAttention lesson builds the mechanism from scratch.