Prompt Caching for LLM Chat Services: A Practical Setup Guide

    Learn how prompt caching can reduce long-context LLM API costs, why cache hits may disappear behind model gateways, and how to configure protocols, routing, and stable request prefixes. This guide also explains how to verify caching through token usage, request logs, and billing data.

    1. Why Long Conversations Become Expensive

    When building agents or multi-turn chat applications with LLM APIs, developers often discover that costs rise sharply as conversations grow. Each request carries more context, and a single turn can eventually cost tens of cents or even several dollars.

    Two factors drive this increase:

    1. Repeated billing for long contexts. In multi-turn conversations, the full message history is typically included in every request. Once the context reaches tens of thousands—or even 100,000—tokens, the application pays to process that history again, even if the latest user message is only one sentence.
    2. Missing cache hits. Many major model providers offer prompt caching, which discounts repeated prefixes across consecutive requests. Cache reads may cost around 10% of the standard input rate, depending on the provider. Some services also offer free or low-cost reads for a limited period after a cache write. Without cache hits, the same context is repeatedly billed at the full input rate.

    Consider a higher-priced reasoning model with input pricing of several dollars per million tokens and output pricing several times higher. A request containing 100,000 input tokens could cost more than $0.50 for input alone. If most of that input is a cached prefix billed at roughly one-tenth of the standard rate, the total cost per turn could fall to one-third of its previous level or less.

    The difference becomes substantial over a long conversation.

    2. How Prompt Caching and Billing Work

    Before configuring caching, it helps to understand what a cache hit actually requires.

    2.1 Cache Hits Depend on Matching Prefixes

    Prompt caching is fundamentally prefix caching. The service looks for a matching prefix between the current request and recently processed requests. Content at the beginning of a request—such as the system prompt, conversation history, or document context—can qualify for discounted billing when it matches cached content exactly.

    The practical implications are:

    • Keep the prefix identical. An extra space in the system prompt or a changed timestamp can prevent content after that point from matching.
    • Account for cache expiration. Caches generally have a time to live, or TTL, often measured in minutes. After expiration, the next request may need to create the cache again.
    • Expect new content to remain billable. Only the matching prefix is eligible for cache-read pricing. Newly appended content is charged at the applicable input rate.

    2.2 The Main Billing Categories

    Depending on the provider, input usage may be divided into three categories:

    • Cache writes: Content written to the cache for the first time. Some providers charge slightly more than the standard input rate for this operation.
    • Cache reads: Repeated content that matches an existing cache entry. The rate may be around 10% of standard input pricing.
    • Regular input: New content outside the cached prefix, billed at the standard rate.

    Output tokens are not covered by prompt-cache discounts and remain subject to output pricing.

    3. Common Reasons for Missing Cache Hits

    A frequent troubleshooting scenario is that caching works when calling a model provider directly but disappears when requests pass through a third-party gateway or aggregation service. Conversations still work, yet logs show no cache hits and every request is billed at the full input rate.

    Several issues can cause this behavior.

    3.1 Missing Cache-Control Parameters

    Caching is not entirely automatic in every model ecosystem. Some APIs require an explicit setting or cache breakpoint in the request.

    Common approaches include:

    • Adding cache-control metadata to a system message or a selected point in the conversation history.
    • Including a caching option in a request header or request body.

    The exact fields depend on the provider and API.

    If a gateway converts one API format into another, it may drop these fields during translation. In that case, the caching instructions never reach the model service.

    3.2 Cache Controls Lost During Protocol Conversion

    Many aggregation gateways expose a unified OpenAI-compatible API and translate requests into each model provider’s native format.

    However, there is no universal cache-control representation across OpenAI-compatible interfaces. A native parameter used to mark a cache breakpoint may have no corresponding mapping in the compatibility layer. As a result, caching behavior available through the native API may not carry over.

    This explains why a successful integration can still have no cache hits: request compatibility and caching support are separate concerns.

    3.3 Routing That Breaks Session Affinity

    A gateway may route a single model ID to multiple underlying providers. With lowest-price routing, random selection, or load balancing, consecutive requests in the same conversation may reach different backends.

    Cache availability is tied to the underlying service handling the request. A cache created through one backend may not be available through another. Unstable routing can therefore prevent repeated requests from benefiting from earlier cache writes.

    3.4 Unstable Request Content

    Application behavior can also break prefix matching:

    • Injecting a new timestamp or random ID into the system prompt on every request.
    • Changing the order or formatting of historical messages, including through compression or rearrangement.
    • Modifying tool definitions between turns.

    Even with caching configured correctly, these changes can reduce or eliminate the matching prefix.

    4. How to Enable and Configure Prompt Caching

    Use the following steps to address protocol, routing, and request-construction issues.

    4.1 Prefer the Model Provider’s Native API

    Where practical, switch the client from an OpenAI-compatible interface to the provider’s native protocol and corresponding endpoint.

    A native API either supports its own automatic caching behavior or exposes explicit cache controls, avoiding the risk that a compatibility layer will discard them.

    An illustrative configuration is:

    1. Model ID: <official model identifier>
    2. Base URL: <official model API endpoint>
    3. Protocol: Anthropic native API (example)
    4. API Key: <API key for the selected platform>

    If a third-party gateway is required, confirm that it forwards the relevant cache-control fields. Follow the gateway’s documentation for including explicit caching instructions in requests.

    4.2 Configure Routing for Session Affinity

    In the gateway’s settings or request parameters, replace random or lowest-price routing with an option that keeps a conversation on the same underlying provider.

    Depending on the gateway, available controls may include:

    • Explicitly selecting an underlying provider.
    • Choosing a provider-pinning or preferred-provider routing option.
    • Supplying a session-level identifier, such as a user identifier, when the gateway supports using it for routing.

    After changing the configuration, test several turns of a long conversation. Saving a routing setting is not sufficient evidence that caching works.

    Choosing the cheapest provider alone does not solve the problem. That provider may not support caching through the selected route, or successive requests may still reach different backends.

    4.3 Keep the Request Prefix Stable

    Structure requests to maximize the unchanged prefix:

    • Place fixed content—role instructions, tool definitions, and persistent knowledge—at the beginning, and keep it unchanged.
    • Put dynamic content, such as timestamps, retrieved material, and the latest user input, toward the end.
    • Preserve the order and formatting of conversation history rather than rewriting earlier messages between turns.

    This allows each request to reuse as much of the previous prefix as possible while adding new content at the end.

    5. How to Verify That Caching Is Working

    Configuration alone is not proof. Validate caching through usage data and actual charges.

    5.1 Inspect Response Usage Fields

    Look for cache-related token counts in the API response. Depending on the API, these may include:

    • cache_read_input_tokens
    • cache_creation_input_tokens

    In a repeated multi-turn test, a substantial cache-read count from the second request onward indicates that cached input is being reused. A cache-creation count indicates a write, not a read, so distinguish the two when reviewing results.

    5.2 Compare Per-Request Charges

    For requests with similar context lengths, cache hits should reduce input charges noticeably.

    In favorable cases, the total cost per turn may fall to roughly 20–40% of its previous level. The actual result depends on how much input is cached and how much of the bill comes from output tokens.

    5.3 Reconcile Charges Against the Account Balance

    Record the account balance before and after several conversation turns. Use the provider’s published rates to estimate the expected charges with and without cache hits.

    This provides a fallback check when logs do not expose detailed cache usage. It also helps confirm whether the discount is reflected in actual billing.

    5.4 Recognize Normal Expiration Behavior

    If the gap between requests exceeds the cache TTL, the next request may create the cache again. With providers that charge a write premium, that request may cost slightly more than regular input, followed by lower-cost cache reads on subsequent requests.

    An occasional cache write after a long pause does not necessarily indicate a configuration problem.

    6. Prompt Caching Checklist

    For long-context conversations, prompt caching can be one of the most effective ways to reduce LLM API costs. The key engineering checks are:

    • Protocol: Prefer the provider’s native API where practical, and verify that compatibility layers preserve caching controls.
    • Explicit configuration: Include the required cache-control parameters or breakpoints documented for the selected API.
    • Routing affinity: Keep requests from the same conversation on the same underlying provider.
    • Stable prefixes: Put fixed content first, append dynamic content later, and avoid rewriting earlier history.
    • Billing verification: Confirm cache reads through response usage, request charges, and account-balance changes.

    When a large share of input can be reused, these changes can reduce overall costs by more than half, with substantial savings for agent workloads and long-document analysis.

    Treat cache-hit verification as an acceptance check whenever integrating a new model provider or gateway—not as a billing investigation after costs have already accumulated.