AlgoViz

KV cache and prefix caching

medium 40 min

While a model writes, it saves the key and value vectors of every token it has already seen, so each new token costs one small step instead of a pass over the whole text.

Where it shows up

The same pattern appears across everyday software.

  • A support bot with a long policy

    Every request starts with the same few thousand tokens of policy. With prefix caching on, only the user's message is prefilled, and time to first token drops. Putting the time at the top would undo it.

  • Coding agents

    An agent sends its tools, instructions and the growing history on every turn. Append-only history keeps the whole earlier conversation cached, which is why the Manus team treats hit rate as a key metric.

  • Chat over one long document

    Users ask many questions about the same contract or manual. The document is prefilled once and later questions reuse its blocks, as vLLM's prefix caching guide describes.

  • Few-shot classification at scale

    Millions of short inputs share one block of examples in front. The examples are computed once per replica, and the router keeps the traffic where they are cached.

  • Parallel sampling and search

    Asking for several answers to one prompt, or exploring a tree of reasoning steps, shares the prompt's blocks across all branches instead of copying them.

  • Paying less on hosted APIs

    Teams on Anthropic, OpenAI or Gemini order their prompts so that the long stable part comes first, and read cached-token counts in the usage fields to confirm hits.