How SageMaker’s Prefix‑Aware Routing Cuts LLM Latency

When an LLM‑powered app repeats the same opening text for every request – for example a policy block in a support bot – the model recomputes that block thousands of times. SageMaker’s new prefix‑aware routing directs requests that share the same opening to the same instance, letting the instance reuse its cached key‑value (KV) pairs. The result is a noticeable drop in time‑to‑first‑token (TTFT) and a modest boost in throughput.
How the routing works under the hood
LLM serving frameworks such as vLLM and TensorRT‑LLM can store the KV pairs that result from processing a prompt prefix. KV pairs are the internal representation the model builds for each token; once they exist, the model can skip the heavy attention calculations for those tokens on subsequent calls. Prefix‑aware routing inspects the first N bytes (or characters) of each incoming payload, hashes that slice, and always forwards matching hashes to the same machine. If the chosen machine is already busy, SageMaker falls back to a less‑loaded instance – a safeguard that prevents a single node from becoming a bottleneck.
Benchmark snapshot
The blog post measured Llama 3.1 70B Instruct on a fleet of seven ml.p5.48xlarge instances (each with eight NVIDIA A100 GPUs). The tests compared the new routing strategy with the default random routing while keeping prefix caching enabled.
| Workload | Metric | Random routing | Prefix‑aware routing |
|---|---|---|---|
| Long context (8 000‑token shared prefix) | P50 TTFT reduction | – | 71‑77 % |
| Long context | KV cache hit rate | ~25 % | > 80 % |
| Long context | Throughput increase | – | 15‑16 % |
| Short context (ShareGPT‑style) | P50 TTFT reduction | – | 13‑16 % |
| Short context | KV cache hit rate | ~30 % | 30‑80 % |
| Short context | Throughput increase | – | 1.7‑2.0 % |
| All tests | Routing overhead per request | – | 1.3‑1.9 ms |
Model TTFT in the runs ranged from 63 ms to 280 ms, so the extra 1‑2 ms from routing was negligible. Traffic remained evenly split across the seven instances (13.3‑15.4 % each), confirming that the overload protection kept the fleet balanced.
Typical scenarios that benefit
- Retrieval‑augmented generation (RAG). When a document is fetched and prepended to every user query, all queries about that document share a long prefix. Routing keeps the document’s KV cache warm on one node.
- Multi‑turn chats. Each turn repeats the full conversation history, so the shared history grows longer with every exchange. Sending all turns of the same conversation to the same instance preserves the cache across turns.
- Templated bots. Bots that always send a block of policy text, formatting rules, or persona description gain the most because that block can be tens of thousands of tokens.
- Code completion assistants. When a developer asks for completions within the same file, the file’s content is sent as a prefix for every request.
If your workload does not reuse a substantial prefix – for example a pure question‑answer service where each prompt is unrelated – the routing strategy offers little advantage and may even add a tiny latency cost.
Trade‑offs and practical considerations
The headline numbers look impressive, but a few hidden costs matter in day‑to‑day operations. First, prefix length selection is critical. Setting PrefixLength too short funnels many unrelated requests to the same node, causing the ConcurrencyThreshold overflow rule to kick in and discard cache hits. Setting it too long makes even minor differences (like a different temperature value) scatter requests that should stay together, reducing cache reuse. The recommendation is to start with the exact byte length of the shared block plus a modest buffer (e.g., 4 KB for a 3 KB policy text) and monitor hit rates.
Second, serialization consistency matters. For the native Invoke API the routing hash is computed on raw bytes, so whitespace, JSON key order, or line breaks can change the hash and send identical prompts to different instances. Using a deterministic serializer (compact JSON without pretty‑printing) eliminates this source of drift.
Third, you still need at least two instances. With a single node the routing decision is moot, and you won’t see any cache‑hit improvement because the cache would be warm regardless of routing.
Finally, monitoring is essential. SageMaker can emit KV‑cache‑hit‑rate metrics per model variant. If you see hit rates stuck around the baseline 25‑30 %, either the prefix isn’t being recognized consistently or the PrefixLength is mis‑configured.
Enabling prefix‑aware routing in a single step
- Turn on prefix caching in your serving framework (vLLM enables it by default; other frameworks may need an explicit flag).
- Create or update the endpoint configuration with the new routing strategy. Example CLI snippet (replace placeholders with your own values):
aws sagemaker create-endpoint-config \ --endpoint-config-name my‑llm‑config \ --production-variants '[{ "VariantName": "AllTraffic", "ModelName": "my‑llm‑model", "InitialInstanceCount": 3, "InstanceType": "ml.p5.48xlarge", "RoutingConfig": { "RoutingStrategy": "PREFIX_AWARE", "PrefixAwareRoutingConfig": { "PrefixLength": 4096, "ConcurrencyThreshold": 10 } } }]' - Deploy the endpoint with the usual
aws sagemart create-endpointcall. - Leave your client code unchanged – the same
InvokeEndpointor OpenAI‑compatible Chat Completion calls work as before. - Add optional tenant isolation if you run a multi‑tenant service: include the
X‑Amzn‑SageMaker‑Prefix‑Aware‑Idheader (native) orprompt_cache_keyfield (OpenAI API) to keep different customers’ caches separate.
Once the endpoint is live, watch the SageMaker metrics dashboard for KVCacheHitRate. If the rate climbs above 70 % you’re likely seeing the full benefit; if not, revisit the PrefixLength and serialization steps.
What you can try right now
Pick a simple RAG prototype that prepends the same document to every user query. Deploy the model on SageMaker with two ml.p5.48xlarge instances, enable PREFIX_AWARE routing with PrefixLength set to the document size plus 1 KB, and run a handful of queries. Compare the latency you see in CloudWatch Logs with the same setup using the default RANDOM strategy. The difference will be immediate evidence of whether your workload fits the pattern.


