Stable Prefix

Orden pensado para la caché

Escrito por Darjus Kazakevic · revisado el

Fixed part first. One changed byte makes everything after it a cache miss.

Prompt caching works on an exact prefix (the leading tokens of a prompt). The provider keeps the processed form of a prefix it has seen. A later request that starts with the same tokens skips that work, so it is cheaper and faster. One changed character near the top breaks the match for everything after it.

Details vary by provider. Some cache automatically once a prompt passes a minimum length, often about 1,024 tokens. Others need you to mark where the cached part ends. Cached input is usually billed at a fraction of the normal price. Unused entries expire, by default often within minutes. In every case, the order of the prompt decides the hit rate.

En esencia — Put the parts that never change on top and the per-request part last: the prefix becomes cacheable, and the question lands where models read best.

Cuándo recurrir a él

Recurre a él cuando

  • A large fixed start: long system prompt, tool schemas, many examples
  • Many questions about the same document
  • Agent loops, which resend a growing transcript on every turn

Reconsidéralo cuando

  • Short prompts below the provider's caching minimum
  • Requests that share nothing worth reusing
  • Reordering a readable prompt before there is any cost problem

The parts

The prefix
Everything from the first token to the first thing that changes between requests: system prompt, tool definitions, examples, a shared document. Usually most of the tokens.
The tail
What changes per request: the user's question, retrieved passages, the current time. It goes at the bottom, after everything reusable.
The boundary
The line between prefix and tail. Some providers find it themselves; others let or require you to mark it. Placing it on purpose turns a lucky cache hit into a planned one.

Reglas de diseño

Most stable first, per-request last
System prompt, tools, examples, shared documents, then the question. This also puts the question at the end, where models recall best, so cost and quality agree.
Find the hidden variable
A timestamp in the system prompt, a request id, a set iterated in a new order: each one drops the hit rate to zero. The prompt still looks the same in review. This is the bug that really happens.
Render the prefix the same way every time
Cache hits need byte-identical prefixes. A prompt assembled differently per branch creates several prefixes. Each gets a slice of the traffic, and none stays in the cache.
Measure the hit rate
Most providers report cached and uncached input tokens in each response. A prompt you think is cached but is not costs full price, silently. Reading the code will not tell you.

Errores habituales

  • A timestamp, request id or random value above the boundary
  • A new rule added to the top of the system prompt, invalidating everything
  • Assuming caching works without checking the cached-token count