Inference Economics & LatencyPrefix Cache Hit Rate
Progress

Prefix Cache Hit Rate

medium
Start small, think out loud.

You are reading the monthly invoice for an AI support chatbot, and the finance question on the table is why it costs what it costs. The largest lever is one most teams never measure: prompt caching. The provider keeps the opening part of your prompt, the prefix, in a cache and bills a request at a steep discount when its opening text matches that stored prefix - the discount applies only on an exact match, so one fresh value stamped into the head of the prompt, a run id or a timestamp, drops the discount for that traffic to zero, with no error and no warning anywhere in the product. Implement cache_savings(prefixes, full_cost, cached_cost): prefixes is a prompt fingerprint, one string per request in arrival order; the cache holds the last served prefix, so a request is a hit exactly when its fingerprint equals the previous request's, and the first request of any sequence always misses. full_cost and cached_cost are per-request prices in dollars for uncached and cached-serving requests. Return a two-item list: first the effective hit rate over all requests (hits divided by the request count, rounded to 4 decimal places), then the total savings in dollars (each hit saves full_cost minus cached_cost, rounded to 2 decimal places). A single request returns [0.0, 0.0] - there is nothing to compare against, and a default hit rate of 1.0 would inflate every savings claim downstream.

Time:O(number of requests)
Space:O(1)
prefix-cachinginference-costtoken-economicsai-track
solution.py
⌘↵ run
Loading...
OUTPUT

Run your code to see output