Large language model (LLM) agents are fundamentally constrained by their context windows when tackling long-horizon tasks like deep research or software engineering. As interaction histories grow, agents suffer from performance degradation and massive computational overhead. To overcome this, researchers have introduced Context Folding, a framework that empowers agents to actively manage their working memory by procedurally branching into sub-tasks and "folding" the results into concise summaries.
Trained via a novel reinforcement learning framework called FoldGRPO, the resulting agent matches the performance of long-context baselines while using an active context up to 10x smaller.
The Long-Context Bottleneck
As agents execute complex, multi-step workflows, their context grows linearly. This creates two major bottlenecks:
- Performance Degradation: LLMs struggle to retrieve relevant information from massive contexts, often falling victim to the "lost in the middle" phenomenon.
- Computational Inefficiency: Long contexts lead to quadratic increases in attention computation and massive KV-cache overhead.
Existing solutions typically rely on passive context management, such as post-hoc summarization (which can disrupt the agent's reasoning flow) or hardcoded multi-agent architectures (which are rigid and difficult to generalize). The authors argue that context management should instead be a learned cognitive skill, allowing the agent to decide when to offload details and keep its main reasoning thread clean.
Context Folding: Active Memory Management
The Context Folding framework introduces two new agentic actions that enable active context management:
branch(description, prompt): The agent pauses its main reasoning thread to initiate a localized sub-task within a fresh, empty context.return(message): Upon completing the sub-task, the agent summarizes the outcome. The intermediate steps of the branch are "folded" (discarded from the active context), leaving only the concise summary in the main thread.
Because the agent "rolls back" to a previous state after a branch, this method is highly KV-cache friendly. The system does not need to re-process the entire history; it simply resumes inference from the branch point, integrating the new summary.
Training with FoldGRPO
To teach the agent how to effectively use these new actions, the researchers developed FoldGRPO (Folded-context Group Relative Policy Optimization). This end-to-end reinforcement learning framework uses specific process rewards to guide the agent's behavior:
- Unfolded Token Penalty: Penalizes the agent if the main thread becomes too long, forcing it to learn to branch for token-heavy operations like web searching or codebase exploration.
- Out-of-Scope Penalty: Utilizes an external judge model (e.g., GPT-5-nano) to ensure the agent stays focused on the specific sub-task assigned to a branch, preventing drift.
- Failure Penalty: Discourages unproductive or failed tool calls.
The initial RL training pool was created by collecting trajectories using a base model (Seed-OSS-36B-Instruct) on datasets like SWE-Gym and SWE-Rebench.
Key Results and Emergent Behaviors
The researchers evaluated the "Folding Agent" on two challenging long-horizon benchmarks: BrowseComp-Plus (deep web research) and SWE-Bench Verified (real-world software engineering).
- Superior Efficiency: Operating on a strict 32K active token budget, the agent achieved a Pass@1 of 62.0% on BrowseComp-Plus and 58.0% on SWE-Bench Verified.
- Outperforming Long-Context Giants: The Folding Agent surpassed baseline models equipped with massive 327K context windows, demonstrating that a smartly managed small context outperforms a passively filled large one.
- Massive Compression: The agent achieved over 90% context compression, successfully reducing a 107K-token trajectory to just 6K tokens while retaining all critical reasoning.
- RL Impact: Training with FoldGRPO yielded an absolute improvement of 20.0% on BrowseComp-Plus compared to standard RL approaches.
Through RL, the agent developed an emergent "plan-then-execute" strategy. It learned to reserve the main thread for high-level strategy while delegating "dirty work" (like searching or debugging) to branches. Furthermore, the agent exhibited remarkable length generalization: though trained on tasks requiring at most 10 branches, it adaptively utilized over 30 branches to solve complex queries containing up to 50 combined questions.
Limitations and Future Directions
While Context Folding represents a significant leap in agent efficiency, the current implementation is limited to single-layer folding. Multi-layer or nested folding (branches within branches) remains a promising but difficult next step, as current models struggle with stable nested control flows.
Additionally, the training pipeline relies on an external LLM judge for the Out-of-Scope penalty, introducing dependencies on the judge's accuracy and cost. Finally, the benefits of folding are most pronounced in "depth-first" tasks; tasks requiring massive parallel breadth may require different execution configurations in the future.
Summary updated