Skip to content
mentoor

Topic 2 of 5 · 4 min read

Ordering requests for cache hits

Goal: Reorder a request so its stable part forms a cacheable prefix and place the cache breakpoints correctly.

Order every request from most stable to least stable, and put a cache breakpoint at the end of each stable block, so the longest possible prefix is identical from call to call. Because the cache matches only a prefix, the position of each piece of text decides whether it can be cached.

Check yourself · 1 of 3

Which part of a request is best placed first, so it can form part of the cached prefix?

Choose one answer

Why it matters

A prompt has parts that never change (tool definitions, rules), parts that change rarely (a policy document, a repo summary), and parts that change every call (the current time, the user's message, retrieved snippets). If a changing part sits early, everything after it is lost to the cache, however stable it is.

How to do it

  1. List the parts of your request and mark how often each changes: never, per deploy, per session, per request.
  2. Sort from stable to volatile. A good order is tools, then system prompt, then reference documents, then conversation history, then the new user message and any per-request data.
  3. Move volatile data down. The date, a request id or the user's name belongs near the end, in the user turn, not in the system prompt.
  4. Place breakpoints. On Claude you add a cache_control marker to the last block you want cached. Put one after the stable system and documents. In a growing conversation, a marker on the last message lets each turn reuse the one before. OpenAI and implicit Gemini caching need no markers, but the same ordering rule applies.
  5. Keep stable parts byte-identical. Same wording, same whitespace, same JSON key order, same tool order.

Before and after

Before, a support bot's system prompt starts with the date:

system = f"Today is {today}. You are a support agent for Acme. {POLICY_MANUAL}"
messages = [{"role": "user", "content": question}]

The date changes daily, and every prompt after it differs, including the 15,000-token manual. After:

system = [
    {"type": "text", "text": f"You are a support agent for Acme. {POLICY_MANUAL}",
     "cache_control": {"type": "ephemeral"}},
]
messages = [{"role": "user", "content": f"Today is {today}.\n\n{question}"}]

The manual is now an identical prefix, with a breakpoint at its end. Only the short user turn changes.

What goes wrong

  • A breakpoint placed on a block that contains changing text never produces a hit.
  • Too many breakpoints. Providers cap how many you can set, and extra ones add nothing when the prefix is the same.
  • Putting retrieved documents before the system prompt, so retrieval results break the whole cache.

Examples

Reorder a RAG request

Before: [retrieved passages] [system rules] [tools] [question]
After:  [tools] [system rules] [question + retrieved passages]

Retrieved passages differ per question, so they go last. Tools and rules now form a prefix that every request shares.

Breakpoints in a growing chat

messages[-1]["content"][-1]["cache_control"] = {"type": "ephemeral"}

Marking the last block each turn lets the next turn find the previous turn's cached prefix, so only the newest messages are processed fresh.

Key points

  • Order content from most stable to most volatile: tools, system prompt, documents, history, then the new user input.
  • Per-request data such as the date or a user name belongs in the final user turn, not in the system prompt.
  • A breakpoint goes at the end of a stable block, and the text before it must be identical each call.
  • Identical means byte for byte: wording, whitespace, JSON key order and tool order all count.

Common mistakes

  • Starting the system prompt with the current date or time.
  • Putting retrieved or per-user content before the long stable instructions.
  • Marking a breakpoint after a block that changes every call, which wastes a write.
  • Rebuilding the tool list or JSON in a different order on each request.

Check yourself · 2 of 3

A system prompt begins 'Current time: 14:03:22.' followed by 12,000 tokens of fixed rules. Why does it never get cache hits, and what is the fix?

Choose one answer

Check yourself · 3 of 3

Which request layout lets the next turn of a chat reuse the most cached tokens?

Choose one answer

Practice items, not counted towards your mastery.

Start here