I host a lot of sites on Cloudflare, and CPU usage time kept staying stubbornly high. Every month, CPU time made up more than 80% of the bill. Recently I’d had enough and started trying everything I could to bring it down.
First, understand how CPU usage time is billed
To reduce CPU usage time, you first need to understand how it’s calculated:
CPU Time Cost = (Average CPU ms per request × Request Count - Included Free CPU ms) ÷ 1,000,000 × $0.02
Once you see the formula, the way to save money is clear: there are only two levers — reduce the number of requests, and reduce the CPU time per request.
On request count: high-traffic sites obviously get a lot of requests, but beyond real traffic there’s another big contributor — crawlers and bot traffic. Normal search engine and AI crawlers (Google, ChatGPT, and so on) can’t be blocked, unless you don’t want the traffic they bring you. Beyond that, though, many sites see tons of malicious bot traffic, and that’s exactly what you should block.
To reduce CPU time per request, the most direct approach is to make the site static and make full use of caching. You should also audit your code and cut out anything that burns a lot of CPU.
So recently, I did three things:
1. Block malicious traffic
I’d known for a long time that some of my sites had persistent malicious bot traffic, but I couldn’t recognize it, let alone block it.
This time I used the Cloudflare Agent in the dashboard. I asked it to identify the malicious and bot traffic on each site, and to write the corresponding WAF rules, which I then filled in myself.
To be honest, the Agent isn’t great to use. But since it’s directly connected to all of Cloudflare’s data, it’s still very useful. Of course, even though it always claims it’ll set up the WAF rules directly, nothing happens after waiting a long time, so I just have it write the rules out and I configure them myself.
The effect was immediate: request volume dropped significantly, and total CPU time went down noticeably too.
2. Enable static generation and caching
Most of the sites on Cloudflare are built with Next.js. The Next.js + OpenNext architecture has supported caching all along, but I’d simply never turned it on.
So I had Codex enable caching on the high-traffic sites. For publicly accessible pages — landing pages and blog pages — I switched them to build-time static generation, which greatly cut the CPU time consumed on every visit.
3. Optimize the code
As long as SEO isn’t affected, anything that can be computed and rendered on the frontend, I moved to the frontend.
Use as few automated programs as possible — polling and the like. Careless use of those can burn a huge amount of CPU.
Minimize dynamic rendering: whenever something can use a cache, cache it.
独立产品人日记
Notes on indie products, AI tools, growth, and engineering.
Related Articles
Codex + Hyperframes: Make a Product Video with One Sentence
Using the Codex + Hyperframes plugin to generate a product introduction video with just one prompt — no more painful video editing.
Codex + Game Studio: A Great Tool for Making Games
Hands-on experience with Codex's Game Studio plugin for browser game development — covering 2D games with Phaser, 3D experiments, and why this combo stands out.
The Key to AI Agents Is How to Make AI Perform Better
Using the HTML vs JSON architecture decision for an AI UI Designer as a case study, this article argues that in the AI era we must abandon old best practices and design products around one core principle: how to let AI do its best work.