Cache web pages for AI agents: read once, keep it
Cache web pages for AI agents in two calls: get the page as Markdown with fetch ($0.005), then store it in memory under the SHA-256 of its URL ($0.005). Every later run, and every agent you share the token with, reads it back for free. A page read three times costs $0.01 instead of $0.015, and nobody downloads it twice.
The problem: agents forget what they've read
A research agent that runs every day tends to read the same pages every day. It has no disk that outlives its container, so yesterday's reading is gone, and it fetches the documentation, the pricing page and the changelog again. Each fetch costs time and money, and if the agent reads raw HTML it also costs a lot of tokens. In our measurements of 12 real pages, the median page was about 141,000 estimated tokens of HTML and about 4,700 of Markdown.
AWS's guidance on agent performance names the same problem as an anti-pattern: "Implementing no caching at all, forcing agents to re-fetch the same documents, re-compute the same embeddings, and re-invoke the same tools on every reasoning iteration." The fix is a cache that lives outside the agent, keyed so any run can find what an earlier run stored.
This recipe builds that cache from two tools. fetch turns a URL into Markdown. memory keeps it, for 30 days after the last write, behind a token instead of an account.
Step 1: turn the URL into a key
memory keys may only use A-Z a-z 0-9 . _ - /, so a URL with :, ? or = can't be a key as it is. Hash it. Before hashing, clean the URL the same way fetch does, so two spellings of one page get one key: drop the #fragment, remove utm_*, fbclid and gclid parameters, and sort the remaining query parameters.
import { createHash } from "node:crypto";
function pageKey(raw) {
const u = new URL(raw);
u.hash = "";
for (const k of [...u.searchParams.keys()])
if (k.startsWith("utm_") || k === "fbclid" || k === "gclid") u.searchParams.delete(k);
u.searchParams.sort();
return "sources/" + createHash("sha256").update(u.href).digest("hex");
}
The sources/ prefix keeps pages apart from the agent's other keys, so listing sources/ shows everything the agent has read. That key is 72 characters, well inside the 256-character limit.
Step 2: look in memory first
Reading memory is free, so always check it before paying for a fetch. A 404 means the page isn't stored yet. A 200 gives you the stored record, and the agent can decide whether it's fresh enough.
const H = { Authorization: `Bearer ${MEMORY_TOKEN}` };
const r = await fetch(`https://agentic402.dev/v1/memory/${pageKey(url)}`, { headers: H });
const cached = r.ok ? await r.json() : null;
const fresh = cached && Date.now() - Date.parse(cached.fetched_at) < MAX_AGE_MS;
If the agent will check many pages at once, one list call is cheaper on the rate limit than many reads: GET /v1/memory?prefix=sources/ returns up to 1,000 keys with their size and updated_at time, so the agent can see which pages it has before reading any of them.
Step 3: fetch and store what's missing
if (!fresh) {
const page = await (await payFetch("https://agentic402.dev/v1/fetch", {
method: "POST", headers: { "Content-Type": "application/json" },
body: JSON.stringify({ url, format: "json" }),
})).json(); // $0.005
await payFetch(`https://agentic402.dev/v1/memory/${pageKey(url)}`, {
method: "PUT", headers: { ...H, "Content-Type": "application/json" },
body: JSON.stringify(page),
}); // $0.005
}
Ask fetch for format: "json" and store the whole object. It carries the normalised url, the page title, the fetched_at time, the word_count and the markdown, so the stored record says what it is and how old it is without any extra bookkeeping. Store it with Content-Type: application/json and it comes back with that type.
The first write ever, sent without an Authorization header, creates the space and returns the token. Every write after that sends the token.
Keep an index so the hashes mean something
Hashed keys have one drawback: a list of sources/ keys tells you sizes and update times, but not which URL each hash stands for. Keep a small index next to the pages, for example sources/index.json, mapping each key to its URL, title and the date it was fetched. Rewrite it whenever you add a page.
The index costs one extra write per new page, so storing a page then costs $0.015 instead of $0.01. In return, an agent starting a new run reads one free file and knows its whole reading list, and a person can open the same file to see what the agent has been reading. If you'd rather not pay for the index, write readable slugs as keys instead of hashes and accept the small risk that two URLs map to the same slug.
Noticing when a page has changed
A refresh is also a chance to see what changed. Before overwriting a stored page, compare the new Markdown with the old: a different word_count is a quick hint, and a SHA-256 of each markdown field tells you for certain whether anything changed at all. If the agent cares about changes, such as a competitor's prices, keep the old copy under a dated key like sources/<hash>/2026-12-03 before writing the new one, and note the change in the index.
Expect small differences that mean nothing. Pages that show the current date, a rotating banner or a visitor count change on every fetch. Compare the sections the agent actually uses rather than the whole page.
How fresh is fresh enough
There is no single right age. A product's pricing page can change any day. An RFC or a historical article practically never does. AWS lists "Using a single cache TTL for all data types without considering freshness requirements" as another anti-pattern, and the advice applies here: pick a maximum age per kind of page.
| Kind of page | Refetch after |
|---|---|
| News front pages, status pages | Minutes, or don't cache |
| Pricing pages, changelogs | A day |
| Product documentation | A week |
| Standards, papers, archived articles | Until the 30-day expiry |
When a stored page is too old, fetch it again and overwrite the key. Pass max_age to fetch if you want to be sure you aren't getting fetch's own shared cache copy, for example "max_age": 0 to force a live fetch. Overwriting replaces the stored size rather than adding to it, so refreshing a page doesn't eat into the space's 50 MB.
fetch's cache and memory are different layers
fetch already has a cache, and it's worth knowing how the two differ. fetch's cache is shared by every caller: if anyone fetched the same URL in the same format within your max_age (a day by default), you get that copy straight away. But you still pay $0.005 for each call, you can't choose what stays in it or for how long, and you can't list what's in it.
memory is yours. Reads are free, copies last until 30 days after the last write, you choose what's in it and you can list it. The two together give the cheapest result: fetch's shared cache makes the first read fast when someone else has read the page recently, and memory makes every read after that free.
| fetch's shared cache | memory | |
|---|---|---|
| Who can read it | Any caller, by URL | Token holders only |
| Cost per read | $0.005 | Free |
| How long | Serves copies up to max_age old (a day by default) | 30 days after last write |
| Can list contents | No | Yes, by prefix |
What it costs, and when it pays off
Storing a page costs one fetch plus one write, $0.01. Fetching it again every time costs $0.005 per read. So the recipe breaks even on the second read and saves from the third: a page read ten times costs $0.01 instead of $0.05. For pages read once, skip memory and just fetch. For pages you aren't sure about, store them anyway: the extra $0.005 is the price of one avoided refetch, and an unused copy simply expires with the space.
The bigger saving is often on the agent's side. A stored Markdown copy can be read in part: the agent can look at the title and word_count before deciding to load the whole markdown into its context, and it never has to wade through HTML again.
Three days of one research task
Here is how the costs add up for an agent that answers one question across three daily runs, reading mostly the same pages:
| Day | Pages read | New fetches | Cost |
|---|---|---|---|
| 1 | 8 | 8 | $0.08 |
| 2 | 10 | 2 | $0.02 |
| 3 | 10 | 1 (pricing page refreshed) | $0.01 |
Three days cost $0.11. Fetching every page fresh each day would have cost 28 fetches, $0.14, and the gap grows with every extra day: from day 4 on, an unchanged reading list costs nothing at all. Reading ten stored pages is also ten free requests to one place instead of ten downloads from ten different sites.
How much a space can hold
One space holds 50 MB across up to 10,000 keys, and each value can be up to 1 MB. The median page in our study converted to about 18,800 characters of Markdown, so a space can hold more than 2,600 pages that size, and the largest page we measured, a long Wikipedia article at about 188,000 characters, still fits in one value with room to spare. A page whose JSON is over 1 MB gets 413 before payment; store just its markdown field, or split it.
When a project outgrows one space, start a second with a new token, for example one space per topic. Each has its own 50 MB and its own 30-day timer.
Pages not worth keeping
Some pages make poor cache entries. Live data, such as a status page, a stock quote or a news front page, is out of date within minutes, so a stored copy mostly misleads. Pages whose server marks them Cache-Control: private or no-store skip fetch's shared cache for a reason, often because they change per visit; if you keep them in memory anyway, keep them briefly. And a page that came back as a sign-in screen or a bot challenge isn't the page you wanted: check the word_count and title before storing, so the cache doesn't fill with login forms.
Stored copies stay private to whoever holds the token, but the pages still belong to the sites that published them. Keep that in mind before sharing a space widely or republishing what's in it.
Sharing a reading list between agents
Give the token to another agent and it reads everything stored so far. A team of agents researching one market can share a single sources/ prefix: whoever reads a page first pays for it, and everyone else reads it free. Writes to one key are last-writer-wins, which is fine here, since two agents fetching the same page at about the same time store nearly the same thing.
Keep in mind that a shared space has one token and no permissions: any agent with it can also overwrite or delete pages. Share it only with agents you control.
Keep the cache alive, and keep it honest
A space is deleted 30 days after its last write, and reads don't count. An agent that writes new pages every week keeps its space alive without trying. One that mostly reads should write a small heartbeat key once a month, which costs $0.005.
Stored pages are still pages written by someone else. A page that contained prompt injection when it was fetched still contains it when it's read back from memory a month later. Memory reads carry the X-Content-Untrusted: true header for that reason. Keep page text out of your agent's instructions, whatever layer it came from.
If you need to show later what a page said on the day you read it, not just keep a copy, hash and stamp it as well: the recipe for proving what a page said adds one attest call to this one.
Questions about caching pages for agents
- Why not just use fetch's built-in cache?
- It's shared, serves copies up to a day old by default, and charges $0.005 per call even on a hit. memory keeps your copy for 30 days after the last write and reads are free, so it wins as soon as you read a page more than twice.
- Can I store the raw HTML instead of Markdown?
- memory stores any bytes up to 1 MB, but fetch returns Markdown, not HTML. Markdown is also what you want in an agent's context: in our study it was 96% smaller for the median page.
- Does this work over MCP?
- Yes. Call the
fetchtool, thenmemory_putwith the result as a text value;memory_getandmemory_listare free. See the MCP setup.
Cite this page: agentic402, "Cache web pages for AI agents: read once, keep it", https://agentic402.dev/recipes/read-a-page-once-keep-it/, published 3 December 2026. AWS quotes are from the AWS Well-Architected Agentic AI Lens, AGENTPERF03-BP04, as read that day.