This paper addresses the problem of refactoring code into reusable components to improve code maintainability and generalizability. This problem becomes particularly important when code agents are used to solve one-off programming problems. This study investigates the ability of code agents to refactor code in a way that supports growth and reusability. Through simulations and human studies, we demonstrate that minimum description length best correlates with preferred refactoring. Furthermore, we present the MiniCode benchmark, which requires refactoring multiple files into shared libraries, and Librarian, a sample and re-ranking method for generating reusable libraries. We compare Librarian with state-of-the-art library generation methods and study it on a real code base.