Prefix caching
Same beginning, one copy.
Two requests arrive with the same first 512 tokens: the same system prompt. Same tokens at the same positions give exactly the same k and v. Paged as before, each gets its own copy: the same numbers written twice.
The prefix arithmetic, once
Position ‘s key and value are functions of tokens , so two requests can share cache exactly for their longest common prefix and nothing past it. A shared prefix of tokens across requests costs tokens of cache instead of , and a newcomer with an -token prompt prefills tokens:
In a chat of turns with question and answer , prefill without caching sums to and with it to . SGLang’s RadixCache is the tree, reference counts pin the pages of running requests, eviction is least-recently-used over unreferenced nodes, and --schedule-policy lpm orders the waiting list by matched length. The default policy is fcfs, and lpm itself falls back to it past 128 waiting requests; a newcomer’s prefix is matched when it is admitted under any policy. Matching works in whole pages, so with page size up to tokens at a boundary are prefilled again, and an answer’s last token always is, because its k and v were never written.