AI and Legacy Codebases: What Actually Works
The question we hear most often from engineering leads at companies with mature codebases is some version of: "Our codebase is ten years old, barely documented, and has no tests. Can AI agents help?" The honest answer is yes — but what "help" looks like in a legacy codebase is different from what it looks like in a greenfield one, and the order of operations matters enormously.
Why legacy codebases are hard for everyone
A legacy codebase is not just old code. It is accumulated institutional knowledge encoded in patterns that were never documented, edge cases that were handled once and never explained, and architectural decisions that made sense given the constraints of the time. When that knowledge is not written down, it exists only in the behavior of the code and in the memories of engineers who may no longer be at the company. Changing anything without understanding that context creates risk.
This is the same challenge for AI agents as for new human engineers. Neither can safely change code they don't understand in a codebase that has no tests to catch breakage. The answer is the same in both cases: build the understanding first, then change the code.
Step one: characterization tests
Before any refactoring, any bug fixing, any performance improvement — the first thing an AI agent should do with an untested legacy codebase is write characterization tests. These are tests that document current behavior, not desired behavior. They capture what the code does, including behaviors that might be bugs, and create a safety net that catches any regression.
Writing characterization tests systematically across an entire codebase is a task that would take a human team months. An AI agent can do it in days, reading each function and class, inferring its behavior from the code and any available documentation, and writing tests that lock in that behavior. When the tests exist, you can change the code and know immediately if something broke.
Dependency audit and upgrade
The second step is safer than it looks. A legacy codebase almost certainly has outdated dependencies — libraries that have received security patches, performance improvements, and API improvements that the codebase cannot use because it is pinned to an old version. An AI agent can audit the dependency tree, identify outdated packages, assess the breaking changes in newer versions, and upgrade incrementally with test runs at each step.
This is mechanical work that is time-consuming and low-value for human engineers, and it is exactly the kind of work that AI agents handle well. Upgrading from PHP 7.4 to 8.x, from Laravel 8 to Laravel 11, or from a deprecated authentication library to a current one — these upgrades each have a defined set of breaking changes that an AI agent can reason about systematically.
Security remediation
Legacy PHP codebases are disproportionately represented in vulnerability databases. The combination of PHP's historical ecosystem — with vast amounts of tutorial content teaching insecure patterns — and age means that many mature PHP applications carry SQL injection vulnerabilities, XSS attack surfaces, and authentication weaknesses that were never addressed because the team that introduced them has long since moved on.
After characterization tests and dependency upgrades are in place, security remediation is the highest-value third step. An AI agent can audit every SQL query for parameterization, every user-controlled value that reaches HTML output for proper escaping, every file path that accepts user input for traversal vulnerabilities. This audit, done comprehensively, addresses the risks that create the most business impact.
What AI agents should not do
Two failure modes are worth naming explicitly. The first is attempting big-bang rewrites: reading a large legacy module, deciding it should be redesigned from scratch, and producing a replacement. This approach fails because the legacy module almost certainly encodes behavior that is not documented anywhere, and the rewrite will miss it. The right approach is always incremental: change one thing at a time, verify with tests, merge, repeat.
The second failure mode is changing behavior and structure in the same commit. If you refactor the structure of a function and also change its behavior, you cannot verify that the behavior change was intentional. Separate refactoring (no behavior change, tests pass before and after) from feature work (behavior change, with new tests).
The realistic timeline
A mature legacy PHP application with 100,000 lines of code and minimal tests can typically get to a defensible state — characterization tests covering the critical paths, dependencies upgraded, known security vulnerabilities patched — in four to six weeks of AI-assisted work. The same work done by a human team with competing priorities takes one to two years. This is the compounding return on AI-assisted modernization: the work that was perpetually backlogged because it was never urgent enough to prioritize over feature work gets done.
Frequently asked questions
Can AI agents work with legacy codebases that have no tests?
Yes, but writing characterization tests to document current behavior must come first. Without tests, any change to a legacy codebase carries unknown risk. Characterization tests create the safety net that makes subsequent work verifiable.
What is the right order of operations for AI-assisted legacy modernization?
Characterization tests first, then dependency upgrades, then security remediation, then performance improvements, then architectural refactoring. Each step builds on the safety of the previous one. Skipping steps or doing them out of order increases risk substantially.
What should AI agents avoid doing in legacy codebases?
Big-bang rewrites of large modules without tests, and mixing behavioral changes with structural refactoring in the same commit. Incremental change with verification at each step is always safer than sweeping changes, regardless of how obvious the improvements seem.