LLM Prompt Cache Misses: Find the Changes That Break Prefix Reuse
Your agent sends almost the same instructions and tools on every turn. Yet the provider reports few cached input tokens, and each conversation costs more than expected. The missing piece may be a timestamp near the top of the prompt—or a tool list assembled in a different order on each request. This guide shows how to investigate LLM prompt cache misses with a small, privacy-aware diagnostic. You…
This guide aims to help investigate LLM prompt cache misses by comparing request structures, identifying changed prefixes and measuring if a fix saves money. There are several caches that can exist in an AI application, each solving different problems. The focus of this article is on the first layer, prompt prefix caching.
To begin, identify which cache is missing. There can be multiple caches in an AI application, each serving a different purpose. Layer reuses involve cache hits on an earlier, identical prefix. The model still generates a fresh answer and may not run at all. Retrieval systems may skip some work.
An answer-cache freshness rule cannot determine why a provider recomputed an unchanged system prompt. A prompt-prefix hit does not guarantee that a retrieved document is current or that an answer is correct. Cache boundaries, routing, minimum lengths, expiration and billing vary between providers and even between models.
Start with two locally constructed request objects, having the same model, tool definitions, instructions and request options. Change only the final user message. Capture the provider's reported input and cache usage for both calls. Repeat this process with several pairs to rule out a single miss being due to a defect.
Compare the figures with a cold run and the expected reusable portion. Keep the exact request body local and short-lived, as logs should not include customer messages or secrets. A cold first request is expected, while a second request can miss due to the prefix being too short, cache expiry, routing, model or endpoint changes, or the provider not caching that shape.
Inspect the assembled request in the order relevant to your provider, focusing on tool definitions, system instructions, examples, conversation messages and the new input. Their actual rendering order may differ, depending on the provider. Use a simple fingerprint function to generate hashes of each block, avoiding the need to dump the text.
Restrict access to the diagnostic key, rotate it regularly, and set a retention limit to prevent sensitive operational data from being compromised. Note that the example sorts object keys for stable diagnostic representation, but the actual payload or provider serialization may differ.
A system instruction such as "Current time: 10:01:03" changes on every call. If the model requires exact time, place it in the changing user or task context where the provider's request design permits. If only the date is needed, use the date, but only when the loss of precision is acceptable for the task.
Finally, never freeze time merely to improve a cache metric, as stale time can make a correct workflow incorrect.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to