Skip to content

BlogEngineering

AI agent grep token cost: why agents grep files

· 4 minute read

AI agent grep token cost is the share of a coding agent's context spent on search results and the file reads that follow them. Agents find code by text search, and every match, plus every file opened to check a match, is billed as input. On structural questions, a code graph can return the answer without that loop. Graphify, an open-source CLI, replaces many of those searches with graph queries.

Why does an AI agent use grep at all?Link to Why does an AI agent use grep at all?

Grep is cheap, fast, and needs no index. Anthropic's guide to context engineering for agents describes this "just in time" approach. The agent keeps light references, such as paths, and loads data with tools like grep and glob when needed. That avoids stale indexes. The cost is that search output lands in context.

How big is one grep result?Link to How big is one grep result?

We measured one search. Method: httpx at commit b5addb6 (23 Feb 2026), about 8,800 lines of Python in the httpx package. Command: rg -n "send" httpx. Date: 13 May 2026.

The search returned 60 matching lines and 4,814 characters. At a rough 4 characters per token, that is about 1,200 tokens for one search. A narrower rg -n "def send" httpx returned 188 characters. Results vary by codebase and by query.

Where does the rest of the cost come from?Link to Where does the rest of the cost come from?

The search is the first step. To answer "what calls send?", the agent opens the files behind the matches to read context. Each read adds a file or a large slice of one. If the first search misses, the agent searches again with a new pattern. Those loops repeat, and each one stays in the conversation history.

| Step | What enters context | |---|---| | Broad search | Every matching line | | Narrower search | Fewer lines, still text matches | | File reads | Whole files or large ranges | | Retry | Another round of all three |

Why does text search miss structure?Link to Why does text search miss structure?

Grep matches strings, not meaning. A search for send also hits comments, docstrings, and variable names. It cannot tell a call from a definition, and it cannot follow an import to the file where a symbol lives. The agent rebuilds those links by reading code.

A code knowledge graph stores calls and imports as edges. Graphify, an open-source CLI, builds one with tree-sitter, locally and with no model for code:

bash
uv tool install graphifyy
graphify update .
graphify explain ".send()"
graphify path "handle" "verify"

explain lists a node's connections. On the same httpx checkout, graphify explain ".send()" resolved to .send() in httpx/_client.py at line 879. The full output was 515 characters. Node labels follow the parser, so methods carry a leading dot. path returns the chain between two symbols. graphify query "<question>" --budget 800 caps output at a token budget. The agent opens only the files the answer names.

The graph does not remove grep. Text search still wins for string literals, log messages, and config values.

How do you measure AI agent grep token cost?Link to How do you measure AI agent grep token cost?

Run five real tasks with your agent and note token usage for each. Then build a graph, add its instructions with graphify claude install or the command for your tool, and repeat. Our guide to measuring token spend per task covers the method.

How can an agent grep more cheaply?Link to How can an agent grep more cheaply?

Search in two steps. First run rg -l pattern to list only matching file names. Then search inside the one or two files that matter. Add -m to cap matches per file. This keeps each result small. It also keeps later turns cheaper, because every earlier result stays in the conversation.

For questions about callers or impact, skip grep and ask the graph.

FAQLink to FAQ

Is grep output always the biggest cost?Link to Is grep output always the biggest cost?

No. File reads are often larger. Grep starts the chain that leads to them.

Does a semantic index fix this?Link to Does a semantic index fix this?

It helps find relevant text. It does not store who calls whom, so structural questions still need reads.

Will the graph be stale after edits?Link to Will the graph be stale after edits?

Run graphify update . after edits, or install the git hook with graphify hook install.

Does Graphify work with Codex and Cursor?Link to Does Graphify work with Codex and Cursor?

Yes. See graphify codex install and graphify cursor install.

Get startedLink to Get started

Graphify is open source on GitHub. Install it with uv: uv tool install graphifyy. Read the querying docs and the token reduction glossary entry. Related: Aider repo map vs a code knowledge graph.

Try Graphify now.

Try for free