Skip to content
Definition

Semantic code search

Semantic code search finds code by what it means and how it is connected, not by matching text. A query like "who calls fetchUser?" returns the real callers instead of every line where those characters appear.

The idea, explained

Most code search is lexical: tools like grep and ripgrep match characters, so a search for getUser misses fetchUser, loadAccount, or the same function reached through an import alias. Semantic code search tries to answer the question behind the query, "where is the user loaded?", rather than the literal string. Two broad families exist. Embedding-based search chunks the codebase, encodes each chunk into a vector with a model, and ranks results by vector similarity to the query. This is the technique behind many "chat with your repo" products and is good at fuzzy, natural-language recall. Structure-based search uses the compiler's own understanding, parsing to an AST, resolving symbols and types via an LSP server, and following definition, reference, and import edges, so a hit is a precise language fact, not a probabilistic match.

The two families trade off differently. Embeddings tolerate vague queries and cross synonyms well, but results are approximate, can drift as code changes unless re-indexed, and can't tell you *exactly* which call sites would break under a rename. Structural search is exact and stays correct after edits, but it answers "where is this symbol used" better than "where is the rate-limiting logic," which has no single symbol. In practice strong systems combine layers: symbol/graph resolution for precision, plain text/regex as a fast fallback, and sometimes embeddings on top for natural-language recall.

For an AI coding agent, semantic search is what replaces the blind grep | read-file | guess loop. The agent needs to find the right definition, enumerate every caller, and understand what a change touches, questions that lexical matching answers incompletely and that an agent otherwise pays for in extra round-trips and wasted context. The quality of an agent's edits is bounded by how well it can locate the relevant code first.

Semantic search is also distinct from a one-time index dump: the value is in keeping the model current as the repo mutates, and in ranking results so the highest-impact code surfaces before noise.