← All articlesSecond Brain

How to Read Any Codebase with AI: A Practical Guide

Facing an unfamiliar codebase? Learn how to use AI to map the structure, find the real entry point, and build genuine understanding — not just surface-level reading.

You fork a repo. Open the project in your editor. And stare.

There are 40-something folders, each with their own subfolders. A main.ts, a server.js, something called middleware. The README says "just clone and run" but doesn't explain what any of it actually does. You have no idea where to start.

This experience is nearly universal for new builders — and it doesn't go away as you get more experienced. Open source projects, inherited codebases, npm package internals when a bug goes deep: unfamiliar code is an unavoidable part of shipping software. The question isn't whether you'll encounter it. It's how you'll navigate it.

AI makes this dramatically easier. Not by writing the code for you, but by acting as a knowledgeable guide who has already read every file and can answer your questions. Here's how to use it well.

Why reading from the top doesn't work

The natural instinct is to open index.js and start from line one. For small scripts, this works. For anything real, it's a trap.

Real codebases aren't designed to be read — they're designed to run. The entry point usually just initializes things. The interesting logic lives in modules called three layers deep. If you start at the top, you'll drown in setup code before you ever see what the software actually does.

AI offers a way out of that trap: ask about the structure first, and read the details afterward.

Step 1: Ask for the map

Before opening a single file in detail, drop the directory listing into your AI chat and ask:

"Here's the folder structure of a codebase I'm trying to understand. Can you give me a mental map — what each major directory seems to be responsible for, and how they're likely connected?"

Even with just folder and file names, a capable AI can usually tell you: "This looks like a Next.js app — app/ is your routes, lib/ is shared utilities, components/ is your UI, and prisma/ means they're using Prisma for database access."

That mental map doesn't need to be precise. It just needs to orient you before you dive in. Think of it as getting the legend before you read the terrain.

Step 2: Find the real entry point

The file named index.js isn't always the actual entry point to the interesting logic. Ask AI to help you find it:

"Looking at the main file and package.json, where does the core logic actually start? I want to trace the primary data flow — not just the startup code."

For a web app, the real entry point might be the first route handler that does something meaningful. For a CLI, it's the command parser. For a library, it's the main exported function.

Once you have that starting place, you can follow the logic outward — and AI can walk alongside you at every branch.

Step 3: Ask about patterns, not lines

This is the shift that makes the biggest difference.

Beginners ask: "What does this function do?"

A more useful question: "What pattern is this code following, and why might someone structure it this way?"

The second question returns something you can actually learn from and carry forward. When AI tells you "this is a repository pattern — it abstracts database access so the business logic doesn't need to know which database you're using," you've gained something that applies to a hundred future projects, not just this one function.

// Paste the function, then ask:
// "What design pattern is this, and what problem does it solve?"
class UserRepository {
  async findById(id) {
    return db.users.findUnique({ where: { id } })
  }
  async save(user) {
    return db.users.upsert({ where: { id: user.id }, create: user, update: user })
  }
}

The pattern answer also helps you predict how the rest of the codebase is organized — once you know what vocabulary a team is working in, you can navigate by instinct.

Step 4: Understand the why, not just the what

The most valuable thing AI can give you when reading unfamiliar code isn't a summary of what the code does. It's the reasoning behind design decisions.

Ask things like:

  • "Why might they have separated this into two files instead of one?"
  • "This logic checks the same condition in three different places — is that intentional or a sign of something else?"
  • "Why are they using useReducer here instead of useState?"

The answer to why turns code into knowledge. The answer to what just confirms what you could have guessed from reading.

Sometimes the why answer reveals constraints you couldn't have seen from the code alone — a performance concern they were working around, a framework limitation, or a decision that made perfect sense two years ago but is now slightly awkward. Understanding the context behind code is what separates comprehension from pattern-matching.

This is also why comprehension debt accumulates so quietly when you skip this step — you can produce working code without ever building a real mental model. Reading other people's code is one of the best correctives.

Step 5: Build understanding in layers

Don't try to understand everything at once. The professional approach to an unfamiliar codebase is: understand enough to do the next thing, then go deeper as the need arises.

A practical workflow:

  1. Get the map — paste the directory structure, get a high-level orientation
  2. Find the entry point — locate where the core logic begins, follow the main data flow
  3. Pick the module most relevant to what you need to do
  4. Ask AI to explain that module — its role, its inputs and outputs, how it connects to what you've already read
  5. Surface any surprising choices — anything that looks unusual is worth asking about
  6. Make one small change to test your understanding — does the behavior match what you predicted?

That last step is underrated. There's a difference between understanding that sounds plausible and understanding that's actually correct. A small, targeted change tells you which one you have.

When AI gets it wrong

AI occasionally misreads code, especially in large files with complex state or side effects. The most common failure mode: it explains what a function looks like it does without catching how it interacts with global state or external dependencies.

Two things help:

Paste more context. AI gets things wrong most often because it's only seeing part of the puzzle. Include the file that calls the function, the relevant type definitions, or the test file. More context means more accurate explanations.

Invite self-doubt. After getting an explanation, try: "Are there any parts of this you're less confident about? What might you be missing?" Models surface genuine uncertainties when you ask directly — and that information helps you know where to look more carefully yourself.

The habit that compounds

Each time you encounter a new pattern in an unfamiliar codebase — the repository pattern, a particular way of handling async errors, a state machine approach you hadn't seen before — write it down. Not elaborately. Just: what it's called, what problem it solves, where you saw it.

Keeping a coding journal is one of the best habits a builder can develop, and reading unfamiliar code with AI is one of the richest sources of entries. Over time, you build a personal vocabulary for code — and you start recognizing patterns before AI names them.

That's the inflection point: you stop using AI to understand code, and start using AI to go deeper, faster. Reading becomes learning. And learning compounds.


The takeaway: When you land in an unfamiliar codebase, don't start at line one and read down. Start with the map, find the real entry point, ask about patterns and why, and build your understanding one layer at a time. AI is your guide through the code — not your replacement for understanding it.

#ai#codebase#learning#understanding#beginners#developer-workflow

Build your own product with Codepet

Codepet is a macOS app that coaches you from your first line of code to a shipped product — with an AI crew that grows as you do.

Get Codepet