Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Module 08 · AI · Serve the GPT you built · with code: Python · about 15 min to read

Play it as a journeyTutor comes with a plan

Serve 01: The KV Cache

The Two Clocks
The Two Clocks · Open board

The Two Clocks board puts one request on a ruler, with a long first wait and then a steady tick per word. This lesson builds the thing that keeps that tick short. Stage one of the serving series that Build Your Own Inference Engine maps.

Before you start. A GPT writes a reply one character at a time, and each character needs one pass through the model, the forward pass. In Sampling and What Scale Buys you ran that pass over the last 32 characters for every new character. Attention, from Attention by Hand, makes two vectors from each character, a key and a value, so that later characters can look back at it. Later in this track, Prefix Caching (the same start, computed once) and The Two Clocks (TTFT, TPOT, and why goodput is the number) explain the serving side of this without code, so read them after if the numbers here leave a question. Here you write it.

Lesson 70 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 plansAlready a member? Sign in

Or start free: a free account opens lessons 2 and 3 of every track.

In this lesson

  • Every character, the whole page again
  • What attention keeps: a key and a value per character
  • Two phases: prefill the prompt, decode the rest
  • Did it change the answer?
  • What it saves, measured
  • What the cache costs: memory
  • The 32-character window and the context shift
  • Where does the margin live?
  • What does one new word cost?
  • What happens when the margin fills up?
  • Did the notes pay?
  • All of it in code

Terms it explains: KV cache, prefill, decode, crossover, context shift.

Related paper

Review AI