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

Pick the cache lifetime from the traffic

With the hit rate, each lifetime has an expected price for the shared tokens.

def shared_price(price: dict, per_hour: float, ttl: str) -> float:
    """Expected dollars per million shared tokens: a hit reads them, a miss writes them."""
    if ttl == "none":
        return price["input"]
    hit = hit_rate(per_hour, 5 if ttl == "5m" else 60)
    return hit * price["read"] + (1 - hit) * price["write_" + ttl]


for per_hour in (0.5, 1, 6, 60):
    row = ", ".join(f"{ttl} ${shared_price(SONNET, per_hour, ttl):.2f}" for ttl in ("none", "5m", "1h"))
    print(f"{per_hour:4} an hour: {row}")
Predict

A prefix gets one request every two hours. Which is cheapest: no cache, a 5-minute cache or a 1-hour one?

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