Codegen is a python library for manipulating codebases.
Write code that transforms code. Codegen combines the parsing power of Tree-sitter with the graph algorithms of rustworkx to enable scriptable, multi-language code manipulation at scale.
from codegen import Codebase # Codegen builds a complete graph connecting # functions, classes, imports and their relationships codebase = Codebase("./") # Work with code without dealing with syntax trees or parsing for function in codebase.functions: # Comprehensive static analysis for references, dependencies, etc. if not function.usages: # Auto-handles references and imports to maintain correctness function.move_to_file('deprecated.py')This library requires Python 3.12 – 3.13.
uv pip install codegen Software development is fundamentally programmatic. Refactoring a codebase, enforcing patterns, or analyzing control flow - these are all operations that can (and should) be expressed as programs themselves.
We built Codegen backwards from real-world refactors performed on enterprise codebases. Instead of starting with theoretical abstractions, we focused on creating APIs that match how developers actually think about code changes:
-  Natural mental model: Write transforms that read like your thought process - "move this function", "rename this variable", "add this parameter". No more wrestling with ASTs or manual import management. 
-  Battle-tested on complex codebases: Handle Python, TypeScript, and React codebases with millions of lines of code. Built and validated on refactors at companies like Ramp. 
-  Built for advanced intelligences: As AI developers become more sophisticated, they need expressive yet precise tools to manipulate code. Codegen provides a programmatic interface that both humans and AI can use to express complex transformations through code itself. 
Please see our Contributing Guide for instructions on how to set up the development environment and submit contributions.