By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
Traffic doubled last month and the bill tripled. Nothing in the prompt changed, so the team starts to suspect the provider's meter.
The cause is usually one of three things. Retries resend the whole prompt. Tool results stay in the window for the rest of a long loop. A cache almost never hits. None of that shows up on a dashboard built around request count.
A request is billed in parts, not as one number.
| Line | What makes it grow | How to reduce it |
|---|---|---|
| Input tokens | System prompt, tool schemas, history, tool results | Send less, or cache what repeats |
| Cache write tokens | A prefix you asked the provider to store | Cache once per prefix, not once per request |
| Cache read tokens | The same prefix reused from the cache | Raise the hit rate and keep the prefix stable |
| Output tokens | The answer, plus any thinking tokens | Bound the output and ask for the shortest correct form |
Published price per token differs by line, and output usually costs more per token than input. Do not carry a number in your head. Read the current pricing page and put every rate in one configuration file.
The cache lines surprise people. A cache write costs more than a plain input token, so caching a prefix you read once loses money. A cache read is much cheaper, which is the whole point.
# Relative units per token, not a price list. Replace with published rates.
RATE = {"input": 1.0, "output": 5.0, "cache_write": 1.25, "cache_read": 0.1}
def cost_units(usage: dict) -> float:
return (
usage.get("input_tokens", 0) * RATE["input"]
+ usage.get("output_tokens", 0) * RATE["output"]
+ usage.get("cache_creation_input_tokens", 0) * RATE["cache_write"]
+ usage.get("cache_read_input_tokens", 0) * RATE["cache_read"]
)
def usage_row(response: dict, feature: str, tenant: str) -> dict:
usage = response["usage"]
return {
"feature": feature,
"tenant": tenant,
"input": usage.get("input_tokens", 0),
"output": usage.get("output_tokens", 0),
"cache_read": usage.get("cache_read_input_tokens", 0),
"cost_units": round(cost_units(usage), 2),
}
cold = {"usage": {"input_tokens": 40_500, "output_tokens": 300}}
warm = {"usage": {"input_tokens": 500, "output_tokens": 300,
"cache_read_input_tokens": 40_000}}
print(usage_row(cold, "triage", "acme"))
print(usage_row(warm, "triage", "acme"))Prompt caching stores a prefix of the request on the provider side. A later request with the same prefix reads it instead of processing it again. Reads are cheaper and faster than writes, and the prefix must match exactly to hit.
That gives you one design rule: put what never changes at the front and what changes at the end. The provider walks the prefix in order, so a change early in the request discards everything after it.
A cache also expires. Each read refreshes its lifetime, so a busy prefix stays warm and a quiet one goes cold. Check the current lifetimes and syntax on the caching page before you build around them.
A cache checkpoint is the breakpoint you mark in the request. The provider stores everything up to that point. You can place more than one, and the current limit is documented rather than fixed forever.
Two rules prevent most cache bugs. Put the checkpoint after the content you reuse, not before it. And keep the number of checkpoints small, because each one adds a segment that must stay stable.
Measure the prefix: count the tokens in the system prompt and the tool schemas.
Find the last block that is identical across requests, and mark the checkpoint there.
Move anything variable after that block, including dates, ids and per-user text.
Log the cache read and write counts per request, then compare them a day later.
Edit the prompt on purpose and watch the hit rate, so a regression is visible.
Then watch the hit rate. A cache that hits nine times out of ten is a design win. One that hits a tenth of the time is a write on almost every request, and costs more than no cache.
A retry resends the whole prompt. If validation fails three times, you pay for four calls and keep one answer. Cap retries, and treat a retried call as its own cost line.
A tool result is appended to the conversation, so every later turn reads it again. A huge dump in turn two is billed again in turns three through nine. Trim tool results where they enter the loop, keep the fields you need and summarise the rest.
Then add the part that invoices miss: the cost of a wrong answer. A wrong tag costs a minute of rework. A wrong refund costs money and trust. The cheapest system is not the one with the lowest cost per call; it is the one with the lowest cost per correct outcome.
Model one endpoint before you change it. A spreadsheet is enough.
stop_reason for every request for a day.Key takeaways
Every request bills four lines: input, cache write, cache read and output.
Cache a stable prefix and put everything variable after the checkpoint.
A cache that rarely hits costs more than no cache.
Retries and tool results are cost lines; cap the first and trim the second.
Optimise cost per correct outcome, not cost per call.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.