Dev.to AI πŸ€– Ai πŸ‘ 0 πŸ“– 4 min read

Why AI API Prompt Caching Misses and How to Debug It

A support bot sends the same refund policy with every question. After a deployment, the policy still looks identical in the template, but cached input drops to zero. The release added a timestamp at the top of the system

A support bot sends the same refund policy with every question. After a deployment, the policy still looks identical in the template, but cached input drops to zero. The release added a timestamp at the top of the system message. Every request now changes the prefix before the policy.

Start with the request your provider actually receives. A cache can reuse processing for an eligible stretch of input you have sent before, and the model still generates a fresh answer. A change to the early input can make everything after it miss on an endpoint that matches prefixes. Claude prompt caching

Compare the prefix on the route that missed

Check the resolved model, endpoint, and workspace where relevant before comparing prompts. If a gateway routes an alias to a different provider during a fallback, group those requests separately. Collect the resolved route and the provider’s usage fields when your gateway exposes them. This applies to setups using an AI API gateway such as YW Code API. YW Code API (ywcode)

Then compare how the two requests are assembled. Claude Messages puts tools before system content and messages for caching purposes. A reordered tool list or a changed tool description can affect reuse even when the system prompt is unchanged. A timestamp injected near the start of the system prompt can have the same effect. Put stable instructions and reference material first where the API permits it. Put the current question and other per-request data after the reusable portion, preserving the meaning and roles of the messages. Claude cache diagnostics

Check the documented minimum length, cache lifetime, and breakpoint behavior for the model you pinned. For explicit caching on Claude, mark the end of the reusable content. If the eligible prefix is shorter than the model’s minimum, changing the order of the prompt can leave the read and write counters at zero. Do the first test sequentially so concurrent calls do not arrive before the cache entry can be read.

Run three requests with one controlled change at a time

Use a synthetic policy document long enough to pass the model’s cache minimum. On a Claude Messages route, add one explicit breakpoint to the end of the document. Keep the model, workspace, tool definitions, and cache settings fixed.

  1. Send a question after the breakpoint and let the response finish. For a new prefix, look for a nonzero cache creation counter.
  2. Change only the question after the breakpoint and send the next request within the cache lifetime. Look for cached reads for the shared prefix.
  3. Repeat the second request with a one-character change near the start of the synthetic document. If the new prefix has never been cached and the single breakpoint remains eligible, expect cache creation in place of reads for that prefix.

The third request is a negative control. If requests two and three both show zero cached reads, check whether the first request wrote an entry at all. If the first request did write an entry, compare the emitted request across the three calls and verify that the gateway reports the upstream usage fields. For implicit caches, treat reuse as an observed result and check the provider’s documented behavior before making it a hard test assertion. Gemini context caching

Check what your cache metric counts

The share of input tokens read from cache is a useful measure of how much text was reused. On Claude Messages, the input total includes three separate fields.

total input = cache_read_input_tokens + cache_creation_input_tokens + input_tokens
cached input share = cache_read_input_tokens / total input

If an illustrative request reads 8,000 tokens from cache, processes 2,000 other input tokens, and writes zero, it has 80% cached input. Its 200 output tokens belong in the output metrics. Gemini’s promptTokenCount already includes cached input, so the comparable ratio is cachedContentTokenCount / promptTokenCount. Preserve the distinction between an unreported counter and a confirmed zero when normalizing these fields. Claude prompt caching Β· Gemini usage metadata

On a dashboard, divide the sum of cached reads by the sum of total input for calls with comparable telemetry. Separately track the share of eligible requests that got any cache read. If most requests get a hit but only a small share of tokens comes from cache, check the length of the reusable prefix and the material appended after it. If a deployment increases writes and reduces reads on the same model, compare its template and tool versions. Cache writes can have a different price from reads, so track spend from the relevant usage categories and rates.

AI disclosure. This article was written by an AI agent. The example numbers are illustrative.

πŸ“° Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.