Using AI to Understand and Document Legacy Systems
Overview
Your system is 15 years old. It's the lifeblood of your company. It processes millions in transactions. It runs critical customer workflows. It's the only thing standing between you and business shutdown.
And nobody knows how it works.
The original architects left a decade ago. They took the mental model with them. The code has no comments. No architectural documentation. No data flow diagrams. The only thing that keeps the system running is muscle memory: there's someone on the team who's spent 15 years with this code and they know where all the landmines are.
But that person is going to retire. Or leave for a competitor. Or get hit by a bus (metaphorically, hopefully).
When they're gone, what happens to the system? You're screwed, that's what. You can't modify it safely. You can't troubleshoot it. You can't migrate it. You can't optimize it. You're completely dependent on knowledge that exists in one person's head.
This is the legacy system nightmare. Every large organization has one. Some have several. And the traditional solution, hire someone to document it, is glacially slow. Documenting a 15-year-old system takes months. The documentation gets outdated. And you still have gaps because you didn't understand the system well enough to document it.
AI changes this completely. An AI can read your codebase and understand it. Not perfectly. But well enough to generate documentation that's immediately useful. You can extract knowledge at a scale and speed that was impossible before.
This isn't about replacing engineers. It's about giving your team super-powers. An engineer that used to spend weeks understanding a legacy system can now do it in days. They can focus on actually improving the system instead of just trying to understand it.
The Legacy System Knowledge Problem
Let's be precise about what the problem is. It's not that the code is hard to read. Most legacy code is actually pretty straightforward. It's old patterns, clear variable names, straightforward logic.
The problem is scale and breadth. A system that's 15 years old doesn't have one thing it does. It does dozens of things. It has hundreds of thousands of lines of code. There are data flows you don't understand. There are integrations with external systems. There are edge cases that were added as hotfixes and never cleaned up.
Understanding it requires holding a mental model of the entire system. And that model is in one person's head.
Here's what happens when you try to document manually:
Day 1: You sit down with the engineer who knows the system. "Okay, explain it to me." They start. "So there's the user service, which talks to the account service, which talks to the billing service..."
Two hours later, your head is spinning. You've filled a notebook with illegible notes. You know roughly how it works, but there are a hundred questions. You didn't think to ask them. The engineer skipped details because they thought you understood them. They made assumptions that turned out to be wrong.
Day 2-5: You're documenting what you learned. Writing it down. Realizing gaps. Asking follow-up questions. Trying to draw architecture diagrams. Failing. Trying again.
Week 2-4: You're getting somewhere. You've got a rough documentation of how the system works. But you're only 30% done. There's the payment processing pipeline. The notification system. The reporting engine. The integration with the legacy mainframe. Each of these is complex enough to require 2-3 weeks of documentation by itself.
Week 8-12: You've documented most of the system. But it's already getting outdated. The team made changes you don't know about. You missed some edge cases. Documentation is 70% complete and getting stale by the day.
And you've only documented 40% of it.
This is why most legacy systems never get documented. The effort is enormous. The result is fragile. And by the time you're done, half of it is already wrong.
The Cost of Not Knowing
What's the cost of this knowledge gap? It's huge.
When a production incident happens, you have to get the one person who understands the system on the call, no matter what time of night. When you need to add a feature, it takes longer because engineers have to understand the code first. When someone suggests a refactor, nobody knows if it's safe because nobody understands the dependencies. When you hire a new engineer, it takes them 3-6 months to be productive on this system.
And the risk is always present: what if the one person who knows the system decides to leave?
Some organizations have backups (usually informal), but most don't. It's a single point of failure in your technical infrastructure. The person is too valuable to lose. The system is too important to risk. And you're paying a productivity tax because everything related to that system is slow and expensive.
Reality check: If removing one person from your team would cripple your operations, you have a knowledge management problem. AI can fix this, but it requires intentional effort.
What AI Can Actually Do With Legacy Code
Understand the System
Feed your codebase to Claude. Ask it: "What does this system do?" The answer will be remarkably good. It will understand the general architecture. It will identify the major components. It will explain the data flows.
Is it perfect? No. But it's 80% accurate. And that 80% is worth weeks of manual documentation effort.
You can ask follow-up questions. "What does the billing module do?" "How does it interact with the payment processor?" "What happens when a payment fails?" Claude will answer based on the code.
This is knowledge extraction at scale. You're not manually talking to the engineer. You're asking the code directly, via AI.
Generate Documentation
From understanding comes documentation. You can ask Claude to generate:
Architecture documentation: "Create a high-level architecture document for this system. Include major components, their responsibilities, and how they interact."
API documentation: "Document all the public APIs. For each API, describe what it does, what parameters it takes, what it returns, and example usage."
Data model documentation: "Document the database schema. For each table, describe the columns, their types, constraints, and relationships."
Runbook documentation: "Create a runbook for common operational tasks. How do we deploy? How do we monitor? What do we do when things break?"
None of this documentation will be perfect. But it will be a solid foundation. You can review it, correct it, and refine it. You end up with documentation that's 90% accurate in a fraction of the time.
Identify Patterns and Anti-Patterns
AI can recognize patterns in your code. "You're using this retry pattern in 47 different places. Here's how to abstract it." "You have duplicate logic in these 12 functions. Here's how to refactor."
This is useful not just for understanding, but for improvement. You identify technical debt. You find opportunities for refactoring. You improve code quality as a byproduct of understanding the system.
Find Dependencies and Risk
AI can trace through your code and build a dependency graph. "Service A calls Service B in 47 places. Service B calls Service C. Here are the failure modes if any of these break."
This is incredibly valuable for understanding operational risk. You can identify single points of failure. You can understand cascading failure modes. You can prioritize what to fix based on risk.
Spot Security Issues
AI is good at finding security problems. "You're accepting user input in this function and passing it to SQL without escaping. That's SQL injection. Here are 12 places this happens."
This is a natural byproduct of understanding the code. You identify security risks while understanding the system.
Answer Questions at Scale
Once you've fed your codebase to Claude and built a knowledge base, you can use it to answer questions. "How does the billing system work?" "What's the data flow from the API to the database?" "Where do we handle timezone conversions?"
Engineers can get answers without interrupting the senior engineer who knows the system. This is powerful for onboarding, troubleshooting, and feature development.
The Knowledge Extraction Process
Phase 1: Broad System Understanding (1-2 weeks)
Start with a broad overview. Don't try to understand everything in detail. Get the big picture first.
Collect your codebase into a format that's easy to analyze. Create a directory tree. Collect README files. Identify entry points (main functions, API endpoints, database migrations).
Feed this to Claude with a request: "Analyze this system. What does it do? What are the major components? How do they interact?"
Let Claude ask clarifying questions. "What language is this? What frameworks? What does the company do?" The more context you give it, the better the understanding.
The output: a high-level overview of the system. What it does. Who uses it. What problems it solves. The major architectural components.
Have domain experts review this. Is it accurate? What's missing? What's wrong? Iterate until you have an accurate high-level model.
Phase 2: Deep Dives on Key Components (2-4 weeks)
Now drill down into the components that matter. Pick your three to five most critical systems. Billing. Authentication. Payment processing. Core business logic.
For each component, feed the code to Claude. Ask: "How does this component work? What's the data flow? What are the dependencies? What are the failure modes?"
Claude will give you detailed explanations of each component. Ask follow-up questions to fill in gaps.
The output: detailed documentation of critical systems. Enough detail that a new engineer could understand how these systems work and why they're designed the way they are.
Again, have domain experts review. Iterate to accuracy.
Phase 3: Generate Complete Documentation (2-3 weeks)
Now you're ready to generate comprehensive documentation. You've understood the system. You've validated the understanding. Now you can ask Claude to generate professional documentation.
Ask for: architecture documentation, API documentation, data model documentation, runbook documentation, operational procedures documentation.
Claude will generate all of this based on the code and the context from phases 1 and 2.
Review and refine. Some of it will be spot-on. Some of it will need correction. But you're working from a solid foundation, not starting from scratch.
The output: a comprehensive documentation suite that covers your entire system. Not perfect, but professional and usable.
Phase 4: Build a Knowledge Base (ongoing)
Now build a searchable knowledge base. This could be a wiki, a Confluence instance, or a custom system. The point is: make the documentation easily discoverable and searchable.
Add a system for asking questions. Engineers encounter a question about the system. They search the knowledge base. If the answer is there, great. If not, they ask, the system is researched, documentation is added, and the answer is recorded for next time.
This knowledge base becomes a living system. It starts at 80% complete. Over time, it approaches 95% as gaps are filled in. It stays current because it's updated as the system evolves.
Phase 5: Onboarding and Knowledge Sharing (ongoing)
Use the documentation to accelerate onboarding. New engineers on the team can read the documentation instead of taking up the senior engineer's time for weeks.
Use the documentation for knowledge sharing. Junior engineers can learn from it. Distributed teams can understand the system without flying the expert to their location.
Use the documentation as the foundation for training and certification. Want to be qualified to modify the billing system? Here's the documentation you need to understand first.
The documentation becomes institutional knowledge. It's not in anyone's head. It's written down. It's searchable. It's shareable. If someone leaves, the knowledge stays.
Success criterion: A new engineer can onboard to the system in 4 weeks (instead of 12) by reading documentation and asking questions. The senior engineer spends 10% of their time on onboarding (instead of 40%).
Practical Implementation Guide
Step 1: Assess Code Readability
Not all code is equally readable to AI. Some languages are harder than others. Some codebases are more organized than others.
Do a quick assessment: can Claude read your code effectively? The test: pick a representative file. Feed it to Claude. Ask it to explain what the file does. Is the explanation accurate? Detailed? If yes, you're good. If no, you might need to pre-process the code.
Common pre-processing: remove generated code, remove boilerplate, remove configuration files that aren't relevant, focus on business logic.
Step 2: Start With the Most Critical Component
Don't try to understand the entire system at once. Pick the most business-critical piece. If something breaks, what's the impact? That's your starting point.
For a SaaS company: billing. For a fintech: payment processing. For a marketplace: search and discovery. Pick the thing where failure has the highest business impact.
You understand this component deeply. You validate the understanding. You document it. You're done with one critical piece. Now move to the next.
Step 3: Prepare for AI Review Cycles
You're going to feed your codebase to Claude multiple times. Each time, you're asking slightly different questions. "High-level overview." "Data flow." "Error handling." "Integration points."
Plan for this. Set aside budget for Claude API calls. They're cheap (maybe $100-500 for a complete system analysis), but plan for them.
Also plan for review cycles. For each piece of documentation Claude generates, you need a human (ideally someone who knows the system) to review it. This takes time. Budget for it.
Step 4: Document as You Learn
Don't wait until you've understood the entire system to start documenting. As you understand pieces, write them down. Create a living document that evolves.
This serves multiple purposes: it forces you to think about whether your understanding is accurate (if you can't write it down clearly, you don't really understand it), it gives you something to show the team as progress, and it creates an artifact that can be refined and improved.
Step 5: Create a Feedback Loop
As new engineers read the documentation, they'll have questions. Questions that aren't answered by the documentation. That's feedback. Use that feedback to improve the documentation.
Track questions. When someone asks "how does this work," that's a gap in the documentation. Answer the question. Add it to the documentation. Next engineer with the same question finds the answer.
Step 6: Focus on Actionability
Documentation that's beautiful but not useful is waste. Focus on actionable documentation. "If you need to add a new payment method, here's how. If a payment fails, here's the flow. If you need to debug a customer billing issue, here's where to look."
Include examples. Code samples. Data flow diagrams. Command line instructions. Runbooks for common tasks.
The documentation should let someone do work with the system, not just understand it theoretically.
Scaling Knowledge Across the Organization
Once you've documented a legacy system, don't stop. Use what you've learned to accelerate documentation of other systems.
Build a Template
The process you used to document one system can be repeated for others. Create a template. Create a checklist. Create prompts you can reuse.
Each subsequent system gets faster. First system takes 8 weeks. Second system takes 5 weeks. Third system takes 3 weeks. You're learning the process and your team is getting better at it.
Train the Team
You don't have to be the bottleneck. Train other engineers to do this work. They learn the prompts. They learn the process. They can document components while you focus on synthesis and review.
A team of three engineers can document your entire system in a month. One engineer alone would take six months.
Build Your Knowledge Product
Your documentation becomes a product. It's valuable internally (for onboarding, troubleshooting, feature development). It could be valuable externally if you're building APIs or platforms that others use.
Good documentation is competitive advantage. It makes you faster than competitors. It makes you safer (fewer bugs from misunderstanding). It makes you better at hiring (new engineers are productive faster).
Update Continuously
Documentation is not a one-time project. It's ongoing. As the system evolves, documentation should evolve with it.
Build this into your development process. When you change a component, someone updates the documentation. When you add a feature, it gets documented. This overhead is small if you do it continuously. It's massive if you let it accumulate for a year.
Use AI to help with updates too. "We just changed the billing algorithm. Here's the new code. Update the documentation."
Key insight: Documentation is a feature that compounds over time. Bad documentation is a compounding cost (everyone has to figure things out manually). Good documentation is a compounding benefit (everyone is faster, everyone is safer, onboarding is quicker).
What to Do Monday Morning
Identify your knowledge crisis: Is there a system where knowledge is concentrated in one or two people? Is there a system that's hard to onboard to? Is there a system with high operational risk? Start there.
Pick one critical component: Don't try to document the entire system at once. Pick the most business-critical piece. The thing where failure has the highest impact.
Feed the code to Claude: Ask it to explain what the component does. Ask follow-up questions until you understand how it works and why it's designed that way.
Ask Claude to generate documentation: Ask for architecture documentation, API documentation, data model documentation. Review it. Correct it. Refine it.
Validate the documentation: Have someone who knows the system review it. Is it accurate? Complete? Clear? Useful for onboarding?
FAQ
Q: How accurate is AI at understanding legacy code?
A: Pretty accurate for the main flows. 80%+ for general understanding. Lower for edge cases and weird hacks. The key is to validate with someone who knows the system. You're using AI to accelerate documentation, not replace human understanding.
Q: What if the code is in a language or framework AI doesn't know well?
A: AI is decent at most languages. Even if it's not perfect, it can still understand the logic and data flows. You might get 60% accuracy instead of 80%, but that's still better than zero documentation. And humans can review and correct.
Q: How much code can Claude process at once?
A: Depends on the model. Claude 3.5 Sonnet can process 200K tokens. That's roughly 50K-100K lines of code depending on formatting. For large systems, break it into components. One service at a time. One module at a time.
Q: Should we trust AI's documentation without human review?
A: No. Always have a human review. Someone who knows the system should validate: is this accurate? Complete? Clear? But AI-generated documentation is a fantastic starting point. It's 80% there. You're refining the final 20%, not creating from scratch.
Q: What if the documentation gets outdated as the system evolves?
A: That's a feature, not a bug. You update the documentation as the system changes. Ideally, documentation update is part of the development process. When you change the code, you update the docs. Use AI to help with this too. "Here's the code change. Update the documentation to match."
Q: Can AI generate architecture diagrams?
A: Claude can generate descriptions of diagrams (text-based descriptions or diagram syntax like Mermaid). It can't generate images directly, but you can use the text descriptions to create diagrams in other tools. Or use AI image tools to convert descriptions to actual diagrams.
Key Insight
Legacy systems aren't mysterious because the code is complex. They're mysterious because no one documented them. AI can read code and extract the knowledge at scale and speed that was impossible before. The result: an engineer who used to spend weeks understanding a system can now do it in days. You can onboard new engineers faster. You can reduce operational risk. You can make the system less dependent on any one person's knowledge. This isn't about replacing people. It's about giving people superpowers.
On This Page
Watch the Lecture
The Knowledge Problem
What AI Can Do
Knowledge Extraction Process
Implementation Guide
Scaling Knowledge
Monday Morning Action
FAQ
Chapter Details
Part ofChapter 7
Skill.re