Prefix Caching with a Radix Tree

hard
Unit 25 · Inference III0/0 to milestone

Many requests share a prefix, such as the same system prompt or few-shot examples. Inference servers like SGLang (RadixAttention) and vLLM keep the KV cache of previous prompts in a token trie, so a new request only computes the tokens after its longest cached prefix.

Pro problem

Unlock this and every premium problem across all tracks — plus hidden-test grading, advanced AI engineering modules and new content every week.

Start 7-day free trial
solution.py · Python 3.13
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Prefix Caching with a Radix Tree · TechGradify