Your site runs on Cloudflare Workers, page caching is on, and the CPU time bill keeps climbing anyway. That was our own content site. We pulled one day of request data and found the cache hit rate was only about 4%. Almost every page request was rendered from scratch. We had an AI agent check it, found six causes, and shipped the fixes the same day. Turning the cache on proves nothing. Watch the hit rate.
What happened
Paid Workers plans charge by CPU time. A page rendered from scratch has to query the database and build the HTML, which takes tens of milliseconds. A cached page comes back in one or two milliseconds. So there is only one way to save money: make as many requests as possible skip rendering.
This site gets a little over 200,000 requests a month. That is not much traffic, but the median CPU time per request is above 30 ms, which is high. The code already uses the Cache API to cache pages for visitors who are not logged in, so this should not happen.
The agent did not change any code first. It queried the Cloudflare GraphQL Analytics API for the last 24 hours of requests, grouped them by path and status code, then split them by visitor type. The cache was checked more than 6,000 times and hit only a little over 200 times.
Why the cache almost never hit
| Cause | What was happening | The fix |
|---|---|---|
| Every deploy cleared the whole cache | The cache key, the value that identifies a cached page, included the build time. On a day with dozens of deploys, each deploy created a new key | Use a hash of the frontend file names as the key. If the frontend has not changed, the key stays the same |
| Every new post cleared the whole cache | Any edit to one article expired every page at once | Article pages re-render only when that article changes. List pages still refresh right away |
| Crawlers skipped the cache | The cache was used only for requests that asked for HTML. Many crawlers and link previews do not ask that way | Loosen the rule for reading the cache. Store only HTML |
| Every 404 rendered in full | 404s made up more than a quarter of page requests. Most were requests for icons and old files, plus scanner probes for random paths | Send a one-line plain text 404 for these paths. Cache the other 404 pages for a short time |
| Two APIs were called on every visit | Checking "is the user logged in" and reading the public config. Each was called over 3,000 times a day | Requests without a login cookie get a "logged out" answer right away. Put the public config in the cache |
| Login and sign-up pages were not cached | The visitors to these pages are mostly crawlers, so each request was rendered | When logged out, the content is the same for everyone, so cache it |
The first two causes matter most. On a site that publishes articles and changes code every day, the cache is cleared right after it fills. It is as if the cache were off.
After the release we tested it. The same article page took about 0.9 seconds on the first load and about 0.1 seconds on the second. We will not report a CPU time figure yet. We need a day or two of billing data first.
What this means for you
If your site also runs on Workers and renders pages on the server, some of these causes probably apply to you too. This is especially likely for sites built from a template. Check three signals first:
- Few requests, but high CPU time: most requests are being rendered in full.
- Lots of 404s: open the analytics dashboard and group by status code. If 404s are a large share, it is worth fixing.
- Frequent deploys: several deploys a day, and each one clears the cache, so the hit rate will stay low.
Let your AI agent check it
You do not need to read the code yourself. In your project folder, give the prompt below to your AI agent. It will first produce a data table, then make changes.
Task: Lower the CPU time of this Cloudflare Worker. Measure the data first, then change the code.
Step 1, look only, do not change anything:
1. Read the code and explain: Is there page caching? What makes up the cache key? What actions invalidate the cache?
2. Use the Cloudflare GraphQL Analytics API to query requests for this domain over the last 24 hours. Group them by path, status code, content type, and User-Agent. Use the login credentials that the local wrangler already has. Do not print them.
3. Give me a table: how many page, API, 404, and static file requests there were, which ones were rendered in full, and the cache hit rate.
Step 2, check each item against the data, and change only what has a problem:
1. Does the cache key include any value that changes on every build, such as a timestamp?
2. Does one content change invalidate the whole site's cache?
3. Do crawlers (requests that do not ask for HTML) bypass the cache?
4. Do requests for missing files and scanner paths render a full 404 page every time?
5. Is there an API that is called on every visit and returns the same result for every logged-out visitor?
6. Before a cache hit, does the code still query the database or do other heavy work?
Hard rules:
- Responses for logged-in users are not stored in the cache and are not served from it.
- APIs that handle money, permissions, or personal data are not cached.
- When content changes, that page must update right away for visitors.
- Real files like robots.txt and sitemap must not be treated as junk paths.
After the changes, verify locally: the second request for the same page hits the cache, and a request with a login cookie does not use the cache.
Then tell me what each change did, and which content will be delayed and for how long.
List separately any decisions that need my input. Do not decide them yourself.
The prompt starts with "measure first" because every site has a different problem. This time, cache invalidation cost us the most CPU. On another site it could be a single API. Changing code without looking at the data can waste your time.
Things to watch
- When you loosen the cache, logged-in users matter most. Have the agent test with a request that includes a login cookie. Check that it cannot get someone else's page, and that it does not put your own page into the shared cache.
- Related content on article pages can lag. The article itself updates right away. But related posts and new comments on the page may take up to a day to reach logged-out visitors. If that is too slow for you, shorten the time.
- Put real files with extensions on an allow list. Before you return a plain 404 for missing files, list files like robots.txt and sitemap.xml first. Otherwise search engines cannot crawl them.
- Rarely visited pages do not benefit from the cache. For example, when a crawler visits thousands of tag pages one time each. For these you can only limit crawlers with robots.txt. The cost is that some tools will no longer see your data. Weigh that yourself.


Comments (0)
No comments yet. Start the conversation.