Refactoring Codebases through Library Design

Ziga Kovacic, Celine Lee, Justin T. Chiu, Wenting Zhao, Kevin Ellis · DOI

Introduction

Large Language Models (LLMs) have revolutionized individual code generation tasks, yet they often struggle with the holistic architectural requirements of large-scale software development. A common side effect of LLM-driven development is "myopic" code generation—where similar logic is repeated across multiple files with slight variations. This redundancy contributes significantly to technical debt, which is estimated to cost the global economy nearly $2 trillion annually.

This paper introduces LIBRARIAN, a novel framework designed to mitigate this issue by automatically refactoring specialized codebases into modular, shared libraries. By moving beyond simple correctness to focus on design quality, LIBRARIAN demonstrates how LLMs can be used to discover and implement reusable abstractions at scale.

The LIBRARIAN Framework

LIBRARIAN operates on a "sample-and-rerank" principle to transform redundant code into a structured library. The process involves four primary stages:

  1. Clustering: To manage repositories that exceed the context window of current LLMs, LIBRARIAN first summarizes files and uses agglomerative clustering to group files likely to share latent abstractions.
  2. Sampling: A backend LLM (such as Claude or GPT-4) generates multiple candidate refactorings for each cluster, producing both a shared library and rewritten source files that import from it.
  3. Reranking via Minimum Description Length (MDL): This is the core innovation. LIBRARIAN uses MDL—the negative log probability of the code under a reference model like Qwen-2.5—to rank candidates. The hypothesis is that the most "natural" and compressed representation of code reflects the best architectural design.
  4. Verification: All candidates undergo rigorous unit testing to ensure functional equivalence with the original code.
The LIBRARIAN workflow: distilling abstractions from multiple source files into a centralized library.

Key Insights: The Power of MDL

A central finding of this research is that neural language models, through the MDL metric, are superior judges of refactoring quality compared to traditional software engineering metrics like Cyclomatic Complexity or the Maintainability Index.

Avoiding the "Perl Golf" Trap

Traditional compression-based metrics often favor "token minimization," which can lead to obfuscated, unreadable code (reminiscent of "Perl Golf"). In contrast, MDL penalizes "unnatural" code. Because a language model assigns lower probability to convoluted logic, MDL-optimized refactorings tend to be more readable and aligned with human design preferences. Furthermore, LIBRARIAN often creates functions that are only used once; while traditional wisdom might suggest inlining these, the authors argue this represents a conceptually clean decomposition that the LLM finds "natural."

Comparison of refactoring metrics (MDL, Token count, and MI) across different optimization objectives and sample budgets.

Scaling Test-Time Compute

The research shows that refactoring quality improves steadily as the number of samples ($K$) increases. Interestingly, the utility of the resulting library—measured by function reuse—tends to plateau around $K=20$. This suggests that high-quality library design is computationally feasible without requiring exhaustive sampling.

Experimental Results

The authors evaluated LIBRARIAN using a new benchmark, MINICODE, which includes competitive programming tasks and real-world production code from HuggingFace Transformers and Diffusers.

  • Synthesis Performance: LIBRARIAN achieved a 41.8% relative improvement in solve rates on unseen program synthesis tasks compared to previous state-of-the-art methods like REGAL.
  • Real-World Scale: In a landmark application, LIBRARIAN refactored the HuggingFace Transformers library, reducing its MDL to 67.2% of the original size while maintaining a 100% pass rate on integration tests.
  • Human Alignment: User studies confirmed that developers significantly prefer MDL-optimized refactorings over those produced by standard maintainability metrics.

Conclusion

LIBRARIAN represents a significant advancement in automated software engineering. By treating library design as a compression problem guided by neural language models, it provides a scalable way to manage technical debt and improve codebase modularity. Future work will explore the cost-efficiency of these methods and the extent to which learned libraries can support the development of entirely new applications.

Summary updated

BibTeX
@inproceedings{kovacic2025refactoring,
  author = {Ziga Kovacic and Celine Lee and Justin T. Chiu and Wenting Zhao and Kevin Ellis},
  title = {{Refactoring Codebases through Library Design}},
  booktitle = {DL4C @ NeurIPS},
  year = {2025},
  url = {https://openreview.net/forum?id=qUf8qdxR9I},
}
Figure