Skip to content
mentoor

Topic 5 of 5 · 5 min read

Agentic search with grep and file tools

Goal: Choose between an index and plain file search tools for a given corpus and agent, citing freshness and exact-match needs.

For a codebase or a folder of files, an agent can often search with grep, glob and file reads and skip the vector index, because exact matches, fresh files and zero setup matter more than fuzzy meaning.

Check yourself · 1 of 3

Why might an agent prefer grep to a vector index when asked where the constant MAX_RETRY is used?

Choose one answer

Why it matters

Coding agents such as Claude Code and Cursor explore a repository the way a developer does: list files, grep for a symbol, open the file, follow the imports. There is no embedding step, no index to build and nothing to go stale. When you design retrieval for an agent, you should know when that is the better choice.

The tools

  • Glob finds files by name pattern: src/**/auth*.ts.
  • Grep finds lines by exact text or regular expression: grep -rn "createSession(" src/.
  • Read opens a file, or a line range of it.
  • List shows a directory tree, which tells the agent how the project is organised.

The agent chains them: glob for the module, grep for the function, read the 40 lines around the match, grep for callers.

When file search wins

  • Exact identifiers. Function names, error codes, config keys and ticket ids need exact matching. Embeddings may rank a similar name above the right one.
  • Fresh content. Files change all the time. File tools read the current state; an index is only as new as its last build.
  • Structure matters. Paths, imports and directory names carry meaning that the agent can follow.
  • Small to mid-size corpora and no ops budget. Nothing to host, no chunking choices.

When an index wins

  • Meaning, not wording. Users ask in their own words and documents use others. "Cancel my plan" versus a page titled "Terminating a subscription".
  • Very large corpora. Grepping millions of files per query is slow.
  • Unstructured text. Many PDFs and wiki pages have no useful paths or symbols.
  • Cross-language or paraphrase matching.

Many systems mix them: a semantic search tool and a grep tool, and the agent picks per question.

Before and after

Weak: embedding a 300-file repository, re-indexing nightly, and answering "where is MAX_RETRY used?" from nearest-neighbour chunks that miss two of the seven uses.

Better:

grep -rn "MAX_RETRY" src/ tests/

All seven uses, current as of this second, with line numbers.

What goes wrong

  • Grepping a huge, unstructured corpus and filling the context with matches. Limit output and narrow by path.
  • Using an index for exact-match needs and trusting a near miss.
  • A stale index answering about code that was changed an hour ago.
  • One search style only. Mixed questions need both exact and semantic tools.

Examples

A chain of file tools

Task: "Where do we validate the session token?"

glob  src/**/session*.ts      -> src/auth/session.ts
grep  "validateToken" src/    -> 3 matches
read  src/auth/session.ts 40-90
grep  "validateToken(" src/   -> 5 callers

Four cheap calls, no index, and every line number is current.

Where grep falls short

A help-centre corpus of 40,000 articles. A user asks "how do I stop being charged?". The right article is titled "Ending your subscription" and never uses the words "stop" or "charged". Grep returns nothing useful. A semantic index matches on meaning and finds it.

Key points

  • A coding agent can explore a repository with glob, grep, list and read tools and no embedding index.
  • File search wins for exact identifiers, content that changes often and structured trees with meaningful paths.
  • An index wins when users and documents use different wording, when the corpus is very large or when text is unstructured.
  • File tools read the current state of the files, while an index is only as fresh as its last build.
  • Many agents offer both an exact search tool and a semantic one and choose per question.

Common mistakes

  • Building an embedding index for a small, fast-changing repository where grep would be exact and current.
  • Using grep over a large prose corpus and flooding the context with weak matches.
  • Trusting nearest-neighbour results for an exact identifier such as an error code.
  • Treating the choice as all or nothing instead of giving the agent both tools.

Check yourself · 2 of 3

A help centre has 40,000 articles. Users ask in casual words and the articles use formal titles. Which retrieval setup fits best?

Choose one answer

Check yourself · 3 of 3

A team indexes its repository nightly. An agent answers a question about a function that was renamed this afternoon, and gives the old name. What is the cause?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic