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
- Build the prompt
- Cache lookup
- Count tokens
- Answer
Press Play to watch it run, or step through with ← →. Then switch to Practice and run it yourself.
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 words
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
- 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.
- 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.
- 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.
- 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.
- 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 deeperThe three vendors side by side
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.
| Anthropic | OpenAI | Google (Gemini API) | |
|---|---|---|---|
| How it starts | You 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 own | Automatic on supported models | Implicit caching is on by default on current Gemini models |
| Smallest prompt that can be cached | 512 to 4,096 tokens, depending on the model | 1,024 tokens on the newest models | 2,048 or 4,096 tokens, depending on the model |
| Price of cached input | Reads 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 lives | 5 minutes by default, refreshed each time it is used. 1 hour as an option | From minutes to hours, depending on the model and the retention setting | The page advises sending requests with a similar prefix within a short time |
| Where to see a hit | usage.cache_read_input_tokens | usage.input_tokens_details.cached_tokens | usage.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 deeperWhat counts as "the beginning"
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 deeperA worked example of the saving
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 lifetime | Rulebook cost, in full-price input tokens |
|---|---|
| No caching | 100 × 6,000 = 600,000 |
| With caching | 1 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 deeperWhen caching does not help
- 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 deeperWords to know
| Word | Meaning in simple words |
|---|---|
| Input tokens | Everything you send to the model in one call, measured in tokens |
| Output tokens | What the model writes back, measured in tokens |
| Prefix | The beginning of a prompt, counted from the very first token |
| Prompt caching | Reusing the work done on a prefix when a new prompt starts with exactly the same tokens |
| Cache hit and miss | A hit means the saved prefix was reused. A miss means everything was processed again |
| Usage report | The part of each response that says how many tokens were counted, and how many were cached |
Check your understanding
Sources: the official documentation
This lesson is based on the official documentation. Check it for the latest details.
Something wrong or unclear in this lesson? Tell us, it gets fixed.