CSE Roadmap

Core tier · about 5 hours · 6 parts

Part 4 of 6 · 3 min read

Prompt caching

The provider reuses the start of an identical prompt at a lower price, so the order of your prompt decides your bill.

Prompt caching

Prefix caching

The provider stores the processed form of the start of your prompt and reuses it when a later request begins with exactly the same tokens. Cached input tokens are usually billed at a fraction of the normal rate and may also arrive faster. Some providers charge extra the first time, to write the entry.

Rules that hold in the general case (check your provider for the exact numbers):

  1. Only an identical beginning matches. One different token early on means nothing after it can be reused.
  2. Minimum size. Very short prompts are not cached.
  3. Block alignment. Matching often happens in fixed-size blocks, so the last partial block is not reused.
  4. Entries expire, and using an entry usually refreshes it.

Hence the layout rule: stable first, changing last.

good:  [system instructions][tool definitions][long document][conversation so far][new question]
bad:   [current time][system instructions][tool definitions][long document] ...
AAAWgmp1bWIAAAAeanVtZGMycGEAEQAQgAAAqgA4m3EDYzJwYQAAABZcanVtYgAAAEdqdW1kYzJtYQARABCAAACqADibcQN1cm46YzJwYToyNDAyOWUwZC00YWY2LTQzOWMtOTk3ZC02MjZhZjUzN2JlNTYAAAADl2p1bWIAAAApanVtZGMyYXMAEQAQgAAAqgA4m3EDYzJwYS5hc3NlcnRpb25zAAAAALxqdW1iAAAARGp1bWRjYm9yABEAEIAAAKoAOJtxE2MycGEuaW5ncmVkaWVudC52MwAAAAAYYzJzaJUfT3+b/28aGkumBxL5o6EAAABwY2JvcqNpZGM6Zm9ybWF0bWltYWdlL3N2Zyt4bWxqaW5zdGFuY2VJRHgseG1wOmlpZDphYTAyMWYxZi01ODMwLTQwOWItOGMxZC05ODg5Nzk5YjRmZjJscmVsYXRpb25zaGlwaHBhcmVudE9mAAAB4mp1bWIAAABBanVtZGNib3IAEQAQgAAAqgA4m3ETYzJwYS5hY3Rpb25zLnYyAAAAABhjMnNo6zr5JPYLACkeAGpDqVNN7AAAAZljYm9yomdhY3Rpb25zgqJmYWN0aW9ua2MycGEub3BlbmVkanBhcmFtZXRlcnOha2luZ3JlZGllbnRzgaJjdXJseC1zZWxmI2p1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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPZYQI+9njaimd9RcHJjXV2B58hcHsGah0fE2h04ZDdOIB2GkJXLO7Ke0roF4/Lerm+oNU0p09+lp4xhmkFK08FqlQM=Same content, different order (30 requests, simulated)Timestamp firstinstructions + document (stable)questioncache hit share 0% · total $0.66Stable firstinstructions + document (stable)questioncache hit share 96% · total $0.16Only an identical START of the prompt is reused: one changing token near the top spoils all that follows.
The same words in two orders, 30 simulated requests. Real output of the cache simulation.

In the simulation, putting a timestamp first gives a cache hit share of 0% and a total of $0.66. Putting it last gives 96% and $0.16. Nothing about the content changed.

Things that silently break the cache

A timestamp or request ID near the top; tool definitions listed in a different order each time; a user's name in the system prompt; editing an earlier message in the history. Keep the top of the prompt byte-for-byte identical across calls.

Using the simulation

cache = PrefixCache(min_tokens=1024, block=128, ttl=300)
r = cache.request(tokens, now=12.0)   # r.cached_tokens, r.written_tokens, r.input_tokens
led.add("run", "user", "large", step, Usage(r.input_tokens, 200, r.cached_tokens, r.written_tokens))

Real providers either cache automatically or ask you to mark where the cacheable part ends. Either way, the cost formula is the one above, fed with the cached and written counts from the response.

Check yourself

Your system prompt starts with "Today is Tuesday 14:32". Why is that expensive? (The first tokens change every minute, so no later token can match a stored prefix. Move the time to the end, or to the user message.)

Other ways to cut the bill

  • A smaller model for easy steps (routing). Classify first, then choose, and measure that quality holds.
  • Shorter history: trim or summarise (module 1), at the price of losing detail.
  • Shorter outputs: ask for concise answers, or set a max_tokens cap that fits the task.
  • Batch or off-peak options, where a provider offers them, for work that can wait.
  • Do not call at all: cache your own results for repeated identical questions.

Measure before and after each change using the ledger, never by feel.

What you will use daily at work

Practice What it looks like
Usage logged on every call The cost record is written next to the request log, with run, user, model and prompt version.
Dashboards and alerts Spend per user, per feature and per day, with an alert when a day is far above normal.
Budget caps per run and per user The agent loop checks both before each call.
Cache-friendly prompts A review rule: nothing that changes goes near the top of a prompt.

Draft: confirm token fields, cache pricing, minimum sizes and expiry against your provider's current documentation before relying on them.