4/16
Part 2 of 6 · Cache the shared promptLearn and do

A hit reads the prefix for a twentieth of the price, and a miss writes it for a quarter more

With prompt caching, the provider keeps the computed keys and values of a prompt's opening, the same prefix cache the latency lesson ran on its own GPU. Anthropic charges a 5-minute cache write at 1.25 times the input price and a 1-hour write at 2 times. Reading the cache, a hit, costs 0.1 times the input price in general, and 0.05 times on Sonnet 5.5 and Opus 5.5, which is the $0.10 in the price dict.

def cached_cost(price: dict, shared: int, own: int, output_tokens: int, hit: bool) -> float:
    """One request whose first shared input tokens are read from the cache (a hit) or written to it."""
    shared_price = price["read"] if hit else price["write_5m"]
    return (shared * shared_price + own * price["input"] + output_tokens * price["output"]) / 1e6


print(f"no cache ${request_cost(SONNET, 1860, 250):.5f}")
print(f"a miss   ${cached_cost(SONNET, 1800, 60, 250, False):.5f}")
print(f"a hit    ${cached_cost(SONNET, 1800, 60, 250, True):.5f}")
Predict

A hit reads the 1,800 shared tokens at $0.10 per million instead of $2. What does a hit cost, against $0.00622 with no cache?

Pick one to see what it prints.
Pick an answer above to see the rest