Sub topic 6
How Long a Cache Entry Lives and What Refreshes It
This chapter covers cache lifetime, usually called time to live (TTL). It assumes you know how exact prefix matching works and that you can read a short Python snippet. It also assumes you have already placed a cache breakpoint or let the provider cache automatically.
A cache entry is temporary
A provider keeps the reusable attention state for your prefix only for a limited time. After that, the entry is evicted. The next request with the same prefix is processed in full, and the provider writes a new entry. Nothing in the response tells you an eviction happened. You see it only as a miss in the usage data.
Lifetimes differ by provider and can change, so check the current documentation before you design around a number. These are typical patterns:
- A short default lifetime, often about five minutes.
- An optional longer lifetime, such as one hour, which some providers offer at a higher write price.
- Automatic caching on some providers, where you do not choose the lifetime and the entry may survive only for minutes of inactivity.
What refreshes an entry
The lifetime is usually an inactivity window, not a fixed deadline. Each time a request hits the entry, the timer restarts. A prefix that is read every few minutes can stay cached for hours. A prefix that is read once and then left alone expires.
The timeline below shows a five-minute window.
Two details are easy to get wrong:
- Only a hit refreshes the entry. A request that changes the prefix does not extend the old entry. It creates a different one.
- The refresh applies to the cached prefix. If you have several breakpoints, check your provider's documentation to see which entries a hit refreshes.
Simulating lifetime in code
This toy model shows why traffic spacing matters. It tracks one entry and reports whether each request hits.
def simulate(request_times, ttl):
expires_at = None
results = []
for t in request_times:
if expires_at is not None and t <= expires_at:
results.append("hit")
else:
results.append("miss")
expires_at = t + ttl # a hit refreshes, a miss writes a new entry
return results
minutes = 60
steady = [0, 3, 6, 9, 12]
sparse = [0, 8, 16, 24, 32]
print(simulate([m * minutes for m in steady], ttl=5 * minutes))
print(simulate([m * minutes for m in sparse], ttl=5 * minutes))
print(simulate([m * minutes for m in sparse], ttl=60 * minutes))The first line prints one miss followed by four hits. The second prints five misses, because every gap is longer than the lifetime. The third prints one miss followed by four hits, because the longer lifetime covers the eight-minute gaps.
Choosing a lifetime
The choice depends on how regularly the same prefix is reused.
| Traffic pattern | Likely fit |
|---|---|
| Many requests per minute | Short lifetime. Hits keep it alive. |
| A request every 10 to 30 minutes | Longer lifetime, if offered, or accept frequent writes. |
| Rare, bursty use with long idle gaps | Caching may not pay off. See the later chapter on when to skip it. |
A longer lifetime usually costs more to write. It helps only when the extra hits outweigh that cost, so compare using the provider's published prices.
Common mistakes
- Assuming a cache entry lasts for the whole session. A user who pauses for ten minutes can return to a cold cache.
- Treating a miss after a pause as a bug. Check the gap against the lifetime before you search for prompt changes.
- Hard-coding a lifetime in your cost estimates. Read it from the current documentation.
Exercise
A prefix has a five-minute inactivity lifetime. Requests arrive at minutes 0, 4, 8, 15 and 16. Which requests hit?
Show answer
The requests at minutes 4, 8 and 16 hit. Minute 0 is a miss that writes the entry. Minute 4 is within five minutes and refreshes the timer to minute 9. Minute 8 is within that window and refreshes it to minute 13. Minute 15 comes two minutes after expiry, so it misses and writes a new entry. Minute 16 hits that new entry.
Exercise
Your hit ratio drops every morning for the first few requests, then recovers. The prompt text has not changed. What is the most likely cause, and what are two options?
Show answer
The entry expired overnight, so the first request writes it again. Options: accept the one-time write cost, or use a longer lifetime if the provider offers one and the traffic justifies the higher write price. Keep-alive pings overnight rarely pay off.