The loop guard that looped
The prompt had a clear rule: never run a query you have already run. On one pull request, the model ran the same search fourteen times.
So I wrote a guard, in code, that catches a repeated tool call. Almost three weeks later, one review asked the same question 25 times. The guard was on, and it caught every one of them.
This is the first of the mistakes I promised in the overview of the PR agent. It is about the small piece of code between a model and its tools, and how I got it wrong twice.
Who ends the loop
The agent uses Spring AI. Like every agent framework, it gives you the tool loop.
The model asks for a tool. The framework runs it, adds the result to the conversation and calls the model again. The loop ends when the model answers with text.
So the model decides when it is over. We had two limits around that, and both were blunt. A gate could make 50 rounds of tool calls, and it had 15 minutes.
Each round also costs more than the one before. The whole conversation goes to the model again, with every earlier result in it. A loop is slow, and it gets more expensive as it goes.
Fourteen times
The first loop I read line by line was a search for one file. The model ran it, got its answer and ran it again with the same arguments. Then it did that twelve more times.
The prompt already said not to repeat queries. I could have written the rule in capital letters. But a model that ignores a rule will also ignore a louder copy of it.
So the rule had to stop being text. It had to be code, at the point where a tool is about to run.
The only way to interrupt a model
In the middle of a tool loop, the model is not listening to you. It asked for a tool, and it is waiting for the result. The result is the only thing it will read next.
So the guard answers the tool call itself. Before a call runs, it checks if the same tool already ran with the same arguments in this conversation. If it did, the tool does not run, and the guard writes the result:
assistant codeSearch {"query": "parseInvoice"}
tool No results.
assistant codeSearch {"query": "parseInvoice"}
tool DUPLICATE CALL — you already ran this exact query.
It returned: No results.
The answer will not change. Do not repeat it;
use it and move on.
For the model, this is a normal tool result. For us, it is a way to speak at the one moment it pays attention.
“The same arguments” means the same after sorting the keys. A model that sends {"a": 1, "b": 2} and then {"b": 2, "a": 1} is still repeating itself.
The check works call by call. If the model asks for three tools in one round and one is a repeat, the other two run as normal.
A guard with no memory
The obvious design is a cache: remember each call and its result, for each review. That needs a place to keep it and a moment to clean it.
It also saves the wrong thing. A cache saves the call to the tool, which is cheap. The loop is expensive because of the model rounds, and a cache does not stop those.
And the memory was already there. Every round, the framework hands over the whole conversation: each tool call the model made and each result it got. The guard reads that list and rebuilds everything it needs.
So the guard keeps nothing between rounds. Several gates go through it at the same time and cannot mix. When a review ends, there is nothing to clean up.
The same question in other words
An exact repeat is the easy case. A model also loops with small changes, and the first check sees each one as a new call.
So a second check turns every call into a set of words. It drops the arguments that only say where to look, like the project, the depth or the page. It lowercases the rest and splits it on anything that is not a letter or a digit.
| Call | Words left | Similarity with the first |
|---|---|---|
parseInvoice |
parseinvoice |
|
parseInvoice() |
parseinvoice |
1.0 |
.*parseInvoice.* |
parseinvoice |
1.0 |
The similarity is Jaccard: the words two sets share, divided by all the different words in both. At 0.6 or more, two calls count as the same question.
Two attempts at one question on one tool are fine. The third does not run. The model is told to try a shorter name, to switch to another tool, or to stop.
A real change of idea is left alone. Going from invoice to invoice parse total shares one word of three, so it is a new search. Asking a different tool starts from zero too.
I did not try to detect an empty result. There are about 25 tools, and each one says “nothing” in its own shape. The shape of the question was enough.
The check is crude. parseInvoice and invoice parser share no word, so it will not see that one. That is on purpose: a missed loop costs a few cents, and a search cut too early costs a worse review.
The guard that looped
Then came the review with 25 identical rounds. It spent almost seven minutes on them.
The logs were strange to read. The guard had caught all 25. Each time it skipped the tool, replayed the old result and told the model to move on, and each time the model asked again.
I had treated a skipped tool as free. It is not. The guard answers in a millisecond, but its answer goes back to the model.
That is a full round, with the whole conversation in it. The guard had stopped the cheap half of the loop and left the expensive half running.
And “do not repeat it” in a tool result worked as well as it had in the prompt. So the guard now gets louder each time, and then stops asking.
| The model asks for the same call | What happens |
|---|---|
| The first time | The tool runs. |
| The second time | The tool does not run. The old result comes back with a note. |
| The third time | The model is told to write its final answer now. |
| The fourth time | The loop ends. |
The reply that caused it
That fixed how long a loop could run. It did not explain why a model wanted the same answer 25 times.
The reason was in the tool. It looks for past incidents related to the change, and it drops the ones under a relevance score. When it dropped all of them, it returned an empty list, next to the counts of what it had found before dropping.
Read that as the model does: something like zero results, twelve matches. It looks like a fetch that failed half way. Trying again is the sensible thing to do.
The tool now answers that case in words, written for a model:
NO RELATED INCIDENT HISTORY. […] This is a normal and FINAL result:
the index is healthy, the query ran, and nothing relevant exists.
[…] Calling this tool again returns exactly this.
Its description got a new sentence too. It says that a second call returns exactly what the first one returned.
The loop was the model reacting to a confusing reply. I had spent weeks correcting the model, when the bug was in what we told it.
After that, I stopped treating tool text as log messages. The model reads names, descriptions and replies the way a client reads an API.
We already had a test for that, and that day I understood why. A snapshot file holds every tool with its description and its schema. Changing one sentence fails the build until someone updates the file on purpose.
The brake that was not connected
The same review showed one more thing. The last brake had never worked.
The round budget was 50. But 50 rounds do not fit in the 15 minutes a gate has, so the timeout always came first. All that time, the number in the configuration had protected nothing.
I cut it to 20. A day later I raised it to 60.
Those two days taught me what the budget is for. It does not stop loops, because the two checks do that long before. It is a net under them, for the day both miss.
At the limit, the tools stop running and the model is told to finish. Three rounds later, the loop ends anyway. Last week a new gate needed a tighter version, with fewer rounds and a time to finish by.
Not found is not absent
Every one of these brakes ends a search early. After that, the model still has to write something.
At first it wrote things like “this method has no callers” or “there are no tests for this”. It was the worst bug of all, and it did not look like one. A wrong comment in a confident voice is worse than a slow review.
The search had not shown that nothing was there. It had only stopped looking.
So the message that ends a search also says how to report it: not found in the indexed code, with low confidence. The model must never write that something does not exist.
When the model repeats our words
A fake tool result has one risk of its own. The model can copy it.
It happened. One gate came back with a summary that was only our own instruction, the one that says the tool budget is spent.
So that message starts with a marker that no real review would contain. Every answer goes through one function before it is published, and that function cuts everything from the marker on. If nothing is left, the gate reports that it could not assess the change.
Counting what the guard refuses
Each refused call is counted by reason: an exact repeat, a reworded one, the budget, the deadline.
Those counters are created at zero when the service starts. The moment that matters is when a line starts to move, and a counter that does not exist yet cannot show it.
A rising line is not good news. It means searches are being cut, and it does not mean they finished.
What I took from it
Three things, mostly.
A rule in a prompt is a suggestion. If something must never happen, write it in code, at the place where it would happen.
Stopping a tool is not stopping a loop. The guard has to be able to end the conversation, or it only makes the loop cheaper.
And when the model loops, read what the tool told it. Our worst loop started with a reply that a person would have found confusing too.