

Tl;dr: Librarian refactors a set of files into a shared library. It samples many refactorings, drops the ones that fail the tests, and keeps the one with the lowest MDL. In a user study, people preferred MDL’s pick over Maintainability Index’s 67% of the time. On Hugging Face Transformers, Librarian’s library has 15 functions used about 100 times; Claude Code’s has 7, used 41 times.
Maintainable and general software allows developers to build robust applications efficiently, yet achieving these qualities often requires refactoring specialized solutions into reusable components. This challenge becomes particularly relevant as code agents are increasingly used to solve isolated one-off programming problems. We investigate code agents’ capacity to refactor code in ways that support consolidation and reusability. We first investigate what makes a good refactoring, finding via simulation results and a human study that proxies of program size, such as Minimum Description Length, better predict preferable refactorings than extant software engineering metrics, such as Maintainability Index. We then present Librarian, a method that combines divide-and-conquer with sample-and-rerank to generate reusable libraries. We compare Librarian to state-of-the-art library generation methods, and study it on real-world Python programs.
129 judgments from 14 people. MI = Maintainability Index.
Claude Code builds a library with 7 functions, used 41 times. Librarian samples from Claude Code and builds one with 15, used ≈100 times.
On the Hugging Face Transformers files. We asked Claude Code once to “reduce code duplication by extracting shared code into a library”.
All integration tests still pass.
@inproceedings{kovacic2026refactoring,
title = {Refactoring Code Through Library Design},
author = {Kova{\v{c}}i{\v{c}}, {\v{Z}}iga and Chiu, Justin T. and Lee, Celine
and Zhao, Wenting and Ellis, Kevin},
booktitle = {Advances in Neural Information Processing Systems},
year = {2026}
}