Originally published on Loop & Retry — field notes on building LLM agents that survive production.
The instinct when a tool call fails or an output is wrong is to retry the same way you'd retry a flaky network call: catch it, tell the model what went wrong, resend. That's correct for a genuinely transient failure. It's the wrong move for a failure caused by the model's own reasoning, because "resend" doesn't mean "give it a clean second attempt" — it means "give it a context window with the wrong answer already sitting in it, and ask it to do better." A model doing next-token prediction over a transcript that already contains one confident, wrong attempt is anchored on that attempt, not liberated from it.
I think of this as context contamination: the failed path isn't just a wasted turn, it's now part of the evidence the next turn conditions on. The context window is a cache — what's true for stale data is true here too, except the thing going stale is your own bad move, and you're the one who put it back on the shelf.
Why the naive retry backfires
Picture an agent asked to extract a date from a messy log line, using a regex tool call. First attempt: \d{2}/\d{2}/\d{4}, which misses the line's actual 2026.07.13 format. The naive retry loop appends a tool-result turn ("no match found") and a nudge ("that didn't match — try again"), then re-calls the model with the full transcript, including the original wrong regex, still sitting there in plain view.
What tends to happen next isn't a fresh attempt. It's a near-duplicate of the first: \d{2}\/\d{2}\/\d{4} with an escaped slash, or \d\d/\d\d/\d\d\d\d with the quantifiers spelled out, or the same pattern with \s* sprinkled around it. The model isn't ignoring the feedback — it's doing the statistically reasonable thing given a context where the most recent, most salient prior turn is "here is a plausible-looking regex for this problem," which is exactly the anchor you didn't want it anchored to. This is a cousin of loop drift: drift is the agent convincing itself it's making progress across many turns; contamination is the narrower case where one specific wrong turn keeps re-asserting itself because you never took it out of the window.
Here's a small illustration of the mechanism, not a claim about real pass rates — the point is qualitative, not a benchmarked number:
# Illustrative only: shows the SHAPE of the failure, not a measured pass rate.
naive_retry_messages = [
{"role": "user", "content": "extract the date from: ERR 2026.07.13 timeout"},
{"role": "assistant", "content": "tool_call: regex(pattern=r'\\d{2}/\\d{2}/\\d{4}')"},
{"role": "tool", "content": "no match"},
{"role": "user", "content": "that didn't match. try again."},
]
# The model's next completion is conditioned on ALL four turns above —
# including its own wrong regex, which is the strongest recent signal
# in the window about "what a plausible answer looks like here."
The transcript is doing the opposite of what you want a retry to do. You wanted the model to reconsider the date format. What it actually saw was: task, an example of a kind of answer, a terse rejection, and an instruction to produce another one of those. Nothing in that shape points away from the failed pattern's neighborhood.
Continue reading: the rest of this post — including the sections that follow — is on Loop & Retry: read the full article
Top comments (0)