Prompt Caching Through a Gateway: Measure Hits, Misses and Real Savings

Learn how to plan prompt cache keys, measure hits and misses, count cache writes, and decide whether caching lowers total AI task cost.

Axiom Studio Axiom Studio December 1, 2026 7 min read

A prompt cache can make a repeated request faster and cheaper. It can also disappoint. A changed tool list, a new system rule, or a long gap between requests may turn an expected hit into a full-price call. The right question is not “Is caching on?” It is “How much did the full task cost after misses, writes, retries, and review?”

Start with the part of the prompt that stays the same

Many agent calls share a long prefix: system rules, tool descriptions, a product guide, or a codebase map. The user question and fresh tool results change on each call. Put stable context first and changing context later. A provider can then reuse eligible prefix work when a later request matches its cache rules.

OpenAI describes prompt caching as reuse of a matching prompt prefix. Anthropic supports explicit cache breakpoints and cache lifetimes. These are different provider designs. Do not assume a cache key that works for one model gives the same result on another. Check each provider’s current rules before you set a gateway policy.

For a support agent, the stable part might be approved answer rules and a product manual. The new ticket belongs at the end. For a coding agent, a stable repository guide may be reused, while the changed files and test output remain fresh. The goal is to reuse context that has not changed, not to reuse an old answer.

A clean request order

  • Put shared instructions and tool definitions first.
  • Add stable reference text that is safe to reuse.
  • Mark or configure the cache boundary where the provider supports it.
  • Put the user’s task and live data after that boundary.
  • Record the provider, model, prompt version, and cache result for each call.

A gateway can help keep those records in one place. The LLM Gateway page describes per-provider token, cache-write, cache-read, latency, and cost views. That is useful when one team uses several models. The provider still decides which prefix can be cached and for how long.

Cache keys should track meaning, not a lucky string

A cache key should change when the shared context changes. Include the prompt template version, tool schema version, and the version of any reference document used in the prefix. If the tenant or access scope changes what the model may see, keep those boundaries separate too. Do not make one tenant’s private prompt available to another tenant through a shared key.

Avoid adding a request ID, timestamp, or random order to the stable prefix. Those fields make every request look new. Put them after the reusable part, or keep them in gateway logs. Also avoid treating an exact cache key as a promise of a hit. Providers may have minimum prefix sizes, lifetime rules, routing rules, or capacity limits.

This is why a simple “cache enabled” flag is weak evidence. A useful dashboard shows eligible requests, hits, misses, writes, and the reasons that can be seen. If the provider does not expose a miss reason, label it unknown. Do not guess.

What to record per call

  • Provider and model, because cache rules and prices differ.
  • Prompt and tool-schema versions, so a changed prefix is visible.
  • Uncached input, cache-write, cache-read, and output tokens when reported.
  • End-to-end latency, including retries and fallbacks.
  • The final task outcome and human review time.

The same request may take a fallback route after a provider error. That second call can use a different cache and price. Count both calls in the task total. A provider switch is not a free retry.

Compare the whole task, not one request

Use a short cost worksheet before promising savings. For each attempt, multiply the reported token type by that provider’s current rate. Add all attempts, any tool charges, and the team’s review work. Then compare that total with a no-cache baseline on the same task set. Prices change, so keep rates as inputs rather than hard-coded facts in the article.

python
# cost_per_task.py — fill rates from current provider price pages
def call_cost(tokens, rates_per_million):
    return sum(
        tokens.get(kind, 0) * rate / 1_000_000
        for kind, rate in rates_per_million.items()
    )

task_cost = sum(call_cost(c["tokens"], c["rates"]) for c in calls)

This is a worksheet pattern, not a benchmark result. The calls input must include misses, retries, and fallbacks. A team can add human review minutes as a separate line item, using its own internal labor rate. Keep the raw usage record beside the result so another reviewer can check the math.

Three outcomes to separate

Outcome What happened What to check
Hit A reported prefix was reused Read tokens, latency, final answer quality
Miss Eligible work was billed as new input Prefix version, lifetime, provider rule
Write New prefix work entered cache Write charge, likely reuse before expiry

A prompt-cache measurement flow separates stable prompt layers, cache writes and reads, and provider routing.

A cache hit helps only when reuse beats writes, misses, retries, and review.

The cache only pays when reuse beats its write cost and operating work. A large reference prompt sent once per day may not behave like one sent many times per minute. If the content changes every call, remove it from the cached prefix. If it must be current, freshness matters more than a high hit rate.

For a broader budget view, Axiom Studio’s AI spend management guide shows why owners need cost by team and use case, not only one model bill. Cache records should feed that same view.

Use cases where the trade-off is clear

Repeated support questions. An approved product guide is shared across many tickets. Keep that guide in the stable prefix. Put each customer’s question and account data in the changing part. Measure whether the prefix is actually reused, and test that answers still reflect the current guide version. When the guide changes, invalidate the old version.

Coding agent tasks. A repository map and team coding rules may be reused across several tasks. A pull request diff should not be cached as shared context for a later, different pull request. Version the map when files or rules change. Count the cost of extra review if a stale map leads the agent to suggest the wrong file.

Provider failover. A gateway may route a failed call to another provider. The second provider can have different cache behavior, token counts, and prices. Keep the same task ID across both attempts, but record each attempt separately. A low request price is not enough if retries raise cost per accepted answer.

Run a small, honest test

Pick a set of real tasks with private data removed. Run them with the same prompt order, model, and quality checks. Compare a cold call, a near-term repeat, a repeat after the likely cache lifetime, and a call after a prompt-version change. Do not call a cache hit successful unless the response passed the task check.

Review hit rate, cost per accepted result, p95 latency, retry count, and human review time together. If a high hit rate does not lower total task cost, look for expensive writes, long outputs, frequent fallbacks, or work the team must redo. For a wider cost view, Axiom Studio’s guide to AI token costs explains input, output, cache-read, and cache-write tokens.

Prompt caching is useful when a team has a stable prefix and enough reuse before it expires. Start by making the prefix safe and measurable. Then use gateway records and provider usage fields to prove whether the saving is real.

Frequently asked questions

What is prompt caching?

Prompt caching reuses eligible work from a matching prompt prefix, such as stable system rules, tool definitions, or reference text, instead of processing that prefix as new input on every call.

What should a team record for each cached request?

Record the provider, model, prompt and tool-schema versions, uncached input, cache-write and cache-read tokens when reported, output tokens, latency, retries, fallbacks, outcome, and human review time.

How should a prompt cache key be designed?

Make the key change when the shared context, tenant, access scope, prompt template, tool schema, or reference document version changes. Keep request IDs, timestamps, and random ordering out of the stable prefix.

How do you know whether prompt caching saves money?

Compare the full task cost with and without caching, including cache writes, misses, retries, fallbacks, tool charges, output, and review work. Use the same task set and current provider rates.

What is a good prompt-caching test?

Run real tasks with private data removed across a cold call, a near-term repeat, a repeat after the likely cache lifetime, and a prompt-version change. Check hit rate, cost per accepted result, p95 latency, retries, and review time together.

Axiom Studio

Written by

Axiom Studio

Turn AI governance insight into evidence

Get weekly governance insights for engineering leaders, then put them to work with VibeFlow.