CustomLabs
Cost & routing

How do you get the discount that prompt caching promises?

Updated First published

5 min read

Markdown

Intent

Put invariant content first in the prompt and per-request content last, so the shared prefix stays byte-identical and the cache hits. Prompt caching pays off only when the shared prefix is byte-identical across requests. One volatile token near the front busts the cache for the entire call. System instructions and tool definitions belong in the invariant part. Few-shot examples belong there too. Retrieved chunks and user input go last. A timestamp goes there as well.

Also known as cache-friendly prompt ordering, stable-prefix ordering, prefix cache optimization

Problem

What problem does this solve?#

Prompt caching recognizes that the start of a request matches the start of an earlier request. It then skips reprocessing of that shared prefix. A prompt template can put something volatile near the front. A timestamp or a per-request ID has this effect. A user name inserted early has it too. A tool list with an unstable serialization order also breaks the prefix. So does retrieved RAGRetrieval-Augmented Generation (RAG) retrieves relevant passages at query time and feeds them into an LLM's context. content placed before the static instructions. Any of these changes the first tokens of every request. The cache then misses for everything after that point, even when the rest is unchanged.

This is easy to miss. A human skimming the prompt sees it as basically the same, and the system still works correctly. The defect affects cost and latency only, and correctness is untouched. For that reason it goes unnoticed until someone reads cache-hit-rate data directly.

Mechanics

How does it work?#

The team orders the prompt template deliberately. System instructions come first. Tool definitions follow in a fixed, deterministic order that never depends on map iteration or registration sequence. Few-shot examples come next. Anything that varies per call goes last. That includes user input and retrieved content. A timestamp goes there too, if the prompt needs one at all.

The team removes volatile content that the prompt does not need. It pushes the remaining volatile content as late as the model allows. No template-authoring convenience justifies placing it ahead of the static instructions. The team canonicalizes tool and few-shot ordering once. Incidental object or map ordering can change between deploys without warning, so the order must not depend on it.

The team tracks the cache-read share of input tokens per feature as a first-class KPI. It puts that share on a dashboard, as it would latency or error rate. A caching regression then raises a specific, immediate alert. An example is a new dynamic field that slips into the prefix. This beats discovering a slow, unexplained bill increase weeks later.

Shape

What are the moving parts?#

  1. Order the prompt invariant-first

    Place system instructions first, then tool definitions, then few-shot examples, then per-request content.

  2. Serialize tools and few-shot examples deterministically

    Fix the order for every call. Map iteration and registration sequence play no part.

  3. Remove volatile tokens that do not need to be there

    Drop timestamps and request IDs from the prefix wherever the model does not need them.

  4. Push necessary dynamic content as late as possible

    Place variable content only as early as the model needs it for its reasoning.

  5. Track cache-read share as a dashboarded KPI

    Monitor this share per feature, as you monitor latency or error rate.

  6. Alert on cache-hit-rate drops

    Treat a caching regression as a latency spike that needs a response.

Fit

When does it fit, and when doesn't it?#

Use it when

  • The provider or platform supports prompt caching, and the system prompt and tool definitions stay mostly static across calls
  • Cache-read token share in provider usage data is low, although consecutive prompts look similar
  • A prompt template places a timestamp or request ID near the start. A per-user greeting there has the same effect
  • Call volume and prompt length are high enough that the caching discount is a material line item

Don't use it when

  • The provider or self-hosted setup does not support prompt caching. Reordering the prompt buys nothing there, and a shorter prompt or smaller model is the lever
  • Every call is unique from the first token. Full personalization leaves no shared static prefix across any two requests, so no stable prefix exists to preserve
  • Prompts are short enough that the caching discount is negligible either way. The reordering effort exceeds the marginal saving
Trade-offs

What does it cost?#

Gain

Reordering alone, with no model or infrastructure change, can unlock a caching discount that was available but unclaimed

Cost

Existing prompt templates may need a real refactor to move dynamic content out of the prefix. That refactor touches code at every call site

Gain

Removing timestamps and IDs from the prefix is often a pure win

Cost

Some volatile content must sit early in the prompt so the model can reason about it correctly. Moving everything to the end can hurt output quality

Gain

Canonical tool and few-shot ordering is a one-time fix that keeps paying off

Cost

It needs discipline to maintain. A new tool that skips the canonical serialization order regresses the cache hit rate again without warning

Gain

Cache-hit rate as a dashboarded KPI catches regressions immediately

Cost

Someone must build and watch that dashboard. A silently degrading discount is the failure this pattern exists to prevent, so skipping the metric defeats the point

Signals

How do you know it's working?#

  • Cache-read token share of total input tokens, tracked continuously per feature
  • Byte-identical-prefix check between consecutive same-feature requests, sampled periodically
  • Measured inference costInference cost is what it costs to run a trained model on a request. compared with modeled cost, watched for the gap this pattern closes
  • Alert firing rate on cache-hit-rate drops, to confirm the alert is not noise
Prevents

What failure modes does this prevent?#

Sources

  1. arXiv - Prompt Cache: Modular Attention Reuse for Low-Latency Inference

    The paper describing prefix-based attention reuse. Our prefix-ordering guidance follows this same mechanism. Retrieved 2026-09-17.

  2. FinOps Foundation - FinOps Framework Overview

    The cost-management framework our cost model's phases map onto. Retrieved 2026-08-24.

Not sure this is the right pattern?

A Ship Audit checks which of these patterns your system needs. It ranks them by what is most likely to break first.

Book a Ship Audit →

Source: https://customlabs.io/patterns/stable-prefix-prompt-caching/

↑↓ navigate ↵ select esc close