Edu Sanchez

The same code review, 78% cheaper. And better.

Our PR agent spent $5.40 to review five real pull requests. One agent in opencode, running in a sandbox, did the same five reviews for $1.20.

That is 78% less, with the same model and through the same gateway. The reviews were also better, and they took a third of the time.

This is the next step I promised in the overview: moving the review into the sandboxes. I expected it to save a little. I did not expect it to save four dollars out of every five.

Four dollars out of five

Here is every review. The costs come from the gateway’s spend log, call by call.

Pull request PR agent opencode Saved
A Java service that moves to a new library version $0.61 $0.12 80%
New fields on Kafka messages, in Kotlin $0.83 $0.24 71%
A Java library that starts publishing to Kafka $1.55 $0.17 89%
The Java service that consumes those messages $1.35 $0.39 71%
A React page to assign events to templates $1.05 $0.28 73%
Total $5.40 $1.20 78%

opencode was cheaper on every pull request. On average, a review went from $1.08 to 24 cents. The biggest saving was on the biggest review, where the library change went from $1.55 to 17 cents.

Put another way, for every $100 the PR agent spends on reviews, opencode needs $22. The PR agent reviews pull requests all day, so that difference adds up fast.

The money comes from doing less work. To review those five pull requests, the PR agent made 197 calls to the model and sent it 12.4 million input tokens. opencode made 54 calls and sent 3.2 million.

How I tested it

I triggered the bot by hand on five open pull requests, one at a time. Right after, I ran opencode in a sandbox on the same diff. Both used Kimi K2.7 through our gateway.

The setup was one agent, the Semantic Graph tools and no copy of the repository. Semantic Graph already knows the code, so the agent asks it instead of reading files. It got what the bot gets: the pull request, the Jira ticket, the diff, the Tech Radar, the past incidents and our testing rules.

Why six gates cost more

The PR agent reviews with six gates. The sixth is new since the overview, and it checks the change against the documents that describe the service. opencode got one set of instructions that covers all six.

Each gate is its own conversation with the model. Each one builds its own context from zero, so the diff, the ticket and many of the lookups are paid for several times.

Every round of a conversation also sends the whole conversation again. A gate that goes round ten times pays for its context ten times.

Where I thought the money was

My guess was the cache. Each gate starts with its own system prompt, so the six share nothing. One conversation sends the same beginning every time, so almost all of it would come from the cache.

The gateway said otherwise. 60% of the bot’s input already came from the cache, so a perfect cache would have cut the bill by a third at most.

A cached token is cheaper, but it is not free. And the output is never cached. So the real saving had to come from fewer rounds.

The first try saved eleven cents

The first opencode review was a small dependency bump. It cost $0.39, and the bot had spent $0.50 on the same pull request.

It made 36 calls to the model, four more than the bot. Most of its input came from the cache. But its conversation grew to 78,000 tokens, and every call read all of it again.

It also put its comments on the wrong lines. It counted lines in the diff file, not in the code, so a comment for line 45 of a pom landed on line 41. The bot gets a diff with the real line numbers written in, and from then on the agent got the same one.

Work in rounds

The biggest fix was one paragraph in the instructions:

Every message you send re-reads the whole conversation, so the number
of messages is the cost. Work in rounds, and never trade a finding for
a saved round.

The agent plans first. Its first message holds every lookup the plan needs, as parallel tool calls. Its second message holds all the follow-ups, and most reviews need nothing more.

On the same pull request, the next version made 6 calls instead of 36. It cost $0.18.

That version also had fewer tools, because I thought fewer tools would mean fewer wrong calls. It was the wrong idea. The PR agent’s prompts do not take tools away. They say when to use each one.

So the final instructions keep every Semantic Graph tool and add a table. Each row is a question, the tool that answers it and its exact parameters. The bar for a finding came from the PR agent’s prompts too:

Precision matters far more than recall. A review that flags nothing is
a correct review when nothing is broken.
[…]
If you suspect a problem, confirm it with a tool or drop it.

That review cost $0.16, a third of the bot’s. It passed every gate with no inline comments. The bot had posted a blocking comment about a test resource that might be missing. The agent could not show it was missing, so it said nothing.

The limit that cost money

Then I tried to squeeze it further, with a limit of five steps. All five reviews used exactly five, and the cost fell by another 70%.

The reviews did not survive it. One never got written, because the agent ran out of steps and asked to be run again. Another lost its best finding.

The last step was also the most expensive one. To force a final answer, opencode removes the tools. That changes the prompt, so the cache misses and the last step pays full price for the whole conversation.

It is the lesson of the loop guard, from the other side. A step limit does not save money. It is a net for loops, and a wrong PASS costs far more than ten cents of tokens.

The limit is now 25. No review reached it, and the longest used 14.

Less to read

The rest of the saving came from the input. Every token in the first message is read again in every round, so cutting it pays several times.

  • Test files arrive as their imports and the names of their tests. On the React pull request, test bodies were 15,000 of 39,000 tokens.
  • The Tech Radar is cut down to the technologies the diff mentions.
  • The testing rules went from about 6,000 tokens to 400.
  • A script removes past tickets that are not incidents, by category, resolution or cause. It dropped 11 of 59 before the model saw them.

Measured where the bill is

To get the bot’s real cost, I compared its own records with the gateway. Over one week, the table where the bot logs each gate run had 4.5 times fewer input tokens than the gateway billed.

So the bot had been showing about a fifth of what it really sent. That is why every number here comes from the gateway, which is where the bill comes from.

Three times faster

The five reviews took the PR agent 44 minutes, between 4 and 15 minutes each. opencode took 15 minutes, between one and a half and four and a half each.

I did not expect that. The gates run in parallel and the agent runs alone. But one conversation with a few rounds finished before six that each went round many times.

And better reviews

Cheaper would have been enough. The reviews were also better.

The bot posted one wrong inline comment across the five reviews, and opencode posted none. That comment was about a Kafka DELETE that a change moved inside a new lookup. The bot said that when the sport is missing, the message is lost, and I agreed with it at first.

So a script now marks every call that was removed and added again inside a new condition. With that mark, the agent read the check around the send. It already skipped the DELETE when the sport was missing, before the change and after it, so nothing was lost.

A good review does that too. It does not only find suspects. It also clears them.

Two of the pull requests were one feature: the library that publishes the new Kafka message, and the service that consumes it. Semantic Graph indexes the main branch. So while reviewing the service, the classes from the library were not there yet.

The bot read that as missing work, and its Jira gate marked the consumer as unmet. But Jira knows every pull request linked to a ticket. A script now passes that list to the agent, which credited the work to the other pull request and reported a merge-order dependency instead.

There was less noise, too. The bot’s incident gate warned on all five pull requests, with up to eight loosely related tickets. The agent got the filtered list and one more rule: sharing a technology like Kafka is not a link. Its incident gate passed on all five.

And it found more of what matters. It asked for contract tests on all four changes that send data to another service, where the bot asked on three. On the React page, both saw that the frontend sends fields the backend does not accept yet. Only opencode saw that the frontend also reads a field the backend does not have.

What it does not prove yet

Five pull requests is a small sample. They all come from one vertical, each one ran once, and I judged the quality myself, by reading every diff and both reviews.

The runs also vary. On the React pull request, one run read the backend’s request classes and showed that they reject unknown fields, so saving would fail. The next run read the same classes and did not say it.

And this is not in production. Nobody gets these reviews yet.

What comes next

The plan keeps most of the PR agent. It still receives the webhook, prepares the input and posts the comment. The sandbox runs opencode with the instructions and hands back the review.

Before that, the same test again: 15 to 20 pull requests from several verticals, two runs each.

What I took from it

The cost of an agent is its number of rounds. Ask for everything you need in one message, and the bill follows.

A step limit is not a budget. If it ever cuts a review short, it costs more than it saves.

And anything a script can decide, the model should not have to. Every token you hand it is read again in every round.

All posts