How LLM Cache Hits Affect Token Costs

    Agent token bills are not driven only by how many tokens you send. This article explains how prefix cache hits change input pricing, why shrinking context can raise cost, and which engineering practices keep prefixes stable for cheaper cache reads.

    1. The starting point: an Agent’s token bill is not only “tokens sent”

    In coding agents, automated task runners, and multi-turn chat systems, model calls are usually chains of requests rather than one-off Q&A. Each step may carry a system prompt, tool definitions, history, tool results, and the latest user input. As steps accumulate, input tokens grow quickly.

    A common misconception is that cutting the number of tokens you send always lowers cost. In practice, input tokens are often billed in different ways: regular input, cache write, and cache read. Whether a request hits cache can change the effective price of those tokens.

    When you evaluate Agent cost, look at cache hit rate, prefix stability, and whether missed prefixes must be written to cache again—not only at total input tokens.

    2. What a large-model cache hit is

    Think of a request as a stack of pages in order: system prompt on top, then tool definitions, history, tool results, and finally this turn’s new content. The service first checks whether the start of the request matches a previous request exactly.

    If two requests share an identical prefix token-by-token from the beginning, that prefix can reuse previously computed intermediate state. That is a cache hit. The hit portion is often billed at a lower cache-read price; the miss portion must be processed again and may be billed as cache write or regular input.

    Simplified:

    1. Request A: [system prompt][tool definitions][history 1][history 2][new question A]
    2. Request B: [system prompt][tool definitions][history 1][history 2][new question B]

    If B shares the system prompt, tools, and history with A, the shared prefix can hit cache. New question B at the end does not break prefix reuse.

    If B prepends a changing timestamp to the system prompt, the prefix has already changed. Later content usually cannot be reused even if it is identical.

    3. How cache hits change the cost structure

    Input cost can be abstracted as:

    1. Total input cost = missed input × miss unit price
    2. + hit input × cache-read unit price
    3. + possible cache-write cost

    Cache-read prices are often much lower than cache-write prices. In one example price set, cache read is $0.01 per million tokens and cache write is $0.125—a 12.5× gap. Hit-rate changes are amplified by that gap.

    Case 1: Full history can be cheaper

    In a replay experiment, two groups used the same long session. Group 1 kept full history: about 5.25 million input tokens, $0.084. Group 2 used a sliding window that truncated old tool outputs each step: about 2.09 million tokens, but $0.263.

    Group 1 hit cache on about 95% of input, so most tokens were billed as cheap reads. Group 2 had a 0% hit rate. It sent ~60% fewer tokens, but those tokens were reprocessed and often billed at write prices. Cost differed by about 3.1×.

    Reducing token count does not always reduce cost. If the reduction breaks prefix stability, cheap cache reads become expensive cache writes.

    Case 2: One timestamp, a large price gap

    Two groups each sent about 5.25 million input tokens with the same model, history, and tool calls. The only difference: one kept a static system prompt; the other prepended a changing current time.

    Costs were about $0.084 vs. about $0.658—about 7.8×. Total latency for 40 requests rose from 149s to 170s.

    The timestamp sat at the front of the system prompt, so every request changed the prefix and later content could not reuse cache, even when history, tools, and results were unchanged.

    4. Why Agents are especially sensitive to cache

    Simple Q&A is mostly a system prompt plus the current question; cache impact is limited. Agents are different. A coding agent’s next request often includes:

    1. System prompt
    2. Tool definitions
    3. All prior messages
    4. Tool results
    5. New observations or instructions

    Early tool output may stay in context even if the current step no longer uses it. A single request can reach hundreds of thousands of tokens; 40 steps can reach millions.

    If every step rewrites history, injects dynamic variables, reorders tools, or puts volatile content first, hit rate collapses and most input is billed as misses.

    5. What breaks cache hits

    Hits require a stable prefix. These operations usually hurt hit rate:

    1. Dynamic content at the start of the system prompt

    Current time, random request IDs, temporary user IDs, live environment data. If they sit at the front, every change invalidates everything after.

    2. Truncating or rewriting history on every step

    Sliding windows are not always wrong, but if each step trims old tool output differently, the historical prefix keeps changing and cache cannot match.

    3. Unstable tool-definition order

    Random tool list order or dynamically rewritten tool descriptions can break prefix identity even when meaning is similar.

    4. Frequent message-structure changes

    Reordering, merging, splitting, or summarizing early messages changes the token sequence. Summaries that differ each time also break cache.

    5. Ignoring model and parameter consistency

    Cache typically depends on request-context consistency. Changing model version, decoding parameters, or other fingerprint-related settings can block reuse.

    6. Engineering practices that improve cache hits

    1. Keep the system prompt static

    Avoid timestamps, random values, and live state in the prompt. If you must send dynamic data, put it at the end of the request, not at the prefix start.

    Suggested layout:

    1. [fixed system prompt]
    2. [fixed tool definitions]
    3. [stable history]
    4. [this-turn dynamic info]

    2. Put volatile content last

    Dynamic time, temp variables, and environment state belong in the new-message region so they do not invalidate an already-formed prefix.

    3. Design sliding windows carefully

    Shorter context helps, but do not regenerate a different summary or truncation every step. Prefer stable segments, e.g. keep history on fixed message boundaries instead of compressing dynamically each step.

    4. Keep tool definitions stable

    Names, descriptions, parameter schemas, and order should stay fixed. If the tool set changes, put the change as far back as possible so long prefixes stay intact.

    5. Make summaries cache-friendly

    If you must summarize, keep the summary stable for a period instead of regenerating it every request. Otherwise the summary becomes an unstable prefix.

    6. Monitor hit rate and cost

    Track input tokens, hit tokens, miss tokens, cache writes, and cost by request type. Total tokens alone mislead.

    Useful metrics:

    1. Cumulative input tokens
    2. Cache-hit tokens
    3. Miss tokens
    4. Cache-read cost
    5. Cache-write cost
    6. Output token cost
    7. Average per-step latency

    7. Core takeaway for cost decisions

    Cache hits are not just a latency optimization; they reshape Agent billing. Fewer input tokens do not always save money; a less stable prefix can multiply cost.

    In practice, stabilize the request prefix first, then compress context. For long Agent chains, a static system prompt, fixed tool definitions, and reusable history often control cost better than cutting tokens alone.