Prompt caching for AI agents: calculate the cost per finished task
Diagnose cached reads, cache writes, ordinary input, output, and retries with a provider-aware ledger and a hypothetical break-even worksheet.
Sources are linked in the guide. Read our editorial policy.
On this page 6 sections
Prompt caching can make repeated context cheaper without making an agent task cheap. The useful comparison is the total cost of reaching an accepted result, including cache writes, output, failed attempts, and repair turns—not the advertised input-token rate.
A recent community question about coding-agent cache prices surfaced in Agent Brief’s November 4, 2026 roundup. The original Reddit thread was blocked during research; the roundup is a discussion lead, not evidence for its price examples or conclusions. Here is a worksheet grounded in provider documentation instead.
Build a ledger before changing the prompt
For every model request, record task ID, attempt ID, provider, exact model, timestamp, outcome, and these mutually exclusive billing buckets:
| Bucket | What belongs here | Common accounting error |
|---|---|---|
| Ordinary input | Input neither read from nor written to cache | Charging all input at this rate |
| Cached input | Tokens billed as cache reads | Assuming every repeated token hit |
| Cache writes | Tokens billed at each applicable write lifetime | Adding a write surcharge to a total write rate |
| Output | Billable generated tokens under the model’s rules | Counting only visible final-answer text |
| Other charges | Applicable tool, storage, or service charges | Treating model tokens as the entire invoice |
Use the provider’s usage definitions rather than a universal parser. OpenAI’s current caching guide documents Responses fields input_tokens_details.cached_tokens and, on supported models, cache_write_tokens with lifetime-specific details. Ordinary input is total input minus those non-overlapping read and write counts. Older models differ: some have no additional cache-write charge.
Anthropic’s guide instead defines input_tokens as ordinary uncached input, alongside cache_read_input_tokens and cache_creation_input_tokens. Its creation breakdown distinguishes write lifetimes. Adding those three buckets produces total input; subtracting reads from Anthropic’s input_tokens would be wrong.
Check the model, endpoint, platform, and current rate card before applying either mapping. A provider name alone does not establish one caching policy.
Calculate dollars, not just a cache-hit percentage
For rates expressed per million tokens:
request cost = (U × ordinary rate + R × read rate
+ sum(W_lifetime × write rate_lifetime)
+ O × output rate) / 1,000,000
+ other applicable charges
Sum every request belonging to the task, including retries and delegated agents. Keep unsuccessful tasks in the evaluation cohort:
cost per accepted result = total cohort spend / accepted results
Report acceptance rate alongside that number. Otherwise a model that abandons difficult tasks early can look deceptively economical. Distinguish automatic transport retries from deliberate repair turns; both may create additional billed work, but they suggest different fixes. Where usage is unavailable, mark the amount unresolved and reconcile with billing rather than silently recording zero.
Try the LLM cost and prompt-cache calculator with your own USD-per-million rates. It separates ordinary input, cache reads, cache writes and output, includes billed retries, and compares spend with a same-token uncached baseline. Its request multiplier assumes identical billing buckets per call; for a varied cohort at the same rates, enter summed token buckets with a request count of one. Defaults are hypothetical, and no inputs leave your browser.
A hypothetical break-even worksheet
These are invented arithmetic rates, not any provider’s prices: ordinary input $4, cache writes $6, reads $1, and output $12 per million tokens.
Assume a stable 20,000-token prefix, a changing 2,000-token suffix, and 1,000 output tokens per call. The first call writes the prefix; later calls fully reuse it before expiration. No extra service charges apply.
| Calculation | Cost |
|---|---|
| One uncached call: 22,000 × $4 + 1,000 × $12, divided by a million | $0.100 |
| First cached call: 20,000 × $6 + 2,000 × $4 + 1,000 × $12, divided by a million | $0.140 |
| Each subsequent full hit | $0.040 |
| Three calls without caching | $0.300 |
| One write and two hits | $0.220 |
For the prefix alone, let P be its size and N the number of calls sharing it:
cached prefix cost < uncached prefix cost
P × [write rate + (N - 1) × read rate] < P × N × ordinary rate
N > (write rate - read rate) / (ordinary rate - read rate)
Here, N > 5/3, so two calls beat uncached processing under these assumptions. This is not a universal reuse threshold: expiration, partial hits, multiple write lifetimes, and changing prefixes alter it. If caching requires another repair call, the three-call $0.220 path becomes $0.260 after one extra full-hit call. Further repairs can erase the advantage.
Preserve reusable prefixes without preserving mistakes
Both providers document prefix reuse. Put stable instructions, tool definitions, and shared reference material before changing task data. Avoid injecting a fresh timestamp or random request ID into that stable section. Keep tool ordering and serialization deterministic where your application controls them.
Do not keep obsolete requirements just to maintain a cache hit. A correct prefix rewrite is better than cheap execution against the wrong specification. Likewise, compaction may reduce future context while triggering fresh processing or writes. Compare its one-time reset cost against later savings over the expected remaining turns.
Use the context-engineering diagnostic guide for relevance and context failures. This worksheet answers the separate economic question: which billed work did that change remove or create?
Diagnose the largest dollar bucket first
- Writes dominate: inspect prefix churn, reuse intervals, lifetime selection, and content unlikely to be reused.
- Ordinary input dominates: check cache eligibility, actual matching boundaries, and dynamic content near the beginning.
- Output dominates: caching cannot discount that bucket; investigate unnecessary generation and failed attempts without weakening acceptance.
- Retries dominate: repair the workflow or tool failure before optimizing token rates.
Compare representative tasks with the same acceptance criteria. If you are considering self-hosting, use the Ollama versus vLLM workload worksheet rather than comparing an API token rate with hardware cost alone.
Sources and discussion
- OpenAI prompt caching: model-specific writes, usage accounting, prefix matching, and retention.
- Anthropic prompt caching: read/write buckets, lifetimes, and cache-control behavior.
- November 4 Agent Brief: discovery source for the linked Reddit question; full thread inaccessible. No community price examples are used here.