Claude Kept Reading Entire Files - Give It a Search Engine Instead
Claude Kept Reading Entire Files - Give It a Search Engine Instead
I watched my AI agent burn through tokens reading a 3,000-line file to find a single function definition. It read the whole thing, processed it, found the 12 lines it needed, and moved on. Then it did the same thing with the next file. And the next.
This is the default behavior for most coding agents. They have a "read file" tool, and they use it by reading everything.
The Cost Problem
A 3,000-line TypeScript file is roughly 15,000 tokens. If your agent reads 20 files to understand a codebase, that is 300,000 tokens just on file reading - before it writes a single line of code. At current API prices, that adds up fast.
Worse, large file contents push useful context out of the window. The agent forgets earlier instructions because its context is full of code it already processed.
Search Indexes Fix This
Instead of giving your agent a "read file" tool, give it a search tool. Tools like ripgrep, tree-sitter indexes, or even simple grep wrappers let the agent find exactly what it needs without reading everything.
The pattern is straightforward:
- Agent needs to find a function - it searches for the function name
- Search returns the file path and line numbers
- Agent reads only those specific lines
This turns a 15,000-token file read into a 500-token targeted read. Multiply that across an entire coding session and the savings are enormous.
What a Good Agent File Tool Looks Like
The best setup gives your agent three tools instead of one: search by symbol name, search by content pattern, and read a specific line range. The agent learns quickly that searching first and reading second is faster and cheaper.
Some agents now do this automatically. Claude Code uses ripgrep and glob patterns instead of reading entire files. The result is faster responses, lower costs, and better accuracy because the relevant code stays in context longer.
Build the Index, Save the Tokens
If you are building agents that work with code, invest in search infrastructure before anything else. The difference between an agent that reads everything and one that searches first is the difference between a toy and a tool.
Fazm is an open source macOS AI agent. Open source on GitHub.