Finding and Fixing Performance Problems with AI Agents
Feed it profiling data instead of guesses, hypothesis before fix, always measure before and after โ otherwise the agent just "optimizes away" the behavior.
No measurement, no fix
An agent told only "the app is slow" guesses just as much as one debugging without a stack trace. Feed it real profiling output: timing data, a flamegraph export, database query logs with durations. Concrete numbers show where time is actually being lost โ not where it merely feels lost.
Hypothesis before fix
Same as debugging: have it name likely causes first, ranked by suspected impact, then test one deliberately. An optimization made without a prior hypothesis often hits the wrong spot โ fast to ship, but pointless.
Measure before and after
Every optimization needs a benchmark run before and after, under the same conditions. Without a comparison number you don't know whether a change actually helped โ only that it feels faster.
Micro vs. architectural optimization
Small changes (a loop, a query) are quick to make but often yield little. The biggest wins usually sit in architectural decisions: an N+1 query problem, unnecessary recomputation, missing caching. An agent should name both categories โ and tell you which one would have the bigger effect.
The trap: optimizing behavior away
An agent can make things "faster" by simply cutting work โ skipping a validation, serving a cache with stale instead of fresh data, hardcoding a result. That looks great on the benchmark but is a correctness bug. After every performance change, check whether the result is still correct, not just whether it's faster.
EXAMPLE
Prompt: "Here's the profiling export for /api/search (attached). Name the three most likely causes of the 800ms latency, ranked by suspected impact. Don't change anything yet. Afterward, measure before/after with the same benchmark command and run the existing test suite."
๐ ๏ธ EXERCISE โ TRY IT YOURSELF
Take a deliberately slow function (e.g. a nested loop or an N+1 query) and have an agent optimize it systematically.
- Measure the current runtime with a fixed benchmark command and record the number.
- Have the agent name the most likely cause first, without changing anything.
- Then let it optimize, rerun the same benchmark, and run the existing tests before you accept the fix.
โ SELF-CHECK
- โ Did the runtime demonstrably drop (number vs. number), not just feel faster?
- โ Are all existing tests still passing?
- โ Did the agent remove or weaken behavior in order to appear faster?
QUICK QUIZ
What trap should you watch for specifically with AI-assisted performance optimization?
SOURCES
- Claude Code Docs: Best Practices (Give Claude a way to verify its work) โ code.claude.com
- MDN: Web Performance โ developer.mozilla.org
- MDN: Web Performance Guides โ developer.mozilla.org