Manuscript Offprint

arXiv:2609.01045

AgentFactory: Towards Automated Agentic System Design and Optimization

Enci Zhang, Haofeng Wang, Yuesheng Zhu, Xiaole Cui, Guibo Luo

Enci Zhang, Haofeng Wang, Yuesheng Zhu, Xiaole Cui, and Guibo Luo✉

Peking University Shenzhen Graduate School, Shenzhen, China

eczhang@stu.pku.edu.cn, hfwang@stu.pku.edu.cn, zhuys@pku.edu.cn, cuixl@pkusz.edu.cn, luogb@pku.edu.cn

Abstract. Large Language Models (LLMs) have demonstrated remarkable capabilities as powerful components in agentic systems, enabling sophisticated reasoning and complex task execution. However, current approaches to manually designing and optimizing agentic systems heavily rely on manual effort, limiting their adaptability and scalability. Recent work has explored the automated optimization of workflow designs. However, these approaches often overlook the crucial role of model capabilities and focus on single performance metrics, failing to address real-world deployment constraints. In this paper, we present AgentFactory, a framework that jointly optimizes both foundation models and workflow structures in agentic systems while considering multiple objectives including performance, cost, and efficiency. AgentFactory leverages advanced LLMs as optimizers to navigate the vast search space of possible configurations, employing a three-stage optimization pipeline to automatically discover effective combinations of fine-tuned models and optimized workflows. Through an iterative optimization process, our framework systematically explores and evaluates different agentic system designs, adapting to task-specific requirements while maintaining operational efficiency. We evaluate AgentFactory across eight benchmarks spanning five domains, including general reasoning, coding, mathematics, medicine, and finance. Our experiments demonstrate that AgentFactory consistently outperforms both manually designed methods and existing automated approaches, achieving an average improvement of 9.1% across all benchmarks, with particularly significant gains in domain-specific tasks (19.6% on MedQA and 18.7% on FinEval). These results establish AgentFactory as a promising approach for developing more capable and efficient agentic systems through automated optimization.

Keywords: Large language model · Agent framework · LLM fine-tuning · Automated agentic optimization

1 Introduction

Large Language Models (LLMs) such as GPT-4o, Claude-3.5-sonnet, and DeepSeek V3 have demonstrated remarkable capabilities as powerful components in agen-

Enci et al.

tic systems, enabling sophisticated reasoning and complex task execution. However, the effective deployment of LLM-based agentic system heavily depends on manual design and tuning. For instance, frameworks like ChatDev [24] and MetaGPT [14] predefine various roles, workflow structures, and their corresponding responsibilities, manually assigning distinct profiles to each agent to facilitate collaboration. This approach, while functional, is labor-intensive and relies heavily on empirical experience, often leading to sub-optimal solutions and making it challenging to explore the vast solution space and discover optimal configurations. The history of artificial intelligence reveals a consistent pattern where learned solutions supersede manual engineering—from features in computer vision to model architectures themselves via Neural Architecture Search (NAS) [26]. This historical precedent strongly suggests that automating the design of agentic systems could yield more effective and scalable solutions than current manual approaches.

Given the impressive capabilities of Large Language Models in understanding complex instructions and generating creative solutions, recent efforts have begun exploring their potential as optimizers for automated agentic system optimization, primarily focusing on prompt engineering and workflow design. Initial approaches like PromptBreeder [11] and TextGrad [35] have demonstrated success in automated prompt optimization. However, optimizing prompts alone may not be sufficient for complex tasks that require coordinated interactions between multiple components. Recognizing this limitation, more comprehensive frameworks like ADAS [17] and AFlow [36] have extended automation to workflow design and optimization. However, these approaches face two significant limitations. First, they concentrate solely on workflow optimization while neglecting model-level enhancements, which particularly limits their effectiveness in specialized domains where workflow optimization alone cannot bridge the domain adaptation gap. Second, these methods typically optimize for single metrics like accuracy or F1 scores, overlooking crucial real-world considerations such as inference costs and response latency.

To address these limitations, we present AgentFactory, a framework designed for automated agentic system generation and optimization. Given a task specification, AgentFactory first generates a basic agentic system and then iteratively optimizes it until all constraints are satisfied. The optimization process operates in the joint space of model parameters and workflow representations, subject to multiple constraints. Unlike previous approaches that only focus on workflow optimization, AgentFactory expands the optimization search space by incorporating foundation model fine-tuning, enabling more comprehensive system optimization. By incorporating fine-tuning into the optimization process and leveraging powerful LLMs as optimizers, our framework simultaneously optimizes multiple objectives including performance, cost, and efficiency. Our extensive experiments across 5 domains and 8 benchmarks show an average performance improvement of 9.1% over existing methods, with particularly significant improvements in specialized domains, achieving 19.6% and 18.7% improvements.

on MedQA [18] and FinEval [37] benchmarks respectively, while maintaining lower inference costs.

Overall, our contributions can be summarized as follows:

– We propose AgentFactory, a comprehensive framework for automated agentic system design and optimization.

– We use optimization theory to formalize the problem of agentic system optimization and develop algorithms to solve it. We further introduce model fine-tuning into the automated agent design process and pioneer multi-objective optimization considering accuracy, cost, and latency.

– We conduct extensive experiments across diverse domains, demonstrating superior performance compared to both manual designs and state-of-the-art automated approaches.

References (36)