Real-world tasks require decisions at varying granularities, and humans excel at this by leveraging a unified cognitive representation where planning is fundamentally understood as a high-level form of action. However, current Large Language Model (LLM)-based agents lack this crucial capability to operate fluidly across decision granularities. This limitation stems from existing paradigms that enforce a rigid separation between high-level planning and low-level action, which impairs dynamic adaptability and limits generalization. We propose ReCode (Recursive Code Generation), a novel paradigm that addresses this limitation by unifying planning and action within a single code representation. In this representation, ReCode treats high-level plans as abstract placeholder functions, which the agent then recursively decomposes into finer-grained sub-functions until reaching primitive actions. This recursive approach dissolves the rigid boundary between plan and action, enabling the agent to dynamically control its decision granularity. Furthermore, the recursive structure inherently generates rich, multi-granularity training data, enabling models to learn hierarchical decision-making processes. Extensive experiments show ReCode significantly surpasses advanced baselines in inference performance and demonstrates exceptional data efficiency in training, validating our core insight that unifying planning and action through recursive code generation is a powerful and effective approach to achieving universal granularity control. The code is available at https://github.com/FoundationAgents/ReCode.
翻译:现实世界中的任务需要在不同粒度上做出决策,人类通过利用统一的认知表征——其中规划本质上被理解为一种高层级行动形式——而擅长于此。然而,当前基于大语言模型(LLM)的智能体缺乏这种在不同决策粒度间流畅操作的关键能力。这一局限性源于现有范式强制将高层级规划与低层级行动严格分离,这损害了动态适应性并限制了泛化能力。我们提出ReCode(递归代码生成),这是一种新颖的范式,通过在单一代码表示中统一规划与行动来解决此限制。在该表示中,ReCode将高层级计划视为抽象占位函数,随后智能体递归地将其分解为更细粒度的子函数,直至达到原始行动。这种递归方法消除了规划与行动之间的刚性边界,使智能体能够动态控制其决策粒度。此外,递归结构本身会生成丰富的多粒度训练数据,使模型能够学习分层决策过程。大量实验表明,ReCode在推理性能上显著超越先进基线,并在训练中展现出卓越的数据效率,验证了我们的核心见解:通过递归代码生成统一规划与行动是实现通用粒度控制的一种强大且有效的方法。代码可在 https://github.com/FoundationAgents/ReCode 获取。