I’ve recently spent some time using AI agents to look for performance improvements in a large codebase.
I didn’t ask an AI to rewrite the system or “make it faster”. I asked it to search broadly, come up with small hypotheses, and bring back enough evidence for me to decide what was worth trying.
I wanted to see how much checking and rework the suggestions would need before they were ready to implement. Here’s what I did.
Starting with examples
The first prompt included a few things:
- The goal was to improve latency along the full request path.
- The proposed changes had to preserve behavior.
- Managed code was preferred. Native code was only in scope for a very high return and low risk.
- Each candidate had to come with evidence, a suggested change, tests, and an estimate of the performance gain.
I also shared links to 3 pull requests I liked and said, “I expect to see findings such as these.” That was probably the most useful part of the prompt. The examples gave the agents a feel for the kind of change I was looking for: small, concrete, and low-risk. Much better than trying to describe all of that in abstract terms.
Before looking for opportunities, the agents mapped the request path and wrote down the rules for the investigation. That gave all the discovery agents the same starting point.
Looking in parallel
I asked the agents to come up with hypotheses, validate them, and suggest improvements. The work was split into 20 areas. A separate set of agents reviewed the proposed candidates and tried to find holes in the reasoning.
In total, 42 agents worked on discovery, validation, ranking, or reviewing the results. They proposed 20 candidates. Two were rejected, and a few overlapping findings were combined. Some areas had no useful findings at all. That’s fine. I didn’t expect to find something in every area just to make the report look complete.
Having a different agent validate each idea was important. It’s easy to read a little too much into a promising code path once you’ve found it. A fresh reviewer can ask the annoying questions: Is this actually called? Does it preserve ordering? What happens for empty input? Is the expensive work already cached somewhere else?
Writing the hand-off
Each candidate became a short brief with:
- The code and evidence behind the idea.
- The smallest change that might help.
- The behavior that must stay the same.
- Tests that would check those invariants.
- A benchmark plan.
- Any unknowns, clearly called out instead of guessed.
I wanted another agent to be able to pick up a brief and implement it without repeating the research. The brief was the hand-off between “this might be worth doing” and “let’s change the code.”
Working in batches
One of the things I like about this approach is that it lets me work in batches. I can set the direction for a group of investigations, let the agents work, and then review the findings together. I don’t need to finish one idea from research to PR before looking at the next.
The briefs make this practical. Once I’ve reviewed the candidates, I can pick a few for implementation and leave the rest for later, without starting the research again.
That’s how AI helps me scale my work. My attention goes to setting the requirements, comparing the evidence, and choosing what to pursue, rather than doing every step of every investigation myself. The review still takes time (and doesn’t go away just because there are more agents).
Tests before changes
For each candidate I chose to pursue, I asked for a draft pull request and made one thing clear: write and run the tests before implementing the change.
The process was:
- Make sure we have tests that describe the existing behavior.
- Run them on the unchanged code.
- Make the change and run the tests again.
- Try plausible incorrect versions of the change and check that the tests fail.
- Compare a small benchmark before and after – either ad-hoc benchmarks, or actual addition to our benchmarking suite
- Open a draft pull request with the results and caveats.
The baseline matters. If a test has never passed before the change, how do we know it describes the behavior we wanted to preserve?
The mutation checks were useful too. They exposed cases where a test could hang instead of failing, and showed that the tests caught the mistakes they were meant to catch. Nothing fancy here, but it gave me more confidence than just seeing a green test run.
Asking if it’s worth it
One candidate looked like an easy allocation reduction. I asked, “What’s the actual gain?”
After tracing where it ran, the answer was: not much. It saved a few hundred bytes and some nanoseconds, on a path already doing two network calls. It also wasn’t used by ordinary requests by default.
So we decided not to open a pull request for it. It might be a reasonable cleanup, but calling it a latency improvement would have been a stretch.
That was a useful outcome too. I didn’t need AI to find more things to change; I needed it to help figure out which ideas were worth the effort.
What worked for me
- Give the agents examples of the kind of work you want.
- Split discovery from validation.
- Let “nothing found” be an acceptable result.
- Write down invariants before changing the code.
- Run tests on the baseline, and make sure they catch deliberate mistakes.
- Keep benchmark results separate from end-to-end performance claims.
- Ask where the code runs and whether that path matters.
- Leave the final decision to a person.
AI helped me cover more ground and get through repetitive work. It also made it easier to follow a candidate from code inspection to tests and a draft PR. But the useful part wasn’t that it produced a long list of optimizations. It was being able to ask it to show its work, then decide which ideas deserved a closer look.
I’ve still got more candidates to review. But time will tell whether these small improvements add up to something users can actually notice.