Skip to content

Freshness and caching

Max age for web content: check a copy's age when you read it

Enforce a maximum age for stored page copies at read time, per question, with HTTP's request directives as the model and a short Python check.

By Curtis Mccann, Live Web Data team · Published

This article was drafted with AI assistance from the sources listed below and edited and published by the Live Web Data team. Disclosure: the Live Web Data API, which this article mentions, is this site's own product.

Max age at read time
  1. 1Store the fetch timeEvery page copy keeps the UTC time your code received it.
  2. 2Pick the limit per questionStart from the page type's limit, for example 86,400 seconds for docs, and tighten it when the question is about right now.
  3. 3Compute the age on readAge is now minus the stored fetch time, in seconds.
  4. 4Use or re-readUse the copy while its age is within the limit. Otherwise read the page live and store the new copy.

Your app keeps copies of the pages it has read, each with the time it was fetched. One user asks how to install a library, and yesterday's copy of the docs page is fine. Another asks what changed in that library today, and the same copy is too old. The page did not change between the two questions. The question did. So the maximum age of a copy is not only a property of the page. It is also a property of the request, and it is checked when you read the copy, not when you store it.

Expiry at write time, max age at read time

There are two places to put a freshness limit, and they behave differently.

An expiry at write time is set on the copy when you store it. requests-cache, a Python HTTP cache, is set up this way in its expiration docs: CachedSession(expire_after=360) sets an expiration that applies to all new responses, and urls_expire_after sets one per URL pattern.

A max age at read time is chosen by the caller for one request. Your code computes the copy's age from its stored fetch time and compares it with the limit for this question. The same copy at 5 hours old passes a 24-hour limit and fails a 1-hour limit.

For an LLM app, the second one is the decision that matters. The page type gives you a default. The question can make it stricter.

HTTP already lets the client set the limit

A response's Cache-Control: max-age is the site's limit for caches. RFC 9111 also defines request directives, which carry the client's own limits:

Request directiveWhat RFC 9111 or MDN saysWhat it maps to in your store
max-age=NRFC 9111: the client "prefers a response whose age is less than or equal to the specified number of seconds"The limit for this question, in seconds
no-cacheRFC 9111: the client prefers that a stored response not be used without successful validation on the origin serverRead the page live for this question
only-if-cachedRFC 9111: a cache that honors it should answer with a stored response or a 504 (Gateway Timeout)Stored copies only, for example in tests or offline runs
max-stale=NMDN: the client allows a stored response that is stale within N secondsA fallback window after a failed live read

Two notes keep this honest. RFC 9111 calls request directives advisory: caches may implement them but are not required to. And MDN notes that the major browsers do not support requests with max-stale or min-fresh. So you would not rely on an intermediate cache to enforce your limit. You copy the model into your own store, where you control it.

MDN also explains one value you will want: browsers still send max-age=0 when reloading, for backward compatibility. In your store, a max age of 0 means a live read.

Put the check where the fetch time is

The check belongs in the one function that loads page copies. That function has the stored fetch time, so it can compute the age without guessing. The code that handles the question decides the limit and passes it in, in seconds. Do not ask the model to judge whether a copy looks recent: it sees only what you put in the prompt.

This runs as written, with no dependencies:

from datetime import datetime, timedelta, timezone

store = {}  # (url, country) -> {"text": str, "fetched_at": datetime}

def read_live(url, country):
    # Replace with your real fetch. Returns the page text.
    return f"live text of {url} ({country})"

def load_page(url, country, max_age_seconds, now):
    """Return (text, fetched_at, origin), origin is 'stored' or 'live'."""
    copy = store.get((url, country))
    if copy is not None and max_age_seconds > 0:
        age = (now - copy["fetched_at"]).total_seconds()
        if age <= max_age_seconds:
            return copy["text"], copy["fetched_at"], "stored"
    text = read_live(url, country)
    store[(url, country)] = {"text": text, "fetched_at": now}
    return text, now, "live"

now = datetime(2026, 11, 22, 12, 0, tzinfo=timezone.utc)
store[("https://example.com/docs", "us")] = {
    "text": "stored docs text",
    "fetched_at": now - timedelta(hours=5),
}

# How to install: 86,400 seconds (24 hours) is fine for docs.
print(load_page("https://example.com/docs", "us", 86400, now)[2])  # stored
# What changed today: 3,600 seconds (1 hour), so the copy is too old.
print(load_page("https://example.com/docs", "us", 3600, now)[2])   # live

The copy is 5 hours old, which is 18,000 seconds. It passes the 86,400-second limit and fails the 3,600-second one, so the second call reads the page live and replaces the stored copy. The store is keyed by URL and country, because the same URL read from two countries can return different pages.

Return the origin and the fetch time with the text. The prompt needs the fetch time either way, and the post on fetch timestamps in context shows where it goes.

Choose the number per question

Start from a limit per page type, kept in configuration. The guide on live vs cached web data gives example starting points, such as 24 hours for docs and 1 hour for prices. Then let the question adjust it:

  • About right now ("is it down", "what changed today", "the price today"): a short limit, or 0 for a live read.
  • The user pasted the URL: 0. They expect what is on the page now.
  • Background or reference ("how does this option work"): the page type's limit.

Let the question tighten the limit, never loosen it. A loose limit on a question about right now is how a 5-hour-old copy ends up quoted as current. A failed live read is a separate case with its own window; keep that number apart from the max age.

Where the store sits with a live API

On Live Web Data API, "Every request fetches the page live; there is no cache." The max age check therefore runs on your own store, and statuses[].fetchedAt gives you the fetch time to save with each copy. "Failed fetches cost 0 credits." The API docs list the response fields.

Frequently asked questions

What is the difference between a cache expiry and a max age?

An expiry is set on a copy when it is stored. A max age is chosen by the reader for one request and compared with the copy's age at that moment, so the same copy can be fresh enough for one question and too old for another.

What does the Cache-Control max-age request directive mean?

RFC 9111 says it indicates that the client prefers a response whose age is less than or equal to the given number of seconds. It is the client's limit, not the site's. RFC 9111 calls request directives advisory: caches may implement them but are not required to.

Where should a max age for web pages be enforced?

In the one function that loads page copies, because it has the stored fetch time. The code that handles the question passes the limit in seconds, and the loading function compares it with the copy's age and reads the page live when the copy is older.

What does a max age of 0 mean for a page copy?

Treat it as a request for a live read. MDN notes that browsers still send max-age=0 when reloading a page, for backward compatibility, and no-cache for a force reload.

Sources

  1. RFC 9111: HTTP Caching (Documentation)
  2. MDN: Cache-Control (Documentation)
  3. requests-cache: Expiration (Documentation)