TL;DR
ReCode (Recursive Code Generation) is a new paradigm for LLM agents that treats high-level planning and low-level execution as a single, unified process. By representing plans as abstract Python "placeholder" functions that recursively expand into executable code, ReCode allows agents to dynamically adjust their decision-making granularity, resulting in superior performance, 80% lower API costs, and massive gains in training data efficiency.
What Problem Does This Paper Address?
In the real world, humans don't just "plan" and then "act" in two separate boxes. We operate across a spectrum of granularity. When you decide to "make breakfast," you don't immediately think about the individual muscle contractions needed to crack an egg; you decompose the high-level goal into sub-tasks (fry eggs, toast bread) and only focus on the "primitive" motor actions when necessary.
Current Large Language Model (LLM) agents struggle with this. Most follow one of two rigid paths:
- Step-by-step reasoning (e.g., ReAct): The agent thinks and acts one tiny step at a time. This lacks strategic foresight for long-horizon tasks.
- Separate Planner Modules: A high-level planner generates a full script, and an executor carries it out. This is brittle; if the environment changes mid-plan, the agent often fails.
The authors identify this "rigid separation" as the core bottleneck. Agents need a way to fluidly move between abstract strategy and concrete execution.
Methods
The authors propose ReCode, which unifies plans and actions into a single code representation.
- Recursive Decomposition: Every task starts as a root function call (e.g.,
solve()). If a task is complex, the LLM generates a code block containing "placeholder functions"—abstract sub-goals likefind_and_take(obj). - The Expansion Loop: The system traverses this decision tree depth-first. When the executor hits a placeholder function, it triggers a recursive call to the LLM to "expand" that function into more specific sub-functions or primitive actions (actual environment commands like
run('open fridge')). - Unified Context Management: Instead of a messy, ever-growing chat history, ReCode uses a shared variable namespace. The model only sees the current variable state, forcing it to "save" important information into variables, which keeps the context window clean and focused.
- Self-Correction: If the generated code fails (syntax errors or runtime issues), ReCode uses a self-correction loop to re-invoke the policy with the error traceback, allowing the agent to recover mid-trajectory.
Datasets
The researchers evaluated ReCode across four diverse environments:
- ALFWorld: An embodied household simulator for daily chores.
- WebShop: A complex online shopping environment requiring search and comparison.
- ScienceWorld: A scientific laboratory setting for conducting experiments.
- ManiSkill: A robotic manipulation benchmark requiring continuous control and fine-grained pose adjustments.
Key Insights and Ideas
- Plans are just "Pseudo-Code": The central "aha moment" is that a plan isn't a different kind of thought; it's just a higher-level action. By using code as the medium, the agent can treat a "plan" as a function call that hasn't been defined yet.
- Hierarchical Data is High-Signal: The recursive structure naturally produces "decision trees" rather than flat action sequences. The authors found that training on this hierarchical data is much more efficient. Even with fewer total training samples, the model learns the underlying logic of task decomposition much faster.
- Dynamic Granularity: Because the recursion only stops when it hits a primitive action, the agent can choose to be as abstract or as detailed as the situation demands. It doesn't waste "thinking tokens" on simple steps but can dive deep into complex sub-problems.
Key Results
- Superior Performance: ReCode achieved an average reward of 60.8 across benchmarks (using GPT-4o mini), outperforming the standard ReAct baseline (47.4) and CodeAct (43.9).
- Extreme Cost Efficiency: ReCode is remarkably cheap to run. On average, its trajectories cost 78.9% less than ReAct and 84.4% less than CodeAct. This is because hierarchical decomposition allows for shorter reasoning paths and fewer tokens per task.
- Training Efficiency: In Supervised Fine-Tuning (SFT) experiments, ReCode achieved better results than ReAct while using 3.7x fewer training pairs.
- Embodied Success: On the ManiSkill robot tasks, ReCode (using Gemini 2.5 Flash) achieved a 100% success rate on the PullCube task, whereas the ReAct baseline struggled at 30%.
Limitations and Future Work
While powerful, ReCode's effectiveness is tied to the underlying LLM’s coding ability. If a model isn't great at following specific Python formats or struggles with logical decomposition, the recursive structure can break down.
The authors suggest that future work should focus on:
- Targeted Pre-training: Training models specifically to understand the ReCode recursive framework.
- Reinforcement Learning: Using RL to reward agents for creating efficient hierarchical plans rather than just successful ones.
- Curriculum Learning: Gradually introducing more complex tasks to help the agent learn deeper decomposition patterns.
Summary updated