How to Read Source Code with AI: A Problem-Driven Approach
Instead of asking an AI coding tool to "analyze this project," this article shows why full-repository scans are expensive and low-value, and how a problem-driven workflow works better. You will learn how to structure a read-only opening prompt, require file path and line number for every hop, allow explicit uncertainty, and fix the output order. It then walks through verifying the rejection path for an expired token in a small time-sensitive signing library, highlights hidden branches such as negative time differences and key rotation behavior, and explains how to turn single-chain tracing into a reusable reading checklist and team convention.
1. Why “Analyze the Whole Project” Is the Most Expensive and Least Useful Instruction
When you ask an AI coding tool to read a source repository, the most costly and least rewarding sentence is usually “help me analyze this project.” The default behavior is to walk the entire repository and then produce a structural overview: which layers the project is split into, which common design patterns it uses, and roughly what each directory is responsible for. After reading it, you get the illusion of having “seen” the code, but once you close the laptop, you still cannot answer the two questions that matter most the next day: where does the code enter, and where does it return.
The root cause is that a full scan treats “amount of information” as “understanding.” A repository’s directory layout, dependency list, and layering can usually be obtained from documentation, if it exists. What is genuinely hard is control flow: where the conditionals sit after a request arrives, where exceptions are thrown, and where state changes. This kind of information cannot come from an overview; it can only be confirmed hop by hop along one concrete path.
So the more effective strategy is to reverse the order: start with a sharply bounded question and require the tool to read only along that single line, ignoring all other code. The example below uses a small library for generating time-sensitive signed tokens, commonly used for signing session data and password reset links. Its source is small enough to make the method easy to demonstrate.
2. How to Structure the Opening Prompt
A reusable opening prompt can take the following form. It works with mainstream AI coding tools and CLI-based coding assistants alike; just replace the question inside the braces:
Do not modify any files yet; perform read-only analysis only.Target question: {A user clicks an expired password reset link. On which line does the system reject the request?}Requirements:1. Trace only the single call chain related to this question; do not scan unrelated directories;2. For every hop, write file path:line number and quote the key code on that line;3. For anything that cannot be determined from static code, mark it as "uncertain" and state what information is missing;4. Output order: call chain -> key checks -> conclusion.
Four parts of this prompt are deliberate.
The first sentence is “do not modify any files yet.” While reading code, a tool that spots something it considers non-idiomatic will often refactor or optimize it in passing, and by the time you notice, the workspace is full of changes unrelated to your reading goal. If the tool supports a plan mode or read-only mode, switching to it achieves the same effect.
The second is “write the file path and line number for every hop.” This is the highest-value sentence in the whole prompt. An explanation without line numbers cannot be checked; no matter how fluent it sounds, you can only choose to believe it. With line numbers, you can compare item by item and demote the tool’s output from “narrative” to “leads awaiting verification.”
The third is allowing “uncertain” to be marked. Dynamic dispatch, reflection, config injection, decorator chains, and code generated by metaprogramming often hide the real jump target from static reading. If you do not give the tool an escape hatch, it will confidently fill in a chain that does not exist.
The fourth is fixing the output order: chain first, conclusion after. This prevents it from stating a conclusion and then reasoning backward to justify it, reducing output that “looks self-consistent but points in the wrong direction.”
3. Manual Review: Treat Line Numbers as Hypotheses to Verify
Once you have the output, the mandatory next step is to check the line numbers yourself. You can use search tools to locate symbols and keywords, then open the files to confirm the surrounding context. Take the rejection path for an expired token: a typical chain is often only three or four hops. The deserialization entry point enters the time-aware serializer’s load logic, which then moves to the internal serializer’s load method; that method calls the signer’s verification method, and the actual time check lives in the verification implementation — when the computed time difference exceeds the maximum allowed age, a “signature has expired” exception is thrown.
When you finish checking, you will usually pick up two details that an overview-style summary would never mention, and these details are exactly where production incidents tend to originate.
The first is the branch for a negative time difference: if a token’s issue time is judged to be in the “future,” it is also treated as expired. In a multi-machine deployment where clocks are not aligned, this means a link the user just received may become invalid immediately, showing up as “intermittent, not reproducible, and fine after a few refreshes.” An overview-style summary usually considers this “unimportant,” but it is a textbook symptom of clock drift.
The second is that the expiration exception propagates straight up rather than retrying verification with an older key. If the system is performing key rotation, behavior during the period when old and new keys coexist depends on this line: whether expiration and verification failure are handled differently, or collapsed into a uniform fallback retry, directly determines the user success rate inside the rotation window. This behavior is not written in the docs; it can only be read from the code branches.
These two points illustrate a criterion: the value of a chain lies not in how many files it covers, but in whether it lands on real decision branches.
4. Expanding Single-Chain Tracing into a Reading Checklist
When one chain is done, immediately move to the next question instead of asking for one large, all-encompassing report. Around signing and token components, a question list like this can be worked through in order:
- Which fields participate in computing the signature value, whether the concatenation order is fixed, and what encoding is used;
- At which step the salt acts during derivation, and how different use cases are isolated from each other;
- Which time source the age is read from, and whether the issue time and the verification time use the same clock;
- Whether verification failure and expiration take different branches, and whether callers can tell them apart;
- Whether multiple keys can coexist with fallback logic, and what triggers the fallback.
Each question maps to one chain. After three or four of them, the skeleton of the whole component emerges naturally: where the entry points are, where state changes, how failures are expressed, and where the extension points sit. Understanding assembled from questions is closer to a maintainable mental model than a summary read from beginning to end, and it is easier to invoke directly during reviews and incident triage.
5. Turning the Method into a Team Convention
This practice can be further consolidated into team agreements, reducing dependence on individual experience. At the prompt level, maintain a read-only analysis template that fixes three constraints: no file changes, line numbers required, uncertainty allowed. At the process level, require any conclusion about an unfamiliar module to come with verifiable location information; a conclusion that cannot provide a location is treated as unverified. At the collaboration level, add the hidden branches found during review to the module documentation, so the next reading starts from a higher baseline.
For repositories with large codebases and many cross-module calls, unified logging services and monitoring alerts can also align static reading with production behavior: a suspicious branch spotted in the code can be validated against real call data to see whether it is actually triggered, which tells you whether it deserves priority.
Ultimately, AI coding tools are good at local retrieval and linking under explicit constraints; they are not good at deciding for you “what is worth looking at.” Narrow the granularity of your questions to one verifiable chain, keep the verification authority in your own hands, and reading source code stops being an action that burns budget and becomes an engineering capability you can keep accumulating.
