Module 08 · AI · Serve the GPT you built · with code: Python · about 12 min to read
Continue where you left off ×
The Prefix Cache: Computed Once
Prefix Caching · Open board
The Prefix Caching board shows two messages that start with the same standing instructions, and the trick that reads those instructions once. This lesson builds that trick on the toy engine. Stage 4 of the serving series, after Build Your Own Inference Engine .
Before you start. Serve 01: The KV Cache built the cache, the saved keys and values that mean a position is never recomputed. Paged Attention: The Cache in Blocks cut that cache into fixed blocks of positions, and this lesson names those same blocks. Batching: A Slot That Frees Early is the engine around them. The no-code version of this idea sits later in this track as Prefix Caching (the same start, computed once) , and this lesson only builds it.
The idea in 30 seconds
A position’s saved keys and values depend only on the characters before it, so a shared opening can be computed once.
A block is named by a hash of its characters and its parent’s name, so one name stands for a whole prefix and a lookup finds the entire shared opening.
A new prompt walks its blocks from the front, copies every named block it finds, stops at the first miss, and always computes its last character.
Hit rate tracks the shared prefix: 3, 51 and 86 percent at 0, 16 and 28 shared characters, and computed work fell from 31.2 characters to 4.4.
The toy’s time to first token barely moves, because Python overhead dominates a pass this small. On a real model, prefill is arithmetic, and a 90 percent hit turns a 90 ms prefill into about 9.
Read the story
Lesson 74 of 83 in AI
The rest of this lesson is part of a plan.
A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year .
See plans Already a member? Sign in
Or start free : a free account opens lessons 2 and 3 of every track.
In this lesson December 2023: a lab notices that its traffic starts the same way The one fact that makes sharing safe Naming a block so a lookup can find it What the cache is judged by What the toy cannot show The tag on each drawer One letter walks the drawers Was the saving real? What if the tag forgot the one before it? All of it in code Terms it explains: RadixAttention, prefix, block name, hit rate, fingerprint.
Review AI
Mark as read Saved in this browser, and to your account when you sign in.