Cutting the Cost of AI PR Reviews
In my last post, I said an AI PR reviewer should do more than read the code. It should prove what it finds, and that’s slower and burns more tokens. I think it’s worth it, but that doesn’t mean the cost doesn’t matter.
So I’ve been looking at where a reviewer’s tokens actually go, and it turns out most of them aren’t spent on the checking that makes it valuable. This post covers what I found, and what I think brings the cost down without cutting the review itself.
Where the Tokens Go
When I went through a reviewer’s sessions command by command, about a quarter of what it spent went on plumbing. That’s things like reading the PR’s comments, checking the builds, working out the diff and logging in. None of it needs any judgement.
Proving a finding was the same story. When reading the code suggests a bug, the reviewer writes a quick test on the fly to try to trigger it. It’s a throwaway unit or integration test that runs against the PR’s code and is never committed. It isn’t one of the pipeline’s tests, which the build runs anyway. It’s there to prove or disprove one suspicion. But before it could run one, the reviewer set up the environment. It fixed the Node path, pointed the PR’s code at the packages that were already installed and sometimes regenerated the database client. And it worked out how to do that from scratch every single time.
The other big cost was repeat work. Nearly half of all reviews were re-reviews after a new push. Each one is a whole session, even when the push only changes a couple of lines.
Review at the Right Time
A review should only start once the PR’s deterministic checks have passed. That’s the build, the linting, the tests and the static analysis. If one of them fails, there’s no point reviewing that push. The coding agent has to fix it anyway, and the fix needs reviewing again.
That way, by the time the review starts, the deterministic checks are done, and the reviewer can spend its tokens on the things that need judgement. It does mean the review starts a little later, but that’s cheaper than reviewing a push that has to change anyway.
Move the Deterministic Parts Into Scripts
If a step gives the same answer every time, it doesn’t belong in the skill’s instructions. It belongs in a script that the skill tells the agent to run.
For example:
- Deciding whether to review at all. Some PRs don’t need reviewing, like a draft PR that’s still being worked on. The same goes for a push that’s already out of date. If the coding agent pushes again before the review starts, only the latest push needs reviewing. A simple deterministic check can spot both up front.
- Gathering what the review needs. At the start of a review, the reviewer needs to know everything about the PR. That’s the code and the diff, the comments on the PR and any replies to the reviewer’s earlier findings, and whether the build passed on the latest commit. Again, a deterministic script can get this information.
- Running a throwaway test. When the reviewer writes a test on the fly to prove a bug, the PR’s code needs to be ready to run it. That means reusing the packages that are already installed rather than installing them again, regenerating the database client if the schema has changed, and knowing the exact command to run just that test. This is all deterministic code that the agent can call.
As well as saving tokens, we get consistency. The mistakes I kept seeing were in the plumbing: saying there were no open comments when someone had replied, or saying a PR was ready based on an old build. A script does it the same way every time.
Fewer Rounds
Every round between the reviewer and the coding agent costs tokens. Here’s what keeps the number of rounds down:
- Say everything in the first round. The reviewer checks every way into the code that’s changing and every way out of it, so problems don’t trickle out one round at a time.
- Only send back what must be fixed. The reviewer is there to catch regressions and problems that make a material difference, not to polish the code. Some code quality issues matter, but minor details don’t. A coding agent tends to act on every finding it’s given, even a minor suggestion that a person would let go, and that’s another push and another review. So once the reviewer says yes, anything that doesn’t block waits for a follow-up. So does a problem outside the scope of the change.
- Wait before re-reviewing. The re-review shouldn’t start the moment a push lands. Waiting a few minutes gives the coding agent time to fix everything from the review, so a quick follow-up push is the one that gets reviewed.
- Re-review only what’s changed and anything it could affect. The reviewer checks its open findings against the new push, and doesn’t raise a finding again if it’s still open.
- Cap the rounds. If the reviewer and the coding agent still can’t agree after a few rounds, it’s time for a person to look.
Cuts I Wouldn’t Make
Some cuts don’t save much, and others miss the regressions the review is there to catch:
- A shorter skill. Every review reads the reviewer’s instructions in full, so I expected trimming them to cut the cost. I reduced the size of the skill by about a third, but it barely made a difference. The cost of a review is the review itself.
- A smaller model. It’s cheaper, but smaller models tend to miss a lot more problems.
- A low effort level. Turning the effort right down saves money, but it tends to miss problems too. On a top model, it can do worse than simply using a smaller one.
The checking is what makes the review worth having, so it’s the part I want to keep.
Where I’ve Got To
I’m still early in this, so I can’t say yet how much it all adds up to. But the main thing I’ve learnt is that the cost of a good review is mostly the review itself. That’s the part I want to keep. What I can cut is the plumbing and the extra rounds around it.