Large Language Models (LLMs) have proven to be surprisingly capable agents, but they often stumble when a task requires more than a few steps. If you ask an LLM to cook a complex meal in a simulator, it might start strong but eventually get "lost in the weeds"—forgetting the ultimate goal because the initial instructions have drifted out of its active context window.
A new paper introduces ReCAP (Recursive Context-Aware Reasoning and Planning), a framework designed to give LLM agents the spatial awareness and memory they need to handle complex, multi-step problems without losing their way.
TL;DR
ReCAP is a hierarchical prompting framework that allows LLM agents to tackle long-horizon tasks by recursively decomposing goals into subtasks while maintaining a shared, managed context. By "re-injecting" high-level plans back into the model's immediate view after subtasks finish, ReCAP achieves up to a 32% performance boost over standard methods on complex reasoning benchmarks.
The Problem: Context Drift and Infinite Loops
Current LLM agents typically use sequential prompting (like the popular ReAct framework), where they think and act in a linear chain. For long tasks, this leads to "context drift." As new observations pile up, the original high-level goal is pushed out of the model's limited context window. The agent effectively develops a goldfish memory, leading to repetitive failures or infinite loops.
Existing hierarchical methods try to fix this by breaking tasks into sub-problems, but they often isolate these sub-problems into separate context windows. This fragments information, making it hard for the agent to coordinate between a high-level strategy (e.g., "make a sandwich") and low-level execution (e.g., "pick up the knife").
How ReCAP Works
ReCAP introduces a recursive architecture that operates within a single, shared LLM context. It relies on three core mechanisms:
- Plan-Ahead Decomposition: Instead of generating one step at a time, ReCAP generates a full list of subtasks. It executes the first item and keeps the rest of the plan "in mind" for later refinement.
- Structured Context Re-injection: When a subtask (a "child" node) is completed, the framework explicitly re-inserts the parent’s original plan and thoughts back into the active context. This ensures the high-level intent is always proximal to the current decision point.
- Memory-Efficient Execution: To prevent the context from exploding, ReCAP uses a sliding window. Because it uses a recursive tree structure, the amount of critical state information scales linearly with the depth of the task tree, rather than the total number of steps taken.
Key Insights and Performance
The authors evaluated ReCAP across four diverse benchmarks using a strict pass@1 protocol (no retries allowed), including Robotouille (a cooking simulator), ALFWorld (household tasks), FEVER (fact-checking), and SWE-bench Verified (GitHub issue resolution).
ReCAP significantly outperformed the baseline (ReAct) across the board, especially as tasks became longer and more complex:
- Robotouille (Synchronous): Achieved 70% success, a 32% absolute gain over ReAct.
- Robotouille (Asynchronous): Achieved 53% success, a 29% absolute gain over ReAct.
- SWE-bench Verified: Solved 44.8% of issues, the highest success rate among the compared methods using GPT-4o.
Why does it work so well? * Context Organization > Context Size: Simply having a massive context window isn't enough due to the "lost in the middle" phenomenon. By strategically re-ordering and re-injecting the high-level plan, ReCAP keeps the model focused on the "why" behind the "what." * Backtracking as a Safety Net: Unlike sequential agents that get stuck when an action fails, ReCAP’s recursive structure allows it to backtrack to a parent node. The parent can then evaluate the failure and revise the remaining plan. * Zero-Shot Generalization: ReCAP doesn't require fine-tuning. It works as a prompting wrapper, making it compatible with state-of-the-art models like GPT-4o, Llama-3, and DeepSeek-V3.
Limitations and Future Work
While powerful, ReCAP delegates all decomposition and backtracking logic to the LLM. If the underlying model is too weak to follow complex instructions, the framework can still fail. Additionally, because ReCAP involves more "thinking" steps and plan refinements, it results in more LLM calls, increasing cost and latency (e.g., roughly three times more expensive than ReAct on Robotouille).
Future directions could include modularizing the architecture—perhaps using a large, expensive model for high-level planning and a smaller, faster model for low-level primitive actions—or moving from a linear dialogue history to a more complex graph-based memory.
Summary updated