Why Retrying Fails: Context Contamination in LLM Agent Pipelines
This paper introduces the Context-Contaminated Restart Model (CCRM) to formally quantify how failed LLM agent attempts contaminate subsequent retries by elevating error rates, deriving theoretical bounds on success probabilities and optimal pipeline depths that are empirically validated against real-world SWE-bench data.
Original paper licensed under CC BY 4.0 (http://creativecommons.org/licenses/by/4.0/). This is an AI-generated explanation of the paper below. It is not written or endorsed by the authors. For technical accuracy, refer to the original paper. Read full disclaimer
The Core Problem: The "Bad Memory" Effect
Imagine you are trying to solve a complex puzzle, like fixing a broken piece of code. You ask an AI assistant to help.
- Attempt 1: The AI tries to fix it but makes a mistake.
- Attempt 2: You tell the AI, "That didn't work, try again."
In a perfect world, the AI would forget the mistake and start fresh. But in reality, the AI remembers the mistake. The failed attempt is still sitting in its "conversation history" (its context window). Because the AI sees its own previous error, it gets confused or stuck in a loop, making it more likely to fail the second time than the first.
The authors call this "Context Contamination." It's like trying to clean a muddy floor, but every time you wipe it, you leave a little more mud behind, making the next wipe harder.
The Solution: A New Math Model (CCRM)
The researchers created a new mathematical model called the Context-Contaminated Restart Model (CCRM) to explain exactly how this happens and how to fix it. They treat the AI's retry process like a game with specific rules:
- The Pipeline: The AI doesn't just "fix" the code in one go; it breaks the job into a chain of small steps (like a pipeline). If any step fails, the whole attempt fails.
- The Contamination:
- Attempt 1 (Clean): The AI has a standard chance of failing (let's say 10%).
- Attempt 2+ (Contaminated): If Attempt 1 fails, the AI is now "contaminated." Its chance of failing jumps up (maybe to 30% or higher) because it's carrying the baggage of the previous mistake.
The Five Big Discoveries
The paper proves five main things about this "bad memory" effect:
1. The Exact Formula for Success
They figured out a precise math equation to predict the odds of the AI finally succeeding within a certain number of tries.
- Analogy: It's like knowing exactly how many times you need to roll a die to get a "6," but knowing that every time you fail, the die becomes slightly "loaded" (weighted) against you.
2. The "Cascade" Overhead
Because of the contamination, you need many more attempts to succeed than you would if the AI had a clean memory.
- Analogy: If you were climbing a ladder, a clean restart is like stepping back down and starting over. A contaminated restart is like trying to climb the same ladder, but every time you slip, the rungs get slippery and harder to grip. You end up slipping many more times than necessary.
3. The Sweet Spot for Task Size
The paper asks: "If we have a limited budget of 'tries' (computing power), how many steps should we break the task into?"
- The Finding: There is a perfect middle ground. If the task is too long (too many steps), one slip ruins the whole thing. If it's too short, you waste time restarting too often. The math tells you the exact "sweet spot" length for the task to maximize success.
4. The Theoretical Limit
They proved that no matter how smart your strategy is, you cannot beat the limits set by this contamination. You simply cannot succeed with fewer attempts than their model predicts without clearing the memory.
5. The Power of "Clearing the Slate"
The most practical finding: If you wipe the AI's memory before it tries again, it works much better.
- Analogy: If you are stuck in a traffic jam, trying to drive through the same jam again won't help. But if you take a different route (clearing the context), you avoid the traffic entirely. The math shows that clearing the context before a retry is always the best move.
Real-World Proof: The "SWE-bench" Test
The researchers tested their theory on real data from a benchmark called SWE-bench (where AI tries to fix software bugs).
- The Old Way (IID Model): Previous theories assumed that if an AI failed once, it had the same chance of failing the next time as it did the first time (like flipping a fair coin).
- The Reality: The old theories were way too optimistic. They predicted the AI would succeed about 98.6% of the time after three tries. In reality, it only succeeded 81.2% of the time.
- The New Way (CCRM): The new model predicted the success rate with almost perfect accuracy (error less than 0.1%).
The Takeaway
When an AI fails a task and you ask it to "try again," it often fails again because it's haunted by its own previous mistakes.
- Don't just retry blindly.
- Do clear the context. The paper proves that resetting the AI's memory before a retry is the single most effective way to save computing power and get better results.
The authors also note that if you can train the AI to make fewer initial mistakes, the benefit is huge because it prevents the "contamination cascade" from ever starting.
Drowning in papers in your field?
Get daily digests of the most novel papers matching your research keywords — with technical summaries, in your language.