Why AI Agents Lose the Plot on Long Tasks — and the 4 Fixes That Stop It
Ever had an AI agent start a task perfectly, then drift halfway through — forgetting the original goal, repeating steps, or losing track of a file it created ten minutes ago? A recent breakdown of how modern agent harnesses actually work explains why that happens, and the four mechanisms serious agent builders use to stop it.
The Real Bottleneck Isn't the Model — It's the Context Window
Every AI agent has a fixed amount of "working memory" — the context window. Long tasks generate a lot of stuff: tool outputs, file contents, error messages, intermediate results. Cram all of it into the context window and two things go wrong at once: the window fills up, and the original goal gets buried under all the noise. The agent doesn't get dumber. It just loses the thread.
Here's how the harnesses behind tools like Claude Code and Deep Agents actually solve this.
1. Context Budgeting and Offloading
Instead of letting everything pile into context, good harnesses set hard limits. Deep Agents writes any tool response over 20,000 tokens straight to disk, keeping only a file path and a short preview in context. Once a session hits 85% of the model's window, older tool calls get swapped out for pointers to where their content actually lives.
Claude Code does something similar: auto-memory is capped at 200 lines or 25KB, tool schemas load only when actually needed, and large files get referenced by path instead of pasted in full on a re-read.
The idea in both cases is the same — keep the pointer in context, not the payload.
2. Compaction
When offloading alone isn't enough, the harness compresses the conversation itself. Claude Code's compaction step summarizes what's happened so far, keeping architectural decisions and unresolved issues while cutting the redundant back-and-forth, then re-reads a handful of the most recently touched files to restore working context.
Deep Agents takes a more structured approach — a summary with dedicated fields for what the session was trying to do, what got built, and what's next — while the full transcript stays saved to disk in case anything needs to be recovered later.
3. Todo-State and Recitation
One of the simplest fixes turns out to be one of the most effective: keep a running todo.md that gets checked off step by step. Repeating the objective back to the model — recitation — keeps the actual goal in its "recent attention," instead of letting it drift toward whatever happened three tool calls ago.
Interestingly, it's not universally a win. Deep Agents' latest version made the todo tool optional after testing showed slightly better results with it turned off for some tasks — though it's still the recommended default for anything long and multi-step.
4. Memory Strategy Across Sessions
The fourth mechanism is the most counterintuitive: memory itself has a cost. Persistent context — project docs, notes from past sessions — gets reloaded automatically after a reset, which sounds like a pure win. But a recent ETH Zurich study found that LLM-generated context files actually increased inference costs by 20–23%. More memory isn't free; it's more tokens the model has to read and weigh on every single turn.
The fix isn't "remember everything." It's remembering selectively — keeping persistent notes under roughly 200 lines and cutting anything that isn't pulling its weight.
What This Means If You Use OpenClaw
This is exactly the tradeoff OpenClaw is built around. Persistent memory is genuinely useful — it's why your agent doesn't restart from zero every session — but memory that isn't managed carefully becomes its own liability, bloating context and slowing everything down.
That's why OpenClaw's memory is built to track actual work done — tools used, tasks completed, results produced — rather than dumping raw conversation history back into context on every run. It's the same instinct behind context budgeting and compaction: keep what matters, offload what doesn't, and don't make the agent re-read its own history just to figure out what to do next.
The Bigger Picture
The agents that hold up over long, multi-step tasks aren't the ones with the biggest context windows. They're the ones with the most disciplined harnesses — the ones that know what to keep, what to offload, and when remembering more actually makes things worse.
If you want to see agent memory built this way in practice — not just theorized about — that's exactly what OpenClaw tutorials are for.