Applied AIPrompt CachingCostsLLMsOptimization

Prompt caching: how to cut the cost and latency of your LLM calls

Pablo Alcalde García

In almost every application with LLMs there is a part of the prompt that repeats on every call: the system instructions, the description of the tools, a long document, some examples. Paying for and processing that identical text over and over is a waste. Prompt caching lets the provider reuse the work already done on that fixed part.

How it works, at a high level

The model processes your prompt from start to finish. If the beginning of the prompt is identical to that of a recent call, the provider can reuse its internal processing instead of repeating it. Result: later calls are cheaper and faster on that part.

Two important consequences:

  1. Only the identical prefix counts. If you change something at the start of the prompt, the cache for what comes after is useless.
  2. Variable content must go at the end. Fixed content first, what changes after.

What providers offer (broadly)

The conditions, discounts and durations change over time and by model, so always check the current documentation and pricing. What is stable in concept:

AnthropicOpenAI
ActivationExplicit: you mark blocks with cache_controlAutomatic for long prompts
CacheLasts a few minutes and is refreshed on each use (longer-lived options exist)Kept for a limited time without use
PriceReading from the cache costs considerably less than reprocessing; writing to the cache may cost slightly more than a normal callDiscount on cached input tokens

In both cases there is a minimum prompt size for it to apply.

Example with Anthropic

You mark the block you want to cache (for example, a long system prompt with instructions and examples):

import anthropic

client = anthropic.Anthropic()
MODEL = "..."  # current model on your account

LONG_INSTRUCTIONS = open("support_manual.md", encoding="utf-8").read()

resp = client.messages.create(
    model=MODEL,
    max_tokens=500,
    system=[
        {
            "type": "text",
            "text": LONG_INSTRUCTIONS,
            "cache_control": {"type": "ephemeral"},  # cacheable block
        }
    ],
    messages=[{"role": "user", "content": "How do I return an order?"}],
)

print(resp.content[0].text)
print(resp.usage)  # includes tokens read from and written to the cache

Look at resp.usage: it tells you how many tokens were read from the cache and how many were written. It is the way to check that it works.

With OpenAI

Since it is automatic, there is nothing to mark, but you do need to order the prompt well: fixed content (instructions, examples, document) at the beginning, variable content (the question) at the end. The response includes in usage the number of cached input tokens.

When it really saves money

  • Long system prompts (manuals, policies, schemas).
  • Many questions about the same document (analysing a contract with 20 questions).
  • Agents and tool loops, where each turn resends the same definitions and the history.
  • Extensive few-shot examples.

When it does NOT help

  • Short prompts (below the minimum).
  • Calls spaced far apart in time: the cache expires before it is reused.
  • Prompts that change from the very start on each request (for example, you put the date and time at the beginning).

Typical mistakes that break the cache

  1. Variable data at the start: timestamps, user IDs or personalised greetings in the first paragraph.
  2. Reordering tools or changing their description between calls.
  3. Changing parameters that invalidate the cache depending on the provider.
  4. Not measuring. Without looking at cached token usage, you don’t know whether you are saving.

A prompt structure that caches well

[1] System instructions (fixed)
[2] Tool definitions (fixed, same order)
[3] Large document or context (fixed for the session)   <- cache point
[4] Conversation history (grows)
[5] Current question (variable)                          <- always last

Combine it with other cost levers

Caching is not the only one. Choosing the minimum sufficient model, using a small model for the simple parts, limiting answer length and batching what isn’t urgent all add up. You have the complete picture in how much it costs to implement AI and in the API pricing comparison. If you build agents, it is one of the first things to turn on.

Conclusion

Prompt caching is one of the optimisations with the best effort-to-result ratio: structure your prompt with fixed content first, enable it (or make sure it applies) and verify in token usage that you are really saving.

If you want to audit the LLM spend of your product, let’s talk.

Call me now