Using AI Coding Agents on Legacy Codebases Safely
Practical techniques for applying AI agents to old code: mapping the codebase, characterization tests, incremental refactoring, migrations.
Published December 1, 2026
Legacy codebases are where AI coding agents can save the most time and also cause the most damage. Old code is under-documented, under-tested and full of behaviour nobody remembers deciding. Agents can read and explain it quickly, write the missing tests, and do tedious migrations, as long as you work in small, verified steps.
The concept
Begin with understanding, not changing. Ask the agent to map modules, entry points, data flows and risky areas, and to write the findings into a document you review. Then protect behaviour with characterization tests that capture what the code does today, including odd edge cases, before any refactoring.
Change incrementally. Upgrade one dependency, one module or one pattern at a time, running the full test suite after each step and committing frequently so any step can be reverted. Resist the temptation to let an agent "rewrite this in a modern framework" in one go; big-bang rewrites fail for AI just as they do for humans.
The approach
- Map: ask the agent to document structure, flows and risky areas
- Protect: characterization tests for code you will touch
- Refactor: one small change at a time, tests green after each
- Migrate: version upgrades and API changes in focused PRs
- Document: update architecture notes and instruction files
Where agents help
| Task | AI is good at | Watch out for |
|---|---|---|
| Explaining old code | Fast summaries of unfamiliar modules | Confident but wrong explanations; verify |
| Characterization tests | Capturing current behaviour | Missing side effects (DB, files, time) |
| Framework / language upgrades | Mechanical API changes across many files | Behaviour changes in edge cases |
| Dead code detection | Finding unused functions | Reflection, dynamic calls, cron entry points |
| Adding types | Inferring types from usage | Types that encode bugs |
The safety net
Practical examples
Ask for a codebase map
Produce a reviewed artefact before any edits.
"Explore this repository without changing anything. Write docs/architecture-notes.md covering:
entry points, main modules and responsibilities, external services, database tables used per module,
and the 5 riskiest areas to modify, with file paths."Characterization test for odd behaviour
Capture what happens, even if it looks wrong.
def test_discount_rounding_current_behaviour():
# Legacy code rounds half-down; keep until product decides otherwise
assert apply_discount(999, percent=15) == 849Focused migration prompt
One concern per PR.
"Replace all uses of the deprecated moment.js in src/reports with date-fns.
Keep output strings identical. Do not touch other directories. Run npm test after."Common mistakes
- Letting an agent refactor code with no tests.
- Big-bang rewrites in a new framework.
- Trusting AI explanations of legacy code without checking.
- Deleting "dead" code that is called dynamically or by scheduled jobs.
Key takeaways
- Understand first: map the codebase and review the map.
- Protect behaviour with characterization tests.
- Change in small, reversible, tested steps.
- Update documentation as you go.
FAQ
Can AI translate a whole app to another language?
It can help, but treat it as an incremental migration with tests at every step, not a one-shot conversion.
How do I find dead code safely?
Combine static analysis, runtime coverage from production or staging, and search for dynamic references before deleting.
What about missing documentation?
Agents are good at drafting docs from code; have someone who knows the system review them.