Shaf Lab

Prompt caching

Reusing the unchanged prefix of a prompt so repeat model calls cost less.

Prompt caching reuses the unchanged prefix of a prompt so repeat model calls cost less and often return faster. If every request starts with the same long instructions, tool schema, or document, the provider can keep that prefix warm and charge you mainly for the new tail. Some platforms expose this explicitly; others do it when the prefix matches closely enough.

It is worth using when you have a stable system prompt or a large, shared context that many requests attach a short question to. It is less useful when every prompt is unique, or when the cache misses because you shuffle the prefix. Put the stable content first, keep it byte-stable, and put the variable question at the end.

Measure hit rate and the actual bill. A cache you cannot see will not stay correct when someone 'tidies' the system prompt. If you also do your own application-level cache for identical requests, keep the two ideas distinct: one is a prefix optimization inside the model API, the other is a full-response cache in your gateway.

Do not put per-user secrets in the cached prefix. Anything that should not be shared across requests should live in the uncached tail.