Troubleshooting LLM API Prompt Cache Hits and Optimizing Costs
Prompt caching can dramatically reduce the cost of long-context LLM API calls, but a working API connection does not guarantee cache hits. This article explains the prerequisites for prompt caching, five common reasons caches fail to hit (protocol adaptation layers, backend routing drift, model version mismatches, unstable request prefixes, and TTL misalignment), a systematic six-step troubleshooting process, and cost governance principles for turning caching into a real, measurable savings mechanism.
In long-context, multi-turn conversation, and AI-assisted coding scenarios, a single request often carries a large system prompt, tool definitions, conversation history, and lengthy documents. If every call is billed on the full input, costs escalate quickly. Prompt caching is designed to bill repeated prefix content as cache reads instead of fresh input tokens, significantly reducing the cost of long-context calls. A common engineering problem is this: the API connects fine, the model responds normally, yet the bill doesn’t drop and no cache read/write metrics appear in the logs. This shows that “the call succeeded” and “the cache hit” are two different things, requiring systematic troubleshooting across four layers: protocol, routing, request structure, and billing semantics.
1. Understanding the Prerequisites for Prompt Cache Hits
Prompt caching is typically not a global cache that automatically applies to arbitrary requests. It is a reusable cache built around the request prefix. Within a request, stable segments at the front—system prompts, tool definitions, long documents, conversation history—are more likely to be cached, while dynamically appended user questions, timestamps, and random IDs should be placed at the end. Caching generally involves three stages: cache write, cache read, and rebuild after expiry. The first request may incur a write cost; subsequent identical prefixes are billed as cache reads; after the TTL expires, the cache must be rebuilt.
Therefore, judging whether a cache hit occurred cannot rely on balance changes or on whether a single response succeeded. The reliable approach is to inspect usage details: input tokens, cache write tokens, cache read tokens, hit rate, and per-request cost. If the logs show only regular input tokens and no cache read tokens, the cache is almost certainly not in effect. Also note that caching is tightly coupled to model version, provider, region, protocol, and request prefix—any change along one of these dimensions can prevent cache reuse.
2. Why “The API Works” Doesn’t Mean “The Cache Hits”
Reason 1: The protocol compatibility layer drops cache semantics. Many model routing gateways offer both a generic chat completions protocol and the model’s native protocol. With the generic protocol, the gateway converts fields between request and response; if cache markers, cache breakpoints, or related control fields are not passed through, the backend model never receives explicit cache instructions and processes the request normally. The interface looks fine, but caching capability has been silently weakened by the compatibility layer.
Reason 2: Routing policies cause backend drift. Aggregation gateways may automatically select different providers, regions, or instances based on price, latency, availability, or weight. Caches generally do not span providers or regions, and may even be isolated across deployments of the same provider. If each request is routed to a different backend, an identical prefix still won’t hit reliably. Price-first routing is common in cost-sensitive scenarios but can sacrifice cache continuity; you may need to pin routing or constrain the acceptable provider set based on business goals.
Reason 3: Inconsistent model identifiers or versions. Some model aliases point to different versions or different backends. The caller believes it’s using the same model, but requests land on different deployments. Cache keys typically include model version information, so old caches naturally become invalid after a version change. When troubleshooting, confirm that the model identifier, version, and provider remain stable, and avoid switching frequently within a long session.
Reason 4: Unstable request prefixes. Inserting the current time, random request IDs, user nicknames, or dynamic permission lists into the system prompt—or having unstable tool definition ordering or JSON serialization order—all change the prefix content and prevent cache key reuse. In multi-turn conversations, truncating, reordering, or summarizing history also breaks the original prefix. The first step of cache optimization is making stable content truly stable.
Reason 5: Cache TTL mismatches the calling cadence. Caches have lifetimes. Low-frequency calls, long idle gaps, and rebuilds after concurrency spikes can all degrade hit rates. For interactive coding assistance or customer service Q&A, evaluate cache TTL against session rhythm, and avoid treating low-frequency long texts as high-frequency cache prefixes.
3. A Systematic Troubleshooting Process
Step 1: Establish usage observability. Don’t estimate from balance changes. Collect per-request input tokens, output tokens, cache write tokens, cache read tokens, model identifier, provider, region, protocol mode, and latency. Aggregate hit rates and per-request costs by session, model, and provider to find anomalous requests.
Step 2: Run an A/B test with a fixed prefix. Prepare a sufficiently long fixed system prompt and a fixed long document, then issue multiple requests with identical prefixes while only varying the trailing user question. Compare cache read tokens and costs between “explicit cache configuration” and “default configuration.” If both are identical and neither shows cache read tokens, the caching pipeline is not working.
Step 3: Check protocol and field pass-through. Confirm whether the caller uses the target model’s native protocol or a generic compatibility protocol, and whether the gateway supports and forwards cache control fields. When in doubt, follow the platform documentation, prefer the native protocol mode, and minimize middleware rewriting of the request body.
Step 4: Pin routing and provider. Change automatic routing to a fixed provider or region, or lock to cache-capable backends within an allowed range. Fixed routing sacrifices some elasticity, but for long-context, high-reuse conversational workloads, cache gains usually outweigh the marginal benefit of routing flexibility.
Step 5: Optimize request structure. Place stable content—system prompts, tool definitions, long documents—at the front, and dynamic content at the end. Keep tool definition ordering and serialization format stable. Preserve stable history prefixes in multi-turn conversations. Avoid writing timestamps, random numbers, or frequently changing permission data into the prefix. The request structure can be abstracted as:
model: "<provider>/<model-id>"base_url: "https://<gateway-host>/v1"protocol: "native-messages" # Prefer the target model's native protocolrouting: "fixed-provider-or-region"cache: "explicit-prefix-marker" # Field names and values per platform documentation
The above is illustrative only; concrete fields must be configured according to the relevant API documentation—do not assume all gateways support the same parameters. If the platform requires explicitly marking cache breakpoints, ensure the marker lands at the end of the stable prefix and verify that the backend recognizes it.
Step 6: Validate and monitor. After going live, continuously watch the proportion of cache read tokens, hit rate, per-request cost, and P95 latency. Set alerts for sudden hit-rate drops and prioritize investigating model version upgrades, routing policy changes, system prompt edits, and tool definition changes. Caching is not a set-and-forget configuration—version iterations and prompt adjustments can invalidate old caches.
4. Cost Governance and Risk Control
The core of cache optimization is not blindly chasing high hit rates, but balancing write costs, read savings, and business stability. The first cache write may incur extra cost, so low-frequency long texts may not be worthwhile. Fixed routing reduces elasticity. If cache prefixes contain sensitive information, isolation and permission boundaries must be evaluated. A layered approach by scenario is recommended: fixed prefixes for high-frequency sessions, fixed document versions for long-document Q&A, and fixed tool definition ordering for tool calling; don’t force caching on low-frequency or highly dynamic content.
In summary, API connectivity only proves availability. Cache hits require protocol pass-through, stable routing, consistent models, and stable prefixes all working together. By locating problems through usage details, running controlled experiments with fixed prefixes, preferring native protocols with pinned backends, and continuously monitoring hit rates and costs, you can turn prompt caching from “appears to be supported” into an engineering capability that actually reduces spend.
