Sub topic 10
Explaining code that spans many files
This chapter assumes you can already write a single-snippet explanation prompt, run small pieces of code to check claims, and search a project with grep or your editor. Here you apply those skills when the code you don't understand is spread across a project.
Why many files are harder
An assistant can only explain what it can see. If you paste one file that calls load_settings(), the assistant has to guess what that function does. Guessing is where made-up details come from.
You also can't paste a whole project. Every assistant has a limit on how much text it can read at once, and quality drops when the input is crowded with unrelated files. The goal is to share the right files, not all of them.
Trace one path, not the whole project
Don't ask "Explain this repository." The answer will be vague. Pick one behavior and follow it from its entry point:
- A command-line command, such as
app import. - A web route, such as
POST /orders. - A scheduled job or an event handler.
Then ask about that path only. "What happens between the request arriving and the database write?" is a question with a bounded answer.
Give a map before the files
Start with a map: a short list of files and the names defined in each. The assistant can then tell you which files it still needs. This script prints such a map for the Python files in a folder.
import ast
import sys
from pathlib import Path
SKIP = {".git", ".venv", "venv", "__pycache__", "node_modules"}
def outline(root):
root = Path(root)
for path in sorted(root.rglob("*.py")):
if SKIP & set(path.parts):
continue
try:
tree = ast.parse(path.read_text(encoding="utf-8"))
except (SyntaxError, UnicodeDecodeError):
print(f"{path.relative_to(root)} (could not parse)")
continue
print(path.relative_to(root))
for node in tree.body:
if isinstance(node, ast.ClassDef):
print(f" class {node.name} (line {node.lineno})")
elif isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
print(f" def {node.name} (line {node.lineno})")
if __name__ == "__main__":
outline(sys.argv[1] if len(sys.argv) > 1 else ".")Run it with python outline.py path/to/project. For other languages, a directory listing plus grep -n "def \|class " or your editor's symbol view gives a similar result.
Write the prompt in two steps
First, share the map and the question, and ask which files are needed.
I'm trying to understand how an order is created in this Python web project. I don't know the codebase.
Here is an outline of the project (files and top-level names):
<outline>
routes.py
def create_order_route (line 12)
schemas.py
class OrderIn (line 5)
orders.py
def create_order (line 20)
db.py
def save (line 8)
</outline>
Question: what happens, in order, when POST /orders is called?
Which of these files do you need to see to answer accurately? List them. Don't guess about code I haven't shared.Second, paste the files it asked for, each labeled with its path, and ask for the walkthrough. Labels matter because the assistant will refer to files by name, and you will need to check those names.
If you use a terminal or editor assistant that can search the project itself, you can skip the pasting. You should still ask it to name the files it read, so you know what its answer rests on.
Check the references
Multi-file explanations fail in a few typical ways. Each one has a cheap check.
| Failure | How to check |
|---|---|
| Names a function or file that doesn't exist | Search for the exact name with `grep -rn "name" .` |
| Assumes what an unshared function does | Ask "which parts of this did you infer rather than read?" |
| Misses a second definition with the same name | Search for the name and count the matches |
| Describes the framework's usual behavior, not this project's setup | Open the configuration file and compare |
Build up, then confirm by running
Once the first path makes sense, move outward one step at a time. Ask about the function the path calls next, or about the module that configures it. Each question should use the earlier answers as context, so keep them in the same conversation or restate them briefly in a new one.
To confirm the whole path, run it. Add a temporary print or use a debugger at the entry point and at each file boundary. If the order of output matches the assistant's walkthrough, the explanation holds. If it doesn't, give the assistant the real output and ask it to reconcile the difference.
Exercise
An assistant explains how a login request is handled and says it calls check_token() in auth/session.py. You can't find that function. Give two things to do next, and say what you should not do.
Show answer
Search the whole project for check_token and for similar names, in case it is defined elsewhere or was renamed. Then ask the assistant which file it read that contained the call, or share the real files involved and ask it to redo the walkthrough from them. Don't accept the explanation, or build on it, until each name it uses is found in the code.
Exercise
Why is it better to ask for an outline-based list of needed files than to paste as many files as will fit?
Show answer
Unrelated files crowd the input and can lower the quality of the answer, and the assistant still may not receive the one file that matters. Asking which files it needs keeps the context small and relevant, and makes visible what the explanation is based on.