Fighting "Lost in the Middle"

Fighting "Lost in the Middle"

Long-running AI agents have a well-documented failure mode: the further a piece of information sits from the start or end of the context window, the less reliably the model attends to it. This is the "lost in the middle" effect, and it's a big reason agents drift off-task after dozens of tool calls, the original goal gets buried under transcript.

The fix that's proven out in production agents is called recitation. Manus's coding agent maintains a todo.md file that it rewrites every few steps: checking off completed items, restating what's left. Claude Code does the same thing internally with its own todo-list state. The mechanism matters more than the tool. By rewriting the objective and progress list on each pass, the agent effectively re-injects its goal at the end of the context window, exactly where model attention is strongest, instead of leaving it to rot near the top of a long transcript.

This is worth building into your own agent loops even without a framework. If you're running a custom agent script, a LangChain graph, or anything with more than a handful of sequential tool calls, add one deliberate step: have the agent write a short state file each iteration: current goal, completed steps, next action. Then feed that file's contents back in near the top of the next turn's prompt. LangChain's Deep Agents project found the effect is strongest on longer, multi-step tasks and with less capable models; for short tasks it matters less.

It's a cheap technique, no new infrastructure, just a habit of re-stating the goal and it meaningfully cuts down on the kind of goal drift that makes long agent runs unreliable.

References

MarkTechPost. (2026, September 12). Context engineering inside the harness: 4 mechanisms that beat context overflow and goal loss on long-horizon tasks. MarkTechPost. https://www.marktechpost.com/2026/09/12/context-engineering-inside-the-harness-4-mechanisms-that-beat-context-overflow-and-goal-loss-on-long-horizon-tasks/

Anthropic. (2025, September). Effective context engineering for AI agents. Anthropic Engineering Blog. https://www.anthropic.com/engineering/effective-context-engineering-for-ai-agents