Many real workloads repeatedly send requests that share a long common prefix — a system prompt, a few-shot template, a long document being asked multiple questions about. Recomputing that shared prefix's KV cache from scratch every request wastes both compute and time-to-first-token. Implement automatic prefix caching: given a store of previously computed KV-cache blocks keyed by the hash of the token sequence they represent, find the longest matching prefix of a new request's tokens already in the store, and report how many tokens (and blocks) of prefill computation can be skipped by reusing it.
hash_i = H(hash_{i-1}, block_i) with hash_{-1} = SEED -- chained hash per block
reused_tokens = block_size * (number of leading blocks whose chained hash is a cache hit)
block_size (a trailing partial block never counts as reusable and is not added to the store as a partial hash).i requires every earlier block to also match.Chain the hash across blocks (hash_i depends on hash_{i-1}) so a match at block i can only happen if every earlier block matched too — this is what makes it a genuine PREFIX match rather than a coincidental match of an isolated block deeper in the sequence.
Stop walking forward at the first block whose chained hash is not already in the store — every block after a miss must be treated as new regardless of its own content, since the chaining means a later "match" would be coincidental, not a genuine shared prefix.
Click "Run Tests" to test your implementation