Topic 1 of 5 · 4 min read
Mapping an unfamiliar codebase with an agent
Goal: Get an agent-written map of a codebase and verify three of its claims against the source.
Ask an agent to draw you a map of the codebase, then treat every line of that map as a claim you can check. An agent can read 200 files in a few minutes. It cannot tell you which of its statements are guesses.
Check yourself · 1 of 3
You ask an agent to map a legacy repo. Which request gives you the most checkable result?
Why it matters
You inherit a 10-year-old billing service. No one who wrote it is left. Reading it by hand takes days. Claude Code, Cursor, Codex or Copilot can produce a first map in minutes. But a map that is wrong is worse than no map, because you will plan work around it.
How to do it
Ask for specific parts, one at a time, and ask for evidence with each.
- Entry points. Where does execution start? HTTP routes, queue consumers, cron jobs, CLI commands. Ask for file and line.
- Data flow. Pick one real request, such as "create an invoice", and ask the agent to trace it from entry to database write.
- Module map. A short list: each folder, its job, what it depends on.
- External dependencies. Databases, third-party APIs, environment variables, scheduled jobs.
- Unknowns. Tell the agent to list what it could not determine. A good map has a gap list.
Then check three claims yourself. Open the cited file and line. Run the app, or a test, and watch the flow. Search for callers with a plain grep. Pick claims that matter to your change, not the easy ones.
Before and after
Weak prompt: "Explain this repo."
Better prompt:
Map this repo for a developer who must change invoice rounding.
1. List entry points (routes, jobs, consumers) with file:line.
2. Trace "POST /invoices" to the database write, one step per line with file:line.
3. List modules and what each depends on.
4. List anything you could not confirm or that looks generated, dead or dynamic.
Do not guess. Say "not found" when you cannot find it.
The first prompt gets a tidy, confident essay. The second gets claims you can verify, and a list of what is still unknown.
What goes wrong
- Plausible invention. The agent names a
PaymentServicethat does not exist, or describes a layer from the framework's usual layout rather than this repo. - Missing the dynamic parts. Reflection, plugin loading, config-driven routing and database triggers do not show up in a text search. Agents often skip them.
- Stale docs treated as truth. The agent summarises an old README as if it described the code.
- Reading only part of the repo. Large repos exceed what the agent reads. Ask which folders it did not open.
Examples
Tracing one request
The prompt asks the agent to trace POST /invoices. It answers: route in routes/invoice.ts:14, handler createInvoice, then InvoiceService.save, then a write to invoices. You open InvoiceService.save and find it also fires a billing.created event that a separate worker consumes. The map missed a side effect, so you add it to the map and ask the agent to find every consumer of that event.
A gap list that helps
The agent ends with: "Could not confirm: how TAX_MODE is set in production; whether legacy/export.js is still called." Those two lines tell you where to ask a teammate or check deployment config before touching anything.
Key points
- Ask for one part of the map at a time and require file and line evidence for each claim.
- Trace one real request end to end; it shows more than a folder-by-folder summary.
- Always ask the agent to list what it could not confirm.
- Verify at least three claims yourself by opening the file, running the code or searching for callers.
- Dynamic behaviour such as reflection, config-driven routing and database triggers is where agent maps are most often wrong.
Common mistakes
- Asking "explain this repo" and accepting the fluent essay as fact.
- Checking only the claims that were easy to confirm.
- Skipping the gap list, so unknowns look like known facts.
- Trusting an old README or doc comments over what the code does.
Check yourself · 2 of 3
An agent's map of a legacy service names a PaymentService class. What is the right next step?
Check yourself · 3 of 3
What should a good agent-written codebase map always include?
Practice items, not counted towards your mastery.
Start here
- Legacy Modernization meets GenAI (martinfowler.com) (opens in a new tab)ArticleFree
Thoughtworks field notes on using generative AI to understand and modernise old systems.
- Characterization test (Wikipedia) (opens in a new tab)ArticleFree
Defines the technique of pinning current behaviour with tests before you change code.
- Strangler Fig Application (Martin Fowler) (opens in a new tab)ArticleFree
The standard pattern for replacing a legacy system piece by piece instead of all at once.
More for this topic
- Best practices for Claude Code (Claude Code docs) (opens in a new tab)DocsFree
A vendor's advice on exploring a codebase first and giving the agent tests to verify its work.