Skip to main content

Prompt Caching

A technology for optimizing inference in large language models (Anthropic Claude, OpenAI, Google Gemini). It stores precomputed attention vectors of the static part of the input prompt (large books, codebases, or system instructions) in server memory, reducing the cost of repeated queries by 50–90%.

1. Concept Overview & Systemic Problem

Imagine visiting a notary daily with the same thick 200-page contract and asking trivial questions:

  • On Monday: “What does it say about force majeure in clause 12?”
  • On Tuesday: “Who is the signatory from the supplier’s side?”.

If the notary had to reread all 200 pages from the first word every morning and charged you the full fee for each day of reading — you would be bankrupt within a week.

Until mid-2024, language models operated similarly: if you loaded a book or codebase into context, with each new reply, the provider forced you to pay for all 100,000 tokens again and again!

The revolution came with Prompt Caching:

  • You load a large text once.
  • The server stores it in hot memory.
  • For all subsequent questions, you pay only 10% of the cost, and the response appears 3–4 times faster!

Mental model: a massive 90% discount on repeated questions to large documents.

2. Architectural Taxonomy & Mental Model

SCENARIO: You interact with a project codebase (50,000 tokens)
and ask 10 consecutive questions.

WITHOUT CACHING (Classic API):
10 requests * 50,000 tokens = 500,000 input tokens
💸 Cost for the conversation: $1.50 (Slow wait each time)

─────────────────────────────────────────────────────────────

WITH PROMPT CACHING (Anthropic / OpenAI):
Request 1: Cache write (50,000 tokens at full price) = $0.15
Requests 2..10: Read from hot cache at a 90% discount!
              9 * 50,000 * 10% of the price = $0.13
🎉 Total cost: ONLY $0.28 instead of $1.50!
⚡ Response speed increased threefold!

3. Technical Pipeline & Internal Mechanics

To ensure servers recognize that text can be cached, use the pyramid stability principle:

  1. Top (100% unchanging part): Overall system role + extensive company documentation or book. (This block is cached permanently).
  2. Middle (Conditionally unchanging): Dialogue history from the last few messages.
  3. Bottom (Always new): The latest short user question.

4. Production Engineering Scenarios

01. Document Analyzer Implementation

When building a document analyzer, ensure Prompt Caching is enabled. This will significantly reduce your monthly bill while enhancing response speed for users.

02. Code Assistant Development

Incorporate Prompt Caching in your code assistant to optimize performance. This will allow for rapid responses to user queries, improving overall user experience.

03. Support Bot Optimization

For a support bot based on a corporate knowledge base, Prompt Caching is essential. It will not only lower operational costs but also provide instantaneous responses to customer inquiries.

5. Pitfalls, Common Mistakes & Security

Avoid placing any random text at the beginning of the prompt, as this will invalidate the cache. Ensure that the static content is always at the start to maximize caching efficiency. Additionally, monitor cache lifetimes to maintain optimal performance and avoid unnecessary costs.

/ Frequently Asked QuestionsSchema.org FAQPage

FAQ: Prompt Caching

Because the server doesn't need to recompute matrices for your 50-page document with each new question. Its KV Cache is already stored in the ultra-fast GPU memory of the data center. The server spends mere microseconds retrieving the precomputed result, hence the nominal fee.
/ Internal links
All terms