The model isn't failing. The session is. The context window becomes a graveyard of stale file dumps, dead-end reasoning, and endless chatter. The signal is simply drowned out by the noise of its own history.
I have found that the only way to combat this isn't through better prompting, but through an architectural pattern known as the Ralph loop.
The concept of the Ralph loop #
The term comes from Geoffrey Huntley, inspired by Ralph Wiggum from The Simpsons—the character who isn't exactly a genius but keeps showing up with relentless optimism. In its most primitive, bare-bones form, a Ralph loop is just a simple bash script:
while :; do
cat PROMPT.md | claude -p
done
That is literally it. You run the agent with the same prompt, but in a brand-new, pristine session, every single time. In each iteration, the agent wakes up with zero baggage. It reads a plan file, identifies the single most critical unfinished task, executes that task, writes its progress back to the disk, and then the process terminates. The loop then restarts it.
The brilliance of this approach lies in what it refuses to do: it refuses to try and maintain a single, long-lived "super-session." It treats a fresh context window as the most precious resource an agent possesses. Instead of trying to make one session smarter, you spend one full, clean window per unit of work, then you throw the session in the trash.
It functions like stochastic gradient descent for software development. Individual iterations might be noisy or even occasionally "dumb" (hence the name), but the direction remains consistent because the agent is always working from a clean slate.
Why context rot is an architectural problem #
We need to be specific about why this happens. It isn't that modern LLMs have small context windows; Claude and GPT-4o have massive ones. The real issue is that an agent's judgment degrades significantly before the window is even full. This is "context rot." Retrieval accuracy drops, earlier instructions lose their binding strength, and the model begins to treat its own previous errors as established ground truth.
You cannot prompt your way out of context rot because the prompt itself becomes part of the rotting context. To fix this, you have to follow a strict rule: State lives outside the model. Sessions are disposable.
The deterministic outer loop (your script or orchestrator) holds no logic. The stochastic inner agent holds all the reasoning, but only for the duration of a single task. The only things that survive the "death" of a session are the side effects: files written to disk, git commits, or updates to a TODO.md
file.
The dependency on external memory #
The weakness of a basic Ralph loop is that it is only as good as its external memory. If you are just using a PROMPT.md
file in a local folder, you will eventually run into issues where the plan drifts from reality, or the agent spends half its time re-litigating decisions made in previous loops because it can't remember why it chose a specific implementation.
To move from a "hack" to a professional AI workflow, you need a way to carry project state between these disposable sessions. I use a hosted MCP server called LLMBrain to act as this persistent "brain." It allows the agent to maintain a high-level understanding of the project state that survives the loop, ensuring that even though the session is fresh, the intelligence is cumulative.
If you are building a real-world deployment of a coding agent, stop trying to build a "smarter" long-running agent. Build a relentless loop instead.
an AI side-hustle playbook, with plenty of directly applicable cases.