Abstract
Code optimization requires deep system knowledge and iterative refinement, tasks where even state-of-the-art Large Language Models (LLMs) often struggle. This paper introduces LessonL, a multi-agent framework that enables a team of smaller, specialized LLMs to collaboratively optimize code by sharing "lessons"—concise natural language insights regarding successful and failed strategies. By employing a structured solicitation–banking–selection loop, LessonL allows models like Qwen-7B and DeepSeek-7B to outperform monolithic giants such as GPT-4o. Our results demonstrate that iterative peer learning significantly improves both performance and cost-effectiveness in complex programming tasks.
1. Introduction
While the current trend in AI favors scaling models to hundreds of billions of parameters, these "one-size-fits-all" giants are not universally superior across all domains. In specialized tasks like geometry processing or histogram optimization, a smaller model with specific training may outperform a general-purpose leader. However, leveraging a diverse team of AI agents effectively remains a challenge. Traditional multi-agent approaches often rely on fixed roles (e.g., coder and reviewer) or simple majority voting, which fail to capture the nuanced reasoning behind successful code transformations.
We present LessonL, a framework designed specifically for code optimization. Instead of merely sharing the best code found so far, LessonL agents exchange "lessons" that explain why a specific change worked or failed. This mimics a classroom environment where agents learn from their peers' collective experience to iteratively refine their output.
2. The LessonL Framework
LessonL operates on an iterative loop consisting of three primary phases: solicitation, banking, and selection.
2.1 Lesson Solicitation
When an agent attempts to optimize a piece of code, the framework evaluates the result based on execution speedup and correctness. The agent is then prompted to generate a "lesson" based on this outcome. If the code is successful, the lesson captures the optimization strategy (e.g., "loop unrolling improved cache locality"). If it fails, the lesson serves as a warning (e.g., "this specific reordering caused a race condition").
2.2 Lesson Banking
These insights are stored in a central repository. Unlike full code blocks, lessons are token-efficient, making them an economical medium for knowledge transfer between agents.
2.3 Lesson Selection
In subsequent rounds, agents are provided with a curated set of $k$ lessons. The selection process is governed by three factors:
- Speedup: Prioritizing insights that led to the highest performance gains.
- Semantic Relevance: Using embeddings (e.g., CodeBERT) to match lessons to the specific problem context.
- Effectiveness Adjustment ($f$): A dynamic factor that tracks the reliability of a lesson. If a lesson consistently helps other agents improve, its weight increases; if it leads to hallucinations or errors, it is filtered out.
3. Experimental Evaluation
We evaluated LessonL using a diverse set of models, including DeepSeek-7B, Qwen-7B, and Qwen-14B, against baselines like GPT-4o, GPT-4o mini, and OpenAI o3.
3.1 Benchmarks
The framework was tested on several rigorous code optimization and generation benchmarks:
- ParEval & PolyBench: Focused on scientific computing and numerical tasks.
- HumanEval & MBPP (+ variants): Standard benchmarks for general code generation.
4. Key Results and Insights
4.1 Outperforming Proprietary Models
On the ParEval (Serial) benchmark, LessonL achieved a 2.16× geometric mean speedup, significantly surpassing GPT-4o's 1.72×. In parallel computing environments (OpenMP), the framework reached a 3.46× speedup, outperforming other multi-agent frameworks like MapCoder.
4.2 The Value of Complementary Strengths
Our analysis revealed that different models possess unique "pockets" of expertise. For instance, Qwen-7B outperformed GPT-4o by 2.5× on specific geometry problems. LessonL successfully identifies and propagates these strengths across the agent pool. In one notable case study, the framework guided small models to replace an $O(n^2)$ naive algorithm with an $O(n \log n)$ divide-and-conquer approach, resulting in a 74.31× speedup.
4.3 Lessons vs. Raw Code
Empirical evidence suggests that passing natural language insights is more effective than passing raw code. Lessons allow agents to stack strategies—such as combining loop reordering with parallelization—without being constrained by the specific implementation details of a previous attempt.
5. Conclusion
LessonL demonstrates that a collaborative framework of small LLMs can achieve state-of-the-art performance in code optimization by focusing on iterative, interpretable learning. While the current implementation is limited to function-level snippets and introduces some iterative latency, it provides a scalable and cost-effective alternative to monolithic model reliance. Future work will explore extending LessonL to repository-level tasks and increasing agent autonomy in lesson selection.
Summary updated