Sub topic 7
Reading Only What You Need from the Codebase
This chapter assumes you have used a coding agent in a terminal, know basic shell commands such as grep and sed, and understand that the agent has a limited context window. Every file the agent reads stays in the context for the rest of the session. Reading with a plan keeps that cost low.
Why reading is the largest cost
Reading is usually the biggest source of context growth. A 1,500-line source file can take thousands of tokens. If the agent opens five such files to change one function, most of that text is irrelevant. It still sits in the context, and later turns pay for it. It can also distract the agent from the lines that matter.
The fix is to read in steps, from cheap and broad to expensive and narrow.
Search first, then read a range
Start by locating the code, not opening it. A search with a capped result list returns file names and line numbers. Then print only the lines around the match.
# Find the definition first, then read only the lines around it
grep -rn "def parse_config" src | head -n 20
sed -n '120,180p' src/config.pyThe head -n 20 caps the search output in case the pattern matches hundreds of lines. The sed -n '120,180p' command prints lines 120 to 180 and nothing else. That is about 60 lines in the context instead of the whole file.
Use structure before content
Many agents offer tools that list the symbols in a file, such as function and class names with their signatures. An outline of a 1,500-line file might take 40 lines. From it you can decide which function to read. Use these tools when they are available.
A directory listing with a limited depth serves the same purpose for folders. Listing src/ one level deep shows the layout without opening anything.
Ask for ranges explicitly
An agent tends to read whole files unless told otherwise. You can steer it in the prompt:
- "Find where
parse_configis defined and read only that function." - "Search for callers of
load_settings, list the file names, and don't open them yet." - "Read lines 100 to 200 of
config.py."
Naming a symbol or a range gives the agent a narrow target, so it has no reason to read everything nearby.
Common errors
| Mistake | Effect | Better approach |
|---|---|---|
| Opening whole files to find one function | Thousands of unused tokens stay in context | Search, then read a line range |
| Searching without a result cap | A broad pattern returns hundreds of lines | Add `head -n 20` or a narrower pattern |
| Reading the same file again after an edit | Duplicate copies of the file in context | Re-read only the changed range |
| Reading generated or vendored code | Large files with little useful content | Exclude `node_modules`, `dist`, `build`, and lockfiles from searches |
| Reading a range that is too tight | The agent misses an import or helper and reads again | Include a margin of 10 to 20 lines |
When to read the whole file
Narrow reading is a default, not a rule. Read a whole file when it is short, when you are about to rewrite most of it, or when the logic depends on state spread across the file. A 60-line module is cheaper to read once than to read in three ranges. The goal is to make each read a deliberate choice.
A worked example
Suppose you need to add a default value to parse_config. A low-cost sequence looks like this:
- Search for
def parse_configand get one match atsrc/config.py:131. - Read lines 120 to 180 and see the whole function.
- Search for
parse_config(with a cap of 20 results to find callers, and note which tests mention it. - Edit the function, then re-read only the edited lines to confirm the change.
The whole task touches roughly 100 lines of source. Opening config.py and each caller in full could cost ten times that.
Exercise
You need to find where load_settings is defined in a large repository and understand how it works. Write the two shell commands you would run first, and say why they cost less than opening the file.
Show answer
Run grep -rn "def load_settings" src | head -n 20 to get the file and line number, then sed -n 'START,ENDp' path/to/file.py with a range around that line, for example 10 lines before it and 50 after. The search returns at most 20 lines, and the range read returns only the function and its surroundings. Both are small compared with a whole file, which would stay in the context for the rest of the session.