Skip to content
mentoor

Topic 2 of 5 · 5 min read

Cutting repeat input cost with prompt caching

Goal: Restructure a request so its stable prefix is cached and confirm the hit rate from the usage fields.

Prompt caching lets the provider reuse the work on the start of your prompt, so put everything that stays the same at the front, everything that changes at the back, and mark where the stable part ends.

Check yourself · 1 of 3

Why does a prompt that starts with the current timestamp get no cache hits?

Choose one answer

Why it matters

Many apps resend the same large block on every call: a long system prompt, tool definitions, a policy document or a codebase summary. Without caching you pay full input price for it each time. A cache read costs about a tenth of normal input on Claude, according to its pricing page, and OpenAI also discounts cached input. For a chatbot with a 6,000-token prefix, that is the biggest single saving available.

How it works

Caching matches on an exact prefix. The provider hashes the start of the request. If a later request begins with the identical tokens, those tokens are read from the cache. One changed character inside the prefix breaks the match from that point on.

  • Claude: you add a cache_control breakpoint to mark the end of the reusable part. A write costs more than normal input (1.25 times for the 5-minute cache, 2 times for the 1-hour cache) and reads cost 0.1 times. The default lifetime is 5 minutes and each hit refreshes it.
  • OpenAI: matching is automatic for long enough prompts, with no code change, and an optional cache key helps route similar requests together.
  • Prompts below a minimum length are not cached, and the minimum varies by model.

How to do it

  1. Order the request: tools, system prompt, reference documents, then history, then the new user message.
  2. Remove anything volatile from the front: timestamps, request ids, user names and random examples belong after the stable part.
  3. Place the breakpoint at the end of the last stable block (Claude) or keep the prefix identical (OpenAI).
  4. Read the usage fields: cache write tokens on the first call, cache read tokens on the next. Hit rate is cache reads divided by all input tokens.
  5. Match lifetime to traffic: if calls come less often than every 5 minutes, the cache expires between them.

Before and after

Weak: the system prompt begins with "Today is 2026-12-02 14:31:07" and then a 6,000-token policy. The first line changes every second, so nothing is ever read from the cache.

Fixed: policy first, breakpoint after it, then the date and the user question. The policy is now a hit on every call within the lifetime.

What goes wrong

  • A timestamp or id at the top of the prompt quietly zeroes the hit rate.
  • Caching a prefix that is reused only once, so you pay the write premium and never get a read.
  • Tool definitions that change order between calls.

Examples

Marking the stable prefix on Claude

const res = await client.messages.create({
  model,
  max_tokens: 500,
  system: [
    { type: "text", text: POLICY_DOCUMENT, cache_control: { type: "ephemeral" } },
    { type: "text", text: `Today is ${today}. User: ${userName}.` },
  ],
  messages: [{ role: "user", content: question }],
});
console.log(res.usage.cache_creation_input_tokens, res.usage.cache_read_input_tokens);

The policy is cached. The date and name come after the breakpoint, so they can change without breaking it.

Reading the hit rate from a log

{ "call": 1, "input_tokens": 40, "cache_creation_input_tokens": 6000, "cache_read_input_tokens": 0 }
{ "call": 2, "input_tokens": 55, "cache_creation_input_tokens": 0, "cache_read_input_tokens": 6000 }

Call 1 writes the prefix. Call 2 reads all 6,000 tokens from the cache. If call 2 showed a write again, something in the prefix changed.

Key points

  • Caching matches an exact prefix, so stable content goes first and changing content goes last.
  • On Claude a cache write costs more than normal input and a read costs a fraction of it, so a prefix must be reused to pay off.
  • The default cache lifetime is 5 minutes and each hit refreshes it, so sparse traffic can miss every time.
  • A timestamp or request id at the start of the prompt makes every request a miss.
  • Measure the hit rate from the cache read and cache write fields in the usage object.

Common mistakes

  • Putting the current time, a request id or the user's name before the large stable block.
  • Caching content that is used once, which only adds the write premium.
  • Assuming the cache lasts all day when the default lifetime is minutes.
  • Never checking the usage fields, so a broken cache goes unnoticed for weeks.

Check yourself · 2 of 3

On Claude, a 5-minute cache write costs 1.25 times normal input and a read costs 0.1 times. When does caching a prefix clearly lose money?

Choose one answer

Check yourself · 3 of 3

A usage log shows cache_creation_input_tokens of 6,000 on every call and cache_read_input_tokens of 0. What is the most likely cause?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic