Matryoshka Agent: Unfolding Sub-Agents for Scalable Long-Horizon Machine Learning Engineering
Matryoshka Agent: Unfolding Sub-Agents for Long-Horizon Machine Learning Engineering
Matryoshka Agent proposes a hierarchical framework for long-horizon machine learning engineering (MLE) tasks, decomposing agentic problem solving into a three-layered structure: Orchestrator for strategic decision-making, Sub-Agents for execution, and Tools for mediation. The framework introduces a Solution Refinement Tree-based training paradigm with online ranking-based reinforcement learning to efficiently optimize the Orchestrator from costly environment interactions. Notably, Qwen3-4B-Instruct as an Orchestrator reaches performance comparable to o4-mini, and applying the method to Qwen3-30B-Coder yields up to 36.7% relative improvement.
TL;DR
Matryoshka Agent introduces a hierarchical framework that decouples high-level strategic decision-making (Orchestrator) from low-level code execution (Sub-Agents) via a standardized Tool interface. This design dramatically reduces long-context burden and enables efficient iterative refinement. The authors further propose a Solution Refinement Tree-based training paradigm with online ranking-based reinforcement learning. The result: a small 4B model as Orchestrator matches the performance of o4-mini, and a 30B coder model gains up to 36.7% relative improvement on the MLE-Dojo benchmark.
Position in the field: This work is a major step forward in modular agent design for machine learning engineering. It moves beyond monolithic agents and simple multi-agent role-playing, offering a principled, trainable hierarchy that separates exploration strategy from execution cost. It is both a practical engineering contribution and a strong empirical validation of hierarchical decomposition for long-horizon tasks.
Problem & Motivation
Machine Learning Engineering (MLE) tasks are a nightmare for current LLM agents. They require iterating over code, debugging, running experiments, and evaluating results over many rounds. Each round produces execution logs, error messages, and intermediate outputs that accumulate rapidly. A monolithic agent must process this entire noisy history to make the next decision. This leads to several core problems:
- Context Saturation: The agent's context window fills with low-level details, making it hard to maintain a strategic view of what has been tried and what should be tried next.
- Expensive Exploration: Each environment interaction (e.g., training a model on a large dataset) costs significant time and computation. Wasting attempts on poor directions is costly.
- Limited Model Capacity: A single model must simultaneously handle long-range reasoning, detailed code generation, and debugging—a challenging combination, especially for smaller models.
The authors' key insight is that these problems can be addressed by separating concerns: the strategic "what to refine" decisions should be made by a lightweight, persistent module (Orchestrator) that maintains only compact summaries, while the heavy "how to implement" work is delegated to disposable, fresh-context modules (Sub-Agents).
Methodology
The Matryoshka Agent Framework
The framework consists of three coordinated layers:
- Orchestrator: The persistent decision-making layer. It maintains a history of tool calls and responses (scores, summaries) but not raw code or execution logs. At each turn, it decides three things: which previous attempt to refine, what new instruction to write, and which previous attempts to use as references.
- Tools: The mediation layer. They translate the Orchestrator's high-level instruction into a fresh Sub-Agent context (including task spec, parent attempt, instruction, and references). After execution, they return a compact response (score and structured summary) to the Orchestrator.
- Sub-Agents: The execution layer. Each Sub-Agent is instantiated with a fresh context and performs a bounded code-debugging loop. It does not inherit the Orchestrator's history or other Sub-Agents' contexts. This keeps execution details local and prevents context pollution.
Key Design Principle: The Orchestrator reasons over outcomes (scores and summaries), not processes (code and logs). This dramatically reduces the growth rate of its context while preserving the information needed for strategic refinement.

Training Paradigm: Solution Refinement Tree + Online RL
Training a hierarchical agent for expensive MLE tasks is non-trivial. The authors propose an elegant solution:
- Solution Refinement Tree (SRT): Starting from a root state, the current Orchestrator policy samples multiple alternative refinement instructions. Each is executed by a Sub-Agent, producing a child node with a score. Repeating this builds a tree of score-annotated attempts. The authors use binary expansion for tractability.
- Preference Construction: Instead of using immediate scores, each branch is labeled with its downstream return (the best score in its subtree). This creates preference pairs that capture long-horizon value: a branch that initially scores low but leads to a high-scoring later attempt is preferred over a branch that scores high early but plateaus.
- Online Ranking-Based RL: The Orchestrator is optimized using a ranking objective that assigns higher probability to the preferred continuation. The loss is derived from an energy-based formulation of the optimal policy, with a KL-like log-ratio parameterization that anchors each update to the previous policy. Sub-Agents are improved via supervised fine-tuning on successful trajectories from the tree.
This training paradigm efficiently reuses costly execution data, provides contrastive signals from common prefixes, and jointly improves both levels of the hierarchy.
Experiments & Results
Setup
The authors evaluate on the MLE-Dojo benchmark (150 training tasks, 50 evaluation tasks) using HumanRank (fraction of human competitors outperformed). They test a wide range of model combinations, including open-source (Qwen3-4B, Qwen3-30B-Coder) and closed-source (o4-mini, GPT-5-nano) models.
Main Results
| Agent Framework | Model Configuration | HumanRank (All) |
|---|---|---|
| Dojo Agent | o4-mini | 0.4832 |
| Matryoshka Agent | o4-mini + self | 0.5465 |
| Matryoshka Agent | Qwen3-4B-RL + o4-mini | 0.5360 |
| Dojo Agent | Qwen3-30B-Coder | 0.3302 |
| Matryoshka Agent | Qwen3-30B-Coder-RL + self | 0.4515 |
Key findings:
- Inference-only gains: o4-mini improves from 0.4832 to 0.5465 simply by being used in the Matryoshka hierarchy (Orchestrator + self as Sub-Agent).
- Small model Orchestrator: Qwen3-4B (0.1878 as monolithic) reaches 0.5360 as Orchestrator with o4-mini Sub-Agents after RL training—nearly matching o4-mini's own Orchestrator performance.
- Large model full system: Qwen3-30B-Coder-RL (both layers) achieves 0.4515, a 36.7% relative improvement over the Dojo Agent baseline.
Plug-and-Play Capability
A trained Orchestrator generalizes to different Sub-Agents at test time. For example, Qwen3-4B-RL (trained with o4-mini) improves GPT-5-nano from 0.3946 to 0.4309 when used as Orchestrator.
Context Length Analysis

The Orchestrator's context grows at ~1202 tokens/round, vs. ~1870 for the monolithic Dojo Agent. Sub-Agents maintain constant context length. This means the Matryoshka Agent can support ~213 rounds vs. ~137 rounds for the Dojo Agent under a 256K context budget.
Ablation Studies
- Orchestrator vs. Sub-Agent contributions: When RL training is applied only to the Orchestrator (with untrained Sub-Agent), gains are smaller than when both are trained jointly, confirming the importance of co-evolution.
- RL without SFT: Direct RL without supervised fine-tuning yields improvements, but the gains are more modest (0.3901 vs. 0.4515 with SFT warmup), showing that SFT provides a strong initialization.
Critical Analysis & Conclusion
Key Takeaways
- Hierarchical decomposition works. The separation of strategic reasoning (Orchestrator) from execution (Sub-Agents) is a simple yet powerful idea that alleviates the long-context bottleneck and enables efficient exploration.
- Training matters. The Solution Refinement Tree and ranking-based RL provide a principled way to learn from expensive, long-horizon interactions. The branch-level preference signal (downstream return) is superior to immediate-score comparisons.
- Small models can punch above their weight. A 4B model, when trained as an Orchestrator, can match the performance of a frontier model like o4-mini in this hierarchical setting. This has significant implications for cost-effective deployment.
Limitations
- Sub-Agent scale sensitivity. The success of the hierarchy depends on the Sub-Agent's ability to execute instructions reliably. Small models (e.g., Qwen3-4B) struggle as Sub-Agents even when the Orchestrator provides detailed instructions.
- Fixed Tool interface. The Tool mediation layer is designed for MLE tasks with executable code and scalar feedback. Adapting it to other domains (e.g., robotics with sensory feedback) may require redesign.
- Training cost. While the tree-based sampling reduces wasted interactions, the training process still requires running many expensive environment executions (though the reuse of prefixes mitigates this).
Future Outlook
Matryoshka Agent opens up a promising direction for building scalable, hierarchical agents. We can expect to see:
- Cross-domain application: Applying similar hierarchical decomposition to scientific discovery, software engineering, and robotics.
- Dynamic hierarchy depth: Allowing the Orchestrator to spawn sub-Orchestrators for even more complex tasks.
- End-to-end learned Tools: Instead of hand-crafted mediation, the Tool interface could be learned to optimize the information bottleneck between layers.
Conclusion: Matryoshka Agent is a well-executed, insightful contribution that demonstrates the power of hierarchical agent design for long-horizon tasks. It is a must-read for anyone working on LLM-based agents for complex, iterative problem-solving.
