Semantic code search: find code by what it does, not what it's called
How semantic code search works (passages, embeddings, nearest neighbours), when it beats grep and when it doesn't, why it can run on your laptop, and how it saves an AI agent from reading the wrong files.
You know what the code does. You don't know what someone called it. You grep for "retry" and get 400 lines across tests, logs and a vendored library; the function you want is scheduleDunning, in a file called billing-jobs.ts, and the word "retry" appears nowhere in it.
Semantic code search answers a different question than grep. Grep asks "where is this string?". Semantic search asks "where is the code that does this?", and takes a sentence: where failed payments are tried again.
#What is semantic code search?
It finds code and notes by meaning rather than by matching characters. Each piece of the codebase is turned into a list of numbers, a vector, by an embedding model trained so that texts about the same thing land close together. Your query is turned into a vector the same way. The results are the passages whose vectors are closest to the query's.
That's why it finds scheduleDunning for "retry failed payments": the model has seen enough code and prose to place "dunning", "charge again" and "retry payment" near each other. It also works across languages, so a question in French can find an English comment, and across file types, so the same query surfaces the function, its test and the design note that explains it.
#How does it work under the hood?
Four steps, each with a choice to make:
- Cut files into passages. A whole file is too coarse, a single line too thin. Most tools cut at natural boundaries (blank lines, new functions, Markdown headings) into passages of roughly a thousand characters, and keep the path and line numbers with each one.
- Embed each passage. The embedding model turns it into a vector of a few hundred numbers. This is the slow part, done once per passage and again only when the file changes.
- Store the vectors. For a repository of a few thousand files, that's tens of thousands of passages, a few dozen megabytes. A flat file searched by brute force answers in tens of milliseconds; you don't need a vector database until you index millions of passages.
- Search. Embed the query, compute its similarity with every passage, sort, show the best ones with their lines.
| Choice | Typical | What it changes |
|---|---|---|
| Passage size | 500 to 1,500 characters | Too small loses context, too large blurs meaning |
| Model | A small open embedding model | Quality, languages, speed |
| Vector length | 256 to 768 numbers | Some models are trained so you can keep only the first numbers, for a smaller index |
| Where it runs | Your machine, or a server | Privacy, and who pays for the compute |
| Refresh | On change | A stale index finds code that's gone |
#Is semantic search better than grep?
Neither replaces the other. They fail in opposite ways:
| grep / ripgrep | Go to definition | Semantic search | |
|---|---|---|---|
| You know | The exact string | The symbol | What the code does |
| Strong at | Identifiers, error messages, regexes | Following calls, renaming | Concepts, synonyms, other languages |
| Weak at | Synonyms, vague questions | Anything you can't click on | Exact names, code written a minute ago |
| Setup | None | A language server | An index |
Searching for ERR_PAYMENT_DECLINED with embeddings is silly: grep finds it at once and exactly. Searching "the screen that edits a listing" with grep is hopeless unless you guess the file name.
The best tools are hybrid. They rank by meaning, then move up the passages that also contain the query's words as written. That catches the case where you half-remember a name, and keeps a literal match from being buried under passages that are only roughly about the topic.
#Should the index run locally or in the cloud?
A few years ago, embedding a codebase meant sending it to an API. Today, good embedding models are small: a few hundred megabytes, quick on a laptop's GPU. That changes the trade-off:
- Local keeps the code on your machine, works offline, and costs nothing per query. The first indexing takes a minute or two for a mid-sized project; after that only changed files are read again.
- Cloud makes sense for a monorepo too large for one machine, or a shared index for a whole company.
Several AI editors send chunks of your code to their servers to compute embeddings. If your code can't leave the building, check where the index is built before you turn it on.
#Do AI coding agents need semantic search?
Claude Code doesn't keep an index. It finds code the way a developer new to a project would: it greps, lists folders, opens files and follows imports. That works well when names are good and the agent knows roughly where to look.
On a large or unfamiliar codebase it gets expensive. An agent asked to "fix the bug where failed payments are retried twice" might grep for retry, open six files that mention it, read three hundred lines of the wrong one, and only then find scheduleDunning. Every one of those reads lands in its context window and is billed again on every later turn.
Given a semantic search tool, the agent asks the question first, gets the two or three right passages with their line numbers, and greps from there. Fewer wrong files read means fewer tokens, a context that stays focused longer, and fewer compactions. See where Claude Code's tokens go.
#How do you get good results?
- Ask in sentences. "Where we decide if a user can see a project" beats "permission check". The model was trained on meaning; give it some.
- Index the notes too. Design docs, ADRs and READMEs answer "why" questions that code can't.
- Leave out the noise. Whatever git ignores, lock files, minified bundles and generated code only add near-duplicates. Leave out keys and passwords as well: an index is one more copy of whatever it reads.
- Re-index what changed, often. Every few minutes, and when a folder is added. Re-embedding the whole project on each change wastes the GPU.
- Show lines, not files. A result should open the file at the passage, with it highlighted. A list of file names sends you back to grep.
#In Relais
The Explorer, a plugin of Relais, is a window on every project's folders with a search that does all three at once: ⌘P matches file names by a few letters in order ("retpay" finds retry-payments.ts), passages by meaning, and lines holding the words as written. Search by meaning is a setting, off at first. Turned on, Relais downloads llama.cpp's server (about 20 MB) and Google's EmbeddingGemma 2 in an 8-bit build (310 MB), each checked against its SHA-256, and everything runs on your computer: nothing indexed leaves it.
Files are cut into passages of about 1,200 characters at blank lines and new blocks (Markdown at its headings), and each vector is cut to its first 512 numbers, which the model is trained for. What git ignores, lock files, minified files, keys and passwords are left out. Relais's own 400 files index in about a minute and a half on an M3 Max; after that only what changed is read, every 10 minutes and when the window comes back. On a Mac the model runs on the GPU and stops three minutes after the last search. Results rank by meaning, with passages holding your words moved up, and a click opens the file at the passage. Another setting gives Claude sessions a search_by_meaning tool over their project, so they find the place before they grep. More on the Explorer page and all features.