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

  1. Map: ask the agent to document structure, flows and risky areas
  2. Protect: characterization tests for code you will touch
  3. Refactor: one small change at a time, tests green after each
  4. Migrate: version upgrades and API changes in focused PRs
  5. Document: update architecture notes and instruction files
A safe sequence for legacy work.

Where agents help

TaskAI is good atWatch out for
Explaining old codeFast summaries of unfamiliar modulesConfident but wrong explanations; verify
Characterization testsCapturing current behaviourMissing side effects (DB, files, time)
Framework / language upgradesMechanical API changes across many filesBehaviour changes in edge cases
Dead code detectionFinding unused functionsReflection, dynamic calls, cron entry points
Adding typesInferring types from usageTypes that encode bugs

The safety net

protectchangeresultLegacy moduleno testsCharacterization testspin behaviourSmall refactorsgreen each stepModern moduletested, documented
Tests first, then change.

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) == 849

Focused 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.

More AI articles