LLM Prefix Caching: How Prompt Cache Hits Cut Token Costs

    Prefix caching (also called prompt caching) explains why a long multi-turn agent session can cost far less than its raw token count suggests. This article breaks down the mechanism behind it: the reuse of Key/Value (KV) vectors for unchanged prefixes. It covers a representative measurement in which more than 99% of a request's tokens were cache hits, the three-tier billing model built on new input, cache writes, and cache reads, and how cache behavior constrains API pricing decisions, GPU memory management, cluster scheduling, and agent framework design. It closes with five practical ways to keep hit rates high: stable prefixes, append-only histories, removing volatile content, controlling cache lifetimes, and structurally separating the dynamic part of a prompt.

    A Counterintuitive Bill

    When you review usage after an agent task finishes, the numbers can look contradictory. The model may have been called hundreds of times, and every request carried the full conversation history, tens or hundreds of thousands of tokens. By that logic, total cost should scale linearly with token volume. The actual bill, however, often comes in far below what you expected.

    The source of that savings is recorded in a dedicated field in the usage report: on top of ordinary input tokens, the system separately reports the portion of input tokens that were cache hits and reused. That field reflects one of the low-level rules of LLM inference: prefix caching, also known as prompt caching. Understanding it means understanding a shared set of constraints across API pricing, GPU memory management, cluster scheduling, and agent framework design.

    The Technical Essence: Reusing the KV Cache

    To understand cache hits, start with how a Transformer-based LLM generates text.

    These models work autoregressively: they produce one token at a time, and generating the next token requires “looking back” at every token that came before. Mechanically, each token computes its own Key and Value vectors (KV) in the attention layers, and the attention computation for every subsequent token depends on them.

    The key insight is that as long as a given prefix (the system prompt, prior conversation turns) stays unchanged, the KV vectors computed for it do not change. Recomputing the KV state for tens or hundreds of thousands of tokens on every request wastes enormous compute and also slows down time to first token.

    A cache hit does one thing: it stores the already-computed KV state for the prefix, and when the next request arrives with an identical prefix, the system reads and reuses that state, skipping the computation entirely.

    1. Request 1: [system prompt 2K] + [history 117K] + [new question] <-- first time: compute and cache KV for the first 119K
    2. Request 2: [system prompt 2K] + [history 117K] + [new answer + follow-up] <-- first 119K hit the cache; only the new part is computed

    What the Numbers Look Like: a 119K Prefix and 875 New Tokens

    The per-step profile of a typical coding agent is representative. To execute one ordinary step, the agent reads roughly 119K tokens of historical prefix, while only about 875 new tokens actually need to be computed from scratch.

    In other words, more than 99% of the tokens in a request are old material being resubmitted. Without caching, those 119K tokens would be recomputed at full input price every time. With cache hits, the vast majority is billed at a small fraction of the standard input price (a common industry approach is around one-tenth or less), and only the 875 genuinely new tokens are billed at the standard rate.

    This is the direct reason why hundreds of calls with hundreds of thousands of tokens each do not produce a proportionally larger bill: you pay full price for new content and only a fraction of that for old content.

    The Three-Tier Billing Structure

    Most mainstream inference services bill along three tiers, and understanding them is central to cost optimization:

    • New input (miss): Tokens seen for the first time are billed at the standard input price. This is the most expensive component.
    • Cache write: Some services charge a write fee slightly above the standard input price when a cache entry is first created, because storing the KV state adds overhead.
    • Cache read (hit): Reused on subsequent requests, these are billed at a price far below the standard input rate, and this is where the savings come from.

    An important corollary follows. Cache hits make reads cheap, but creating a cache may have a cost, and cached entries have a lifetime (a TTL, typically on the order of a few minutes). Cost savings therefore depend on the same prefix being hit repeatedly before it expires, not on stuffing as much content as possible into the context.

    How Cache Hits Constrain the Whole Stack

    The rule behind that single usage field propagates downward through the system.

    API pricing

    Because cache reads, cache writes, and standard input are priced separately, how you structure your requests directly determines your cost profile. When evaluating models, don’t look only at the headline per-token price; look at expected hit rates in long-context scenarios as well.

    GPU memory management

    Cached KV state occupies GPU memory or a dedicated cache pool. The more cache entries there are and the longer the prefixes they cover, the more memory is consumed. Inference systems have to balance memory capacity against hit rate and concurrency, and rely on eviction policies to free the least recently used entries when memory is tight.

    Cluster scheduling

    To make caches reusable, schedulers tend to route requests carrying the same prefix to the same node (cache affinity). In multi-replica clusters this adds scheduling complexity, but it is essential to keeping hit rates high.

    Agent framework design

    This is the layer closest to developers: how an agent organizes its prompts directly determines its hit rate.

    Engineering Practices for Better Hit Rates

    To make caching actually save money, the core principle is simple: keep unchanged content as early, as stable, and as frequently reused as possible.

    1. Put the stable prefix first. Place the system prompt, tool definitions, and fixed instructions at the very beginning, and keep them byte-for-byte identical throughout the session. Changing a single character breaks the cache from that point onward.

    2. Append only; never rewrite. In multi-turn conversations, do not go back and edit messages once they exist. Add new content at the end so the prefix grows monotonically.

    3. Strip out volatile content. Avoid placing timestamps, random IDs, the current date, live retrieval results, or other fields that change on every call in the system prompt or anywhere early in the context. They pollute the prefix and invalidate the cache as a whole.

    4. Manage the cache lifetime. Issue requests that share a prefix in batches or in quick succession, and keep them within the cache’s TTL window. Long gaps let the cache expire and degrade back to full recomputation.

    5. Structurally separate the dynamic part. Decouple the stable, large prefix from the volatile tail:

    1. system = fixed instructions + tool definitions + output spec # stable prefix, high hit rate
    2. history = confirmed conversation history # append-only, grows over time
    3. dynamic = current user input + this turn's temporary data # changes every turn, goes through fresh compute

    Summary

    A cache hit may look like just one field in a usage report, but it is the core mechanism through which LLM inference trades space for compute. By reusing the KV state of unchanged prefixes, it turns the most expensive part of a long context into the cheapest. It is shaped jointly by the billing model, GPU memory capacity, cluster scheduling, and framework design. For developers, the practices of stable prefixes, append-only history, removing volatile content, and managing cache lifetime are enough to cut the inference cost of a long-context agent below what intuition would predict, without sacrificing capability.