Currently Available: Need a skilled Software Developer for your next project?
Categories
Coding Agents

Semantic Code Search vs. grep: How Coding Agents Find Relevant Code and When Codebase Indexing Helps

When a coding agent needs to change an unfamiliar repository, it first has to find the right files. The agent pairs a language model with tools that inspect files, search text, and sometimes trace how pieces of code connect. To locate what it needs, the agent can run grep to match exact words, or it can run a semantic search to find code that matches a description by meaning.

Grep scans files for literal text or patterns. A semantic search tool ranks code by how closely it matches the meaning of a query, even if the query uses different words than the code. Behind the scenes, an index gives the search tool a prepared map of the codebase so it can find candidate files or snippets quickly. You get better results from running grep when you already know the exact identifiers or strings to look for, and you get better results from running semantic search when you know what the code does but not what the authors named it. Agents work most reliably when they use both, inspect what comes back, and refine their next search.

What grep and semantic search find

Grep-style tools look for literal text or a regular expression, which is a pattern that describes a set of strings. Developers often use rg (ripgrep) to scan a repository and its subdirectories. By default, ripgrep respects .gitignore, so it skips files that the repository already excludes. Its user guide also lists flags for changing which files a search covers.

Running grep works well when a task gives the agent a known identifier, error message, configuration key, or API name. If you ask where retryDelay is set, grep finds every file containing that exact string, and you can verify the pattern and the matches yourself. If the codebase calls the same setting backoffInterval instead, a search for retryDelay misses it completely.

Semantic search bridges that vocabulary gap. A semantic search tool ranks code by how closely it relates to a plain-language query, such as “where does the client wait longer before retrying a failed request?” Many of these tools convert both the query and code snippets into embeddings, which are numeric representations of their content, and then rank the snippets by how similar those numbers are. The term semantic search describes what the tool retrieves rather than a single required implementation. For example, Sourcegraph’s Cody documentation lists several context sources, including keyword search and code-graph context.

Search researchers also frequently refer to BM25. BM25 ranks files based in part on how many query terms they contain and how rare those terms are across the repository. Like keyword search, it relies on matching words, yet it behaves differently from grep because it returns a ranked list instead of every literal match. That difference matters when you read benchmarks. A study comparing dense retrieval with BM25 compares embeddings against a ranked keyword algorithm rather than against a shell command.

Why coding agents combine search tools

An agent rarely has to stop after its first query. It can open a promising file, read the surrounding code, search for a function name it just spotted, and then trace where the rest of the project calls that function. Text search, semantic search, file reading, and symbol-reference tools each answer a different question. Symbol references show where a named function or variable is defined or used; semantic similarity only suggests that two pieces of code talk about a similar topic.

VS Code’s workspace documentation describes how agents combine these tools and pull only the relevant snippets into their context. The agent does not need to load the whole repository into the language model at once. Even so, it still has to read the candidate files and confirm that they fit the task, because a high ranking does not prove that a search caught every relevant file.

In practice, an agent often starts with semantic search when a task describes a behavior, then switches to grep to check exact identifiers and list every occurrence. Once the agent spots a central function, it can use symbol tools to trace callers and definitions. For example, an agent looking for retry logic might search with a description of failed-request handling, read a candidate function, and then run grep for that function’s name to find every call site.

What recent comparisons show

A November 2026 preprint, Embeddings vs. Grep: Rethinking the Dense Advantage in Code Search, showed that the measured edge of dense, embedding-based retrieval depended heavily on how the comparison was set up. The researchers compared BM25 searches over whole files against dense retrieval over chunks, which are smaller pieces of code. They found that changing the chunk size could flip the direction of the measured difference. Those findings describe the benchmark rather than a universal ranking of search methods, because the preprint’s experiments covered specific repositories, query sets, and retrieval configurations.

Query vocabulary also shifted the results. When the researchers added code-specific terms such as identifiers, API names, or error strings to the queries, BM25 gained more than dense retrieval did. In the tested Python pools, expanded whole-file BM25 matched or beat dense retrieval by point estimate. Meaning-based retrieval helps when the agent does not know the codebase’s terminology, while lexical retrieval catches up once the agent has those terms.

The same preprint tested agents on 96 tasks. Across shell grep, lexical indexing, dense indexing, and a router, the agents scored within about 0.04 of one another on exact-match file localization. The benchmark counted an exact match when the agent’s final edit touched the exact set of files in the gold set. Because the researchers did not run test suites, the score measures whether the agents found the right files rather than whether their edits worked. On a separate 30-task subset, localization scores varied more across different language-model configurations than across search tools. The sample is small, so it does not settle the question, but it warns against assuming that swapping the search tool alone will reliably improve an agent’s final code.

On a 539-query lockbox in the same benchmark, combining lexical and dense rankings beat either method alone. That result shows the two methods complemented each other in the tested setup, though it does not guarantee that every hybrid system will outperform a single method. Earlier CodeSearchNet research also depended on the specific models and datasets tested. A 2021 study found that BM25 beat several pre-BERT neural baselines, while its conclusion did not find that BM25 outperformed CodeBERT. Because models, datasets, and evaluation tasks vary so much, no single benchmark delivers a universal verdict for every coding agent.

When codebase indexing helps

You get the most value from building a codebase index when a repository is large or unfamiliar, when an agent searches the same code repeatedly, or when tasks describe behavior without naming the implementation. The index gives the search tool a prepared map for finding candidate snippets across the project, without forcing the agent to load the entire repository at once.

Relying on an index also brings operational trade-offs that direct text search avoids. The indexing tool has to include the files your task depends on, keep up with your changes, and follow your organization’s data policies. In VS Code, .gitignore and files.exclude rules affect workspace indexing, and an existing index might not reflect recent local edits. If semantic search is unavailable or misses a file, VS Code points agents to text search, grep, file search, and language tools instead.

Product design affects privacy too. GitHub states that semantic indexing for non-GitHub workspaces in VS Code uploads indexable files to GitHub so its service can search them. That behavior applies to GitHub’s documented setup rather than to every semantic search tool, and GitHub’s repository indexing documentation explains its current policies and behavior. Before you turn on indexing for private code, check your provider’s current terms and your organization’s rules.

Run a direct text search when a task requires a literal match or a complete audit of occurrences. If you need to find every use of a deprecated API, search for the exact API name and verify that your search covers all the relevant files, because a ranked semantic list cannot guarantee that every match appears in the results. Use symbol tools when you need to know where a named function is defined or called, and use semantic search when you know the behavior you want but not what the code calls it.

What I'm building

Delegate tasks. Get software.

Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.

Take a look at vroni.com

Email updates

Usually a new article and a few links I found interesting.

No spam. Unsubscribe with one click.

Leave a Reply

Your email address will not be published. Required fields are marked *