Measure, fan out, fix once: a speed pass for agent-built apps
Making an agent-built web app fast is an engineering process, not a prompt. Baseline a production build, fan out four read-only audits, fix everything in one pass inside guardrails, then measure again the same way.
We build production GenAI for operations, knowledge, and data. More of the software around that work now gets written by coding agents, and agent-built web apps share a habit. They ship correct and slow. Nothing is broken. The homepage arrives late on a phone, every list pulls every row, and a charting library rides along on pages that never draw a chart.
Asking the agent to "make it faster" does not fix that. A procedure does. We run a speed pass the same way every time, in four stages: measure a production build, fan out read-only audits, fix once, measure again. The agent does most of the typing; the procedure decides what counts as done.
Stage one: a baseline you can trust
Measure first, and measure the thing users actually get rather than the thing running on your laptop.
A dev server is the wrong patient. It skips minification, recompiles on request, and carries debugging helpers no visitor ever downloads. The agent starts from a production build (next build && next start in a Next.js app) or a preview deployment that matches production.
Then Lighthouse in mobile mode, three runs per page, and we keep the median. One run is noise. A background tab, a cold cache, the laptop fan spinning up halfway through the second run (an unglamorous variable, but a real one): any of these can fake a win or hide a regression.
Which pages? The homepage, plus the two or three slowest pages the team already complains about. Not the whole sitemap. A pass that measures everything usually fixes nothing.
Write the medians to a file before a single line of code changes.
That file is the contract for the rest of the run, and stage four gets checked against it rather than against anyone's memory of how slow the site felt last week.
Stage two: fan out, read-only
Next the agent sends out four subagents, one per area. Each may read the codebase and run measurements, but none may edit a file.
Images and fonts. Every image becomes WebP or AVIF, sized to the box it fills on screen rather than a camera original shrunk by CSS. Images below the fold load lazily; the large image at the top loads first. Fonts load through the framework's own mechanism (next/font in Next.js), and the site sticks to one or two families. Third-party scripts nobody uses come out. Analytics and chat widgets wait until the page has finished loading.
Frontend bundle. A bundle analyzer names the heaviest packages, and a whole utility library imported for one function becomes an import of that function. Components the first screen does not need (charts, rich text editors, maps) load on demand through a dynamic import. Homepage data comes from the server, so the first paint already has content. AI answers stream as they are generated. Anything still waiting on data shows a skeleton, not a blank rectangle.
Database and APIs. Columns used to filter and sort get indexes, including the columns that row-level security policies check, such as user_id. In a Postgres policy, a bare auth.uid() call can be evaluated row by row; wrapping it as (select auth.uid()) lets the planner run it once per query. N+1 loops collapse into a single query. Long lists paginate. Heavy work like sending email, resizing uploads, or building reports moves to background jobs, so the request returns without waiting.
Servers and network. Server functions run in the same region as the database. Hosting defaults do not know where your database lives, so check the region you were given. Pages whose content rarely changes become static or incrementally regenerated. When a page calls three APIs one after another, run them in parallel or merge them. Last, someone opens Chrome's device emulation, picks a low-end Android profile on throttled 4G, and clicks through the main flows by hand. Lighthouse grades a page load. It will not mention that the checkout button takes a beat too long to answer a tap.
Every finding names the file and the page it affects. "Images are too large" is not a finding. "The hero image on the pricing page ships at full camera resolution into a slot the width of a phone" is.
Why read-only? A lane that cannot edit has no reason to shrink a problem it would then have to fix. Four readers never collide, so they run at once. And a list is something a person can read and strike lines from before anything moves.
The database lane runs at higher effort. The other three mostly pattern match (oversized image, unused package, missing lazy flag). The database lane has to read queries, follow them through security policies, and decide which index actually serves which access pattern, because an index on the wrong column slows every write and speeds up nothing. That judgment is worth the extra tokens.
Stage three: one fix pass, with guardrails
With four lists in hand, the main agent fixes everything in one consolidated pass.
We keep audit and edit apart because we have seen them mix. An agent that fixes as it discovers will touch the same component three times for three reasons, and the third edit quietly undoes part of the first. Interleaving also ruins the re-measure: if the score moved, nobody can say which change moved it, or whether two changes cancelled out. One merged list gives one diff a reviewer can read top to bottom.
The guardrails are written into the brief, so nobody relies on taste halfway through a long run:
- Do not remove features to gain speed. A faster page that lost its search box is a different product.
- Ask before removing a dependency. The agent may be right that a package looks unused, and it may also have missed the build script that needs it.
- Ask before changing the deploy region or anything about the database. Those changes reach every user, the bill, and sometimes where data is allowed to live.
Caching gets its own rule. It is where speed work quietly turns into a security bug. Cache responses that are identical for every visitor, like a public price list or a marketing page, and never anything tied to a signed-in user. A cache key that forgets the session hands one customer's dashboard to the next. Very fast, too.
Stage four: measure again, the same way
Same build mode, same pages, same three runs, same median, then a report with both columns side by side and the file change that explains each moved number.
The symmetry is the point. A baseline from a production build compared against a re-measure from a preview link in another region is fiction. This is the evidence habit from the checker is not the maker and the numeric quality gates note, applied to speed. "Faster" without a stored before and after is a mood.
Some numbers will not move. Report those too, because a finding that changed nothing measurable means the fix was cosmetic or the real bottleneck sits where none of the four lanes looked.
What it costs
A careful pass is slower than a sloppy one. Three Lighthouse runs on every page in scope, done before and again after, add up to real wall time, and the high-effort database lane spends more tokens than any other. The approval questions interrupt a person who wanted to walk away from the keyboard.
We pay it anyway. A pass that skipped the baseline cannot prove it helped, and a pass that edited during the audit cannot explain what it did. We also do not yet have a clean rule for when a median of three is not enough, for instance on a page whose score swings widely between runs. For now that page gets extra runs and a line in the report saying so.
A brief you can hand your agent
Here is the skeleton we start from. Swap in your own framework commands.
- Baseline. Run a production build, or use a preview link that matches production. Run Lighthouse in mobile mode three times on the homepage and on [slowest page 1], [slowest page 2], and [slowest page 3]. Record the median per page in a baseline file. Edit nothing yet.
- Audit. Send four read-only subagents, one per area: images and fonts, frontend bundle, database and APIs, servers and network. Every finding names the file and the page it affects. Run the database subagent at high effort.
- Fix. Merge the four lists and fix everything in one pass. Do not remove features. Ask me before removing a dependency, changing the deploy region, or changing the database. Cache only responses that are identical for every user.
- Re-measure. Repeat step one exactly. Report before and after for each page, with the change that explains each difference.
Then the report, blank on purpose. Fill it from your own runs, never from a guess.
Agent-built apps do not have to be slow apps, but they need the discipline a performance engineer would bring anyway: measure the real thing, look before touching, change it once, prove the result. The agent makes every step cheaper. It makes none of them optional. That discipline lives in the harness around the model, and at sunrisegenai.com the harness is the part we build.