Logo
All posts

What is Prompt Caching in LLMs?

Your AI application talks to a large language model through an API, and the API bills you by the token.

Gaurav Sen/Updated /10 min read

Most of a prompt is the same from one call to the next. The tool definitions and the system instructions rarely change, and a user query is often fixed text with a few blanks.

Yet the model processes the whole prompt again on every call, and you pay for every token of it. Anthropic's API offers a way to pay less for the part that repeats.

That way is prompt caching. The Claude Code team wrote a post about it called "Prompt caching is everything".

In this post, we will learn what prompt caching is and how to lay out a prompt so the cache can work. By the end of it, you will be able to pay the cached price for the parts of a prompt that never change.

A survey sent to a model

Let's take an example of a prompt that repeats. Say your server sends survey responses to the model, one API call per person. The survey has six questions.

Each call carries the tool definitions, the system instructions and the user query. Here the user query is the survey.

The survey as one API call carries it, under the tool definitions and the system prompt. First each answer (orange) sits in its blank, under its own question. Then the six answers leave the form and stand in one strip under the last question.
The survey as one API call carries it, under the tool definitions and the system prompt. First each answer (orange) sits in its blank, under its own question. Then the six answers leave the form and stand in one strip under the last question.

The six questions are the same in every call, and only the answers change. Yet the model reads all six questions again for every person.

What the model does with a prompt

Why does a long prompt cost so much? It's the way a model reads it.

A language model reads a prompt through self-attention. Self-attention relates the different positions of one sequence to compute a representation of it.

So every token of the survey gets compared with every other token of the survey.

Every part of the prompt gets a score against every other part. The numbers are only there to show the shape. The green block is what the API processed on an earlier call and reuses, and the orange row and column, named User Params, are all the model computes for a new person.
Every part of the prompt gets a score against every other part. The numbers are only there to show the shape. The green block is what the API processed on an earlier call and reuses, and the orange row and column, named User Params, are all the model computes for a new person.

The Transformer paper gives the cost of one self-attention layer as O(n²·d). Here n is the length of the sequence. The size of each token's representation is d.

The cost grows with the square of the length. Double the length of the prompt and that layer does four times the work.

Prompt caching

What if the model didn't have to read the same questions twice?

An API can keep what the model processed in an earlier call. It then fully processes only what's new.

Anthropic bills an input token read from this cache at 0.1 times the base input price. A cached token is one the API has already processed in an earlier request.

Reuse of a cached token has one condition. The API matches the start of each request, called the prefix, against content it recently processed.

The match is exact. A change anywhere in the prefix recomputes everything after it.

Anthropic's API builds the prefix in a fixed order: tools, then system, then messages. The tool definitions and the system instructions rarely change, so they match.

Reusing the processed start of a prompt this way is called *prompt caching*.

1. Fill in the blanks

So where do the answers go in the prompt? The usual way is a string replace.

The server writes each answer into its blank, in the middle of the text. The prompt reads like a form a person filled in.

Look at where that leaves the prefix. The first answer, the email, is written right under the first question.

The prefix ends at that email. Five of the six questions come after it, so the model recomputes them for every person. A string replace pays full price for most of the survey.

2. Answers at the bottom

What if the text of the survey never changed? Keep the six questions fixed, and mention the answers at the bottom of the prompt, after the last question.

Now the prefix runs through the tools, the system instructions and all six questions. Only the answers are new work.

Anthropic's documentation asks for the same layout. It says to place static content at the beginning of your prompt.

That layout has a price and a clock. A write to the 5-minute cache costs 1.25 times the base input price.

The lifetime is 5 minutes by default. It's refreshed each time the cached content is used.

A very short prompt isn't cached at all. The minimum is between 512 and 4,096 tokens, depending on the model.

Don't change models mid-session

You might want a cheaper model for an easy question. But a cache belongs to one model.

In the words of the Claude Code team, prompt caches are unique to models. Send the next survey to a different model and nothing matches.

The Claude Code team gave a bigger example of this in April 2026. Say you are 100k tokens into a conversation with Opus, and you have a question that is fairly easy to answer.

Switching to Haiku for that question costs more than letting Opus answer. Haiku doesn't have a cache for those tokens and has to rebuild it.

Claude Code does let you switch. The next request then reads the entire conversation with no cache hits.

To avoid those misses, the team's advice is a subagent. Opus prepares a hand-off message for the other model on the task it needs done.

Static at the top, answers at the bottom

We saw where the prefix ends, and what it costs. Whenever your server sends a request to a large language model, make most of the prompt static. Place the static parts at the top.

A server sends one request to an LLM. First the request has three parts: tools, system prompt and user query. Then the parts that stay the same (green) are on top, and the parameters (orange) come last.
A server sends one request to an LLM. First the request has three parts: tools, system prompt and user query. Then the parts that stay the same (green) are on top, and the parameters (orange) come last.

For the survey, we recommend answers at the bottom, instead of a string replace. The prompt says the same thing to the model.

The bill for that prompt is different. While responses keep arriving within 5 minutes, every call after the first reads the six questions at 0.1 times the base input price.

Summary

  1. Anthropic bills a token read from its prompt cache at 0.1 times the base input price.
  2. One self-attention layer costs O(n²·d) for a sequence of n tokens.
  3. With caching, the API reuses what it already processed.
  4. The prefix has to be identical. A change anywhere in it recomputes everything after it.
  5. Anthropic's API builds the prefix in the order tools, system, messages.
  6. A string replace ends the prefix at the first answer. Answers at the bottom keep every question inside it.
  7. A write to the 5-minute cache costs 1.25 times the base input price.
  8. The cache lives 5 minutes, and each use refreshes it.
  9. Prompt caches are unique to a model.

Sources

  1. Lessons from building Claude Code: Prompt caching is everything, Thariq Shihipar, 30 Apr 2026.
  2. Prompt caching, Claude API documentation.
  3. How Claude Code uses prompt caching, Claude Code documentation.
  4. Attention Is All You Need, Ashish Vaswani and others, 2017.
WhatsApp