Skip to Content

Tokens, cost and prompt caching

You pay for every token on every call, even for text that never changes. Watch the same prompt miss the cache every time, then hit it after three lines move to the end, and learn to read the usage report that proves it.

11 minFree

Tokens, cost and prompt caching
  1. Build the prompt
  2. Cache lookup
  3. Count tokens
  4. Answer
FIG. PE-COST-AND-CACHING / 06 PARTSEnquiry deskyour appRulebookfixed, 6,000 tokensQuestionchanges each callModel APIany vendorPrompt cachesaved beginningsUsage reporttokens you pay for

Press Play to watch it run, or step through with ← →. Then switch to Practice and run it yourself.

00/10
Time now: {{time}}
Passenger: {{passenger_name}}

You are the enquiry desk assistant of a railway booking office.
Answer only from the rulebook below. If the rulebook does not
cover the question, say so.

<rulebook>
(Refund and cancellation rules. About 6,000 tokens.
 The same text on every call.)
</rulebook>

Question: {{question}}

Usage report: what you paid for

No calls yet. Press Play.

Background: the idea in plain words2 min read

A model does not read words. It reads tokens (small pieces of words). Google's docs say that for Gemini models a token is about 4 characters, and 100 tokens are about 60 to 80 English words. Vendors use tokens to measure your prompt, and to measure the bill.

Every call has two token counts. Input tokens are everything you send: the instructions, any documents, the tool definitions, the chat history and the question. Output tokens are what the model writes back. You pay for both, on every call.

The words "on every call" matter. A model remembers nothing between calls (see Context windows and prompts). If your prompt carries a long rulebook, the full rulebook is sent, counted and paid for each time, even though not one word of it changed.

Prompt caching is the vendors' answer to this. A cache is a store of work already done. When a new prompt begins with exactly the same tokens as a recent one, the vendor reuses the work it did on that beginning. The shared beginning is called the prefix. Cached input tokens cost less, and the answer starts sooner.

There is one catch, and it is the heart of this lesson. The match is counted from the first token, and it stops at the first difference. So the order of your prompt decides your bill. Text that never changes must come first. Text that changes on every call must come last.

The running example is the enquiry desk of a railway booking office. Its prompt holds a refund rulebook of about 6,000 tokens. Each passenger adds a few short lines: the time, a name and a question.

Press Play to watch two calls miss the cache. Then switch Try it to After: fixed text first, changing lines last, and then to the edge case.

What to notice

  1. You pay for the whole prompt on every call. The rulebook was the same for Ravi and for Asha, and it was counted in full for both. Long fixed text is the biggest part of most bills.
  2. The cache matches a beginning, not a meaning. It compares tokens from the start of the prompt. One changed character near the top means nothing after it can be reused.
  3. Order is the fix. Both versions of the prompt had the same words. Moving the time, the name and the question to the end turned 0 cached tokens into 6,000.
  4. The usage report is the proof. Do not guess. Each vendor returns the number of cached input tokens with every response. Zero on a second, similar call means the cache was not used.
  5. Output tokens are never cached. The answer is written fresh each time. Caching lowers the cost of what you send, not of what comes back.
Go deeper

The three vendors side by side

2 min read

All three vendors document the same idea: a stable prefix is reused. The details differ, and they change over time. The table below holds only what the official pages said when this lesson was written (November 2026). Check the linked sources before you plan a budget.

AnthropicOpenAIGoogle (Gemini API)
How it startsYou add a cache_control field to the request: one field at the top level for automatic placement, or up to 4 marked points of your ownAutomatic on supported modelsImplicit caching is on by default on current Gemini models
Smallest prompt that can be cached512 to 4,096 tokens, depending on the model1,024 tokens on the newest models2,048 or 4,096 tokens, depending on the model
Price of cached inputReads cost 0.1 times the normal input price on most models. Writes cost 1.25 times (5-minute lifetime) or 2 times (1-hour lifetime)Cached tokens are "discounted up to 95%"Savings are passed on automatically when a request hits the cache
How long an entry lives5 minutes by default, refreshed each time it is used. 1 hour as an optionFrom minutes to hours, depending on the model and the retention settingThe page advises sending requests with a similar prefix within a short time
Where to see a hitusage.cache_read_input_tokensusage.input_tokens_details.cached_tokensusage.total_cached_tokens

The advice on order is the same everywhere. Anthropic: place static content (tool definitions, system instructions, context, examples) at the beginning of your prompt. OpenAI: put stable instructions and shared reference material first, and dynamic content such as timestamps or user details at the end. Google: try putting large and common contents at the beginning of your prompt.

This order is good for quality too Fixed text first and the question last is also the order that vendor guides recommend for long documents. See Context, delimiters and order. You do not have to trade quality for cost here.

Go deeper

What counts as "the beginning"

1 min read

The prefix is more than your prompt text. On Anthropic, the docs give the order as tools, then system, then messages. So a change to a tool definition, even to one description, makes everything after it a miss. OpenAI's docs say the cached prefix can include developer messages, tool definitions and conversation history. Google's docs say system instructions and tools are counted as input tokens.

Things that quietly break the prefix:

  • A time stamp or a date at the top of the system prompt.
  • The user's name inside the first line ("You are helping Ravi").
  • Tools listed in a different order on each call.
  • A small wording fix to the fixed part. This is fine, but expect one miss after each edit, as the third scenario shows.

This is one more reason to build prompts as templates with the slots at the end. See Templates, variables and versions.

Go deeper

A worked example of the saving

1 min read

These numbers use Anthropic's published multipliers and the 6,000-token rulebook. The price itself is left out, because it differs by model. Count the cost in "full-price input tokens".

100 calls within the cache lifetimeRulebook cost, in full-price input tokens
No caching100 × 6,000 = 600,000
With caching1 write: 6,000 × 1.25 = 7,500. Then 99 reads: 99 × 6,000 × 0.1 = 59,400. Total 66,900

That is about 11% of the uncached cost for the rulebook part. The 40 changing tokens and all the output tokens cost the same in both rows.

Go deeper

When caching does not help

1 min read
  • The prompt is short. Below the vendor's minimum, nothing is cached. Anthropic's docs note that no error is returned. You just see 0 cached tokens.
  • Calls are rare. If the next call comes after the entry has expired, it is a miss again. On Anthropic you also paid the higher write price for nothing.
  • The fixed part is not really fixed. If every call carries a different document, there is no shared prefix to reuse.
  • The cost is in the output. A short prompt that asks for a long essay is expensive because of output tokens. Caching does not touch those. Ask for a shorter answer instead.

Caching is also not the first thing to try. First ask if the prompt needs all that text. Fewer tokens are cheaper than cached tokens. To see how big a prompt is before you send it, count its tokens. The Gemini API has a count_tokens method for this. For how text becomes tokens, see Tokens.

Go deeper

Words to know

1 min read
WordMeaning in simple words
Input tokensEverything you send to the model in one call, measured in tokens
Output tokensWhat the model writes back, measured in tokens
PrefixThe beginning of a prompt, counted from the very first token
Prompt cachingReusing the work done on a prefix when a new prompt starts with exactly the same tokens
Cache hit and missA hit means the saved prefix was reused. A miss means everything was processed again
Usage reportThe part of each response that says how many tokens were counted, and how many were cached

Check your understanding

1. Your system prompt starts with “Current time: 10:42:07” and then has 6,000 tokens of fixed rules. What will the usage report show for cached tokens?
2. Two prompts have the same 6,000-token beginning and different questions at the end. Is that a cache hit?
3. How do you check that prompt caching is working in your app?
Sources: the official documentation4 links

Something wrong or unclear in this lesson? Tell us, it gets fixed.