Tutorial
Generative AI
8 min reading

How to Calculate Cached LLM Input Cost From API Usage Fields

A stream of tokens splits into three channels for misses, reads and writes that together drive one input cost gauge, beside a disconnected request counter.

Summarize this article with:

summary

A practical guide to cached LLM input accounting. Normalize usage fields into misses, cache reads and cache writes split by cache lifetime. Map each provider from its documentation: Gemini, for example, can report written context as cached_tokens with no separate write count. Price each request at the rates that apply to it, and report effective input price beside miss-token share. A worked example uses Claude Opus 5.5 rates to choose a cache lifetime.

A high request hit rate can still produce an expensive input bill. Take an illustrative workload: 90 cache-hit requests of 1,000 tokens and 10 misses of 20,000 tokens. The request hit rate is 90%, but cache reads are only 90,000 of 290,000 input tokens, about 31%.

Request hit rate versus token hit rate

Percent

  • Requests that hit the cache
  • Input tokens read from cache

Source: illustrative arithmetic from 90 requests of 1,000 tokens and 10 requests of 20,000 tokens.

The chart shows why request counts can't give you cost when request sizes differ. You need token totals, split three ways where the provider allows it, because many usage objects report cache writes on their own. Anthropic returns cache_creation_input_tokens. Eden AI's Chat Completions responses return cache_write_tokens. A calculator that bills every non-read token at the base rate prices those writes as misses.

This guide maps the usage fields documented as of December 3, 2026, gives you a configurable Python calculator, and applies it to one cache-lifetime decision.

Miss-token share is the number to watch

Miss-token share

Miss-token share = miss tokens / (miss tokens + read tokens + write tokens). Miss tokens are billed at the base rate, read tokens come from the cache, and write tokens create cache entries. Worked line: 100,000 / (100,000 + 850,000 + 50,000) = 10%.

A small miss share can still drive most input spending when reads are heavily discounted. The scenarios below leave writes out to isolate that effect. They are arithmetic from the read multipliers shown, not rates that apply to every model.

Miss-token shareRead rate vs. baseMisses' share of input cost
5%0.05×51.3%
10%0.05×69.0%
5%0.10×34.5%

Source: arithmetic from the multipliers shown. Anthropic's pricing documentation lists 0.05× and 0.1× cache-read multipliers for different models. Checked December 3, 2026.

Misses' share of input cost at a 90% token hit rate

Share of input cost paid for misses (reads make the rest)

  • Read rate 0.05x
  • Read rate 0.10x

Source: arithmetic from the read-rate multipliers shown, with no write tokens. Anthropic's pricing documentation lists 0.05x and 0.1x cache-read multipliers. Checked December 4, 2026.

At a 0.05× read rate, the 10% of tokens that miss the cache make 69% of input cost. Even at 0.10×, misses are more than half of it.

Calculate cost from misses, reads and writes

Effective input price

Effective input price = (miss tokens × base rate + read tokens × read rate + write tokens × write rate) / total input tokens. Rates are prices per million tokens, write tokens use the write rate of their cache lifetime, and total input tokens are misses, reads and writes combined. Worked line at Claude Opus 5.5's rates: (100,000 × $4 + 850,000 × $0.20 + 50,000 × $5) / 1,000,000 = $0.82 per 1M input tokens.

Effective input price by token hit rate

Share of the base input rate

  • Read rate 0.05x
  • Read rate 0.10x
  • 0% hit rate
  • 25% hit rate
  • 50% hit rate
  • 75% hit rate
  • 100% hit rate

Source: arithmetic from the read-rate multipliers shown, with no write tokens. Anthropic's pricing documentation lists 0.05x and 0.1x cache-read multipliers. Checked December 4, 2026.

Effective price falls as more input tokens become cache reads. The read rate sets the lowest possible price, while every miss pulls the result back toward the base rate.

Price each request at the rates that apply to it, then add the costs. Rates can change from one request to the next: Claude Haiku 5.5 is priced by prompt length, and a request whose prompt is over 100,000 tokens pays higher prices. Each write rate replaces the base rate for its tokens. OpenAI's prompt-caching guide says it plainly: cache-write pricing is not an additive fee.

From API responses to an effective input price

Normalize usage

Turn each response into misses, reads and writes per cache lifetime.

Price each request

Apply the dated rates for its model, lifetime and prompt length.

Aggregate

Sum costs and token categories over the reporting window.

Report

Return total cost, effective price and miss-token share.

Normalize the usage fields before calculating cost

How one response's input tokens split

All input tokens

The prompt of one response.

Reads

Matched an existing cache entry: billed at the read rate.

Writes

Stored as a new cache entry: billed at its lifetime's write rate.

Misses

Processed again: billed at the base input rate.

CategoryAnthropic MessagesEden AI Chat Completions
Missesusage.input_tokensusage.prompt_tokens minus reads and writes, if it includes both
Readsusage.cache_read_input_tokensusage.prompt_tokens_details.cached_tokens
Writesusage.cache_creation_input_tokensusage.prompt_tokens_details.cache_write_tokens

Sources: Anthropic prompt-caching documentation and Eden AI prompt-caching documentation. Checked December 4, 2026.

Anthropic's cache_creation_input_tokens is the sum of its 5-minute and 1-hour writes, which the cache_creation object reports separately. The two lifetimes are priced differently, 1.25× and 2× the base input price, so price each from that object.

The Eden AI prompt-caching page lists usage.prompt_tokens_details.cached_tokens and usage.prompt_tokens_details.cache_write_tokens for Chat Completions. The mapping depends on the provider. Eden AI's documentation says Gemini can report the explicitly cached context as cached_tokens on the first marked request, and may not expose a separate cache-write count. On such a provider, a read count can include tokens that were written, so set each provider's mapping from its documentation. Before you derive misses from a prompt total, confirm whether that total includes reads and writes.

The two-bucket calculation misprices cache writes

A two-bucket calculator treats every token that was not read from cache as an ordinary miss, which loses the separate write rate. Take an illustrative response with 10,000 total input tokens, 6,000 read tokens and 1,000 write tokens. The two-bucket method reports 4,000 misses. Three-category accounting reports 3,000 misses and 1,000 writes.

At hypothetical rates of $2 base, $0.20 read and $2.50 write per million tokens, the two-bucket calculation gives $0.0092. Three-category accounting gives $0.0097, so the old method understates this example by $0.0005.

Turn responses into a production cost report

Python

def valid_counts(*counts):
    return min(counts) >= 0 and sum(counts) > 0

def cost_report(miss_tokens, read_tokens, write_tokens,
                base_rate, read_rate, write_rate):
    # Rates in USD per 1M tokens, dated by you.
    counts = (miss_tokens, read_tokens, write_tokens)
    if not valid_counts(*counts):
        raise ValueError("token totals must be non-negative, and not all zero")
    total = sum(counts)
    cost = (miss_tokens * base_rate + read_tokens * read_rate
            + write_tokens * write_rate) / 1_000_000
    return {"input_cost": cost,
            "effective_price_per_million": cost * 1_000_000 / total,
            "miss_token_share": miss_tokens / total}

print(cost_report(3000, 6000, 1000, 2, 0.2, 2.5))

Worked example: choose a cache lifetime

A longer cache lifetime only helps when its higher write price is recovered through later reads. Anthropic's pricing for Claude Opus 5.5 on December 3, 2026 is $4 / $5 / $8 / $0.20 per 1M base input / 5-minute write / 1-hour write / read tokens. Anthropic says the 5-minute write saves money after one cache read, and the 1-hour write after two.

Claude Opus 5.5: cost of 1M reused-prefix tokens

USD per 1M prefix tokens, over the whole sequence

  • With caching
  • Without caching
  • 5-minute write + 1 read
  • 1-hour write + 2 reads
  • Entry expired, rewritten each time

Source: Anthropic pricing documentation, Claude Opus 5.5 rates. Arithmetic from those rates, checked December 3, 2026.

Reuse timing decides which line you land on. Anthropic recommends the 5-minute cache for prompts sent more often than every 5 minutes, because each use refreshes it at no extra charge. It suggests the 1-hour cache for prompts reused less often than every 5 minutes but more often than every hour.

Feed Eden AI usage into the same calculator

Claude Opus 5.5 is available on Eden AI as anthropic/claude-opus-5-5. On December 4, 2026, that endpoint listed $4 per 1M input tokens and $0.20 per 1M cached input tokens.

Claude Haiku 5.5 is also available on Eden AI, called with the same API key.

Eden AI's prompt-caching documentation notes that pricing belongs to a concrete provider endpoint, so providers serving the same model name can differ. Set your report's rates per endpoint, not per model name. If you send the bare name claude-opus-5.5, Eden AI uses sticky routing to keep conversations on the provider holding their cache.

Through Chat Completions, Eden AI automatically marks the system prompt and trailing turn of cache-capable Claude models with cache boundaries, with a five-minute default retention. For the one-hour lifetime, Eden AI's documentation points to its Anthropic-compatible Messages endpoint with cache_control, which returns Anthropic's native usage fields. Pass the totals from either dialect to the calculator.

Python

import requests

MODEL = "anthropic/claude-opus-5-5"
# Set per provider from its documentation: Eden AI's provider
# table lists cache_write_tokens for Anthropic.
REPORTS_WRITES = True

response = requests.post(
    "https://api.edenai.run/v3/chat/completions",
    headers={"Authorization": "Bearer YOUR_EDENAI_API_KEY"},
    json={"model": MODEL, "max_tokens": 256, "messages": [
        {"role": "system", "content": "Long, stable instructions and reference material..."},
        {"role": "user", "content": "The changing question goes last."},
    ]},
    timeout=120,
)
response.raise_for_status()  # stop on HTTP errors
usage = response.json().get("usage") or {}
details = usage.get("prompt_tokens_details") or {}
reads = details.get("cached_tokens") or 0
if REPORTS_WRITES:
    writes = details.get("cache_write_tokens") or 0
else:
    writes = None  # a zero says nothing here: report writes apart
print({"prompt_tokens": usage.get("prompt_tokens"), "reads": reads, "writes": writes})

Each Eden AI response also carries a cost field that already accounts for the provider's cache-read and cache-creation prices. It gives you a per-request total to reconcile with your report once output tokens are added. Eden AI adds no markup to provider prices and charges a 5.5% platform fee at checkout, so keep that fee as its own line.

What this calculation leaves outside the input price

The formula covers only the input categories you give it. Output tokens belong on a separate line, and so does any surcharge or discount on the provider's current pricing page. Add them up afterwards if you need an all-in request total. Our guide to how LLM billing works covers the output side.

For broader cost control, pair this report with token-usage monitoring and the prompt-caching cost playbook, which compares caching across model families.

The takeaway

Cached-input reporting checklist

  • Aggregate tokens, not requests. Request hit rate ignores how large each prompt is.
  • Keep misses, reads and writes apart. Split writes by cache lifetime, because each lifetime has its own rate.
  • Price each request, then add. Store each rate with its model, endpoint, lifetime, prompt-length tier and date.
  • Report two metrics together. Show effective input price beside miss-token share.

FAQ

Why is request hit rate not enough to calculate cost?
Requests carry very different numbers of tokens, so a few large misses can outweigh many small hits. Cost depends on aggregated token totals for misses, reads and writes.
Are cache-write charges added to the base input rate?
No. OpenAI's prompt-caching guide says cache-write pricing is not an additive fee: each input token is billed at the uncached, cached or cache-write rate.
What should a cost calculator do when cache_write_tokens is missing?
Eden AI's prompt-caching documentation says fields that the provider does not report can be absent or zero, and that Gemini may not expose a separate cache-write count. Map each provider's fields from its documentation.
How many reads does a cache write need to pay off?
Anthropic says a 5-minute cache write saves money after one cache read, and a 1-hour write after two. Prices vary by model and provider, so check the pricing page for yours.
Does effective input price include output tokens?
No. It covers misses, reads and writes only. Report output tokens and other documented pricing modifiers on separate lines.
Why can the miss share stay high after I add cache breakpoints?
Anthropic measures cache lifetime from the start of the request, so a response that streams for 4 minutes leaves about 1 minute for the next request to reuse the prefix. Anthropic also warns that a breakpoint on a block that changes every request, such as one with a timestamp, means you pay for a fresh cache write on every request and never get a read.

Further reading

Similar articles

Tutorial
Generative AI
How to Add Automatic Fallbacks Between LLM Providers in Production?
12/4/2026
·
Written byClément Moreau
A key inside a glass project vault secured by two identity badges, with a side path leading to a backup model tower.
Tutorial
Generative AI
Which Google Account Should Own Your Gemini API Key?
12/1/2026
·
Written byClément Moreau
An approved package pipe bypasses an agent sandbox wall before an external policy gate blocks its route to other systems.
Tutorial
Generative AI
How OpenAI's Test Agents Got Out, and How to Contain Yours
11/30/2026
·
Written byClément Moreau
let’s start

Start building with Eden AI

A single interface to integrate the best AI technologies into your products.