# Semantic Code Search vs. grep: When to Use Each

Source: https://kivgraph.dev/blog/semantic-code-search-vs-grep/
Kivgraph blog, licensed Apache-2.0 (https://spdx.org/licenses/Apache-2.0.html).

---

Use grep for literal text and semantic code search for relationships between program entities. The two approaches solve different problems, and a good coding workflow keeps both available.

## What grep does well

grep is fast, ubiquitous and often the cheapest way to answer a narrow question. The [GNU grep manual](https://www.gnu.org/software/grep/manual/) defines it as a tool for printing lines that match a pattern, which is exactly why it is useful for literal text.

Typical literal-text questions include:

- Does this exact string exist?
- Which files mention a configuration key?
- Where is a log message written?
- Does a small repository contain a known token?

For a rare name or a simple absence check, text search can be the right first move. It has almost no setup cost and gives the agent raw evidence to inspect.

## Where text search becomes ambiguous

Source code reuses names. A method, type or variable can appear in multiple packages and repositories. An occurrence can also be a comment, a string, a shadowed local variable or a different declaration with the same spelling.

Text search does not establish that one occurrence calls another. It also cannot naturally answer which dependency paths leave a repository or what is affected transitively by a change.

## What semantic search adds

Semantic code search uses language-aware facts to connect declarations and references. The result can carry the repository, file, symbol, line range, confidence and provenance needed to decide whether the relationship is exact or unresolved.

Typical questions include:

- Who calls this function?
- Which declarations does this implementation reference?
- Which repositories consume this exported type?
- What reaches this package within two dependency levels?

Kivgraph exposes these questions through [find_references](/docs/tools/find-references/), [trace_dependencies](/docs/tools/trace-dependencies/) and [find_cross_repo_consumers](/docs/tools/find-cross-repo-consumers/).

## The right workflow for an AI agent

Start with the cheapest method that can answer the question. Move to semantic tools when the question depends on identity or structure:

1. Search text for literals, configuration and rare terms.
2. Resolve the symbol when multiple declarations or imports are possible.
3. Query references, dependencies or impact.
4. Read only the returned source needed for the change.

This is not a claim that a graph wins every query. Kivgraph publishes a [benchmark](/comparison/) that includes the cases where grep is cheaper, as well as the cases where structural queries avoid repeated exploration.

## A simple rule

If the question contains “where does this text occur?”, use grep. If it contains “what does this symbol mean?”, “who calls it?” or “what breaks if it changes?”, use semantic code search.
