Codebase indexing explained: how tools index a repo for AI
Codebase indexing is the process of reading a repository once and storing what a tool needs to answer questions about it quickly. The stored result, the index, might hold text fragments, vectors or a graph of functions and calls. Agents and search tools query the index instead of scanning every file again.
Every coding agent and code search tool indexes your code in some way, even if the index only lives for one session. The kind of index decides which questions the tool can answer well. This post walks through the four common kinds and how to keep any of them current.
What are the main kinds of codebase index?
There are four common kinds of codebase index: text, embedding, syntax and compiler grade. They differ in what they store and in which questions they answer quickly.
| Kind | What it stores | Answers well | Misses |
|---|---|---|---|
| Text index | Short character sequences and where they occur | "Where does this exact name or error string appear?" | Calls through aliases, interfaces and re-exports |
| Embedding index | A vector for each chunk of code | "Find code that does something like this" | Exact relationships; a caller can look nothing like its callee |
| Syntax index or code graph | Functions, classes and files as nodes; calls, imports and inheritance as edges | "Who calls this? What is the path from A to B? What does this change touch?" | References that only exist at runtime |
| Compiler grade index | Every symbol resolved the way the type checker resolves it | "Go to definition", "find all references" with full precision | Needs a working build per language; slower to produce |
Many tools combine two or more. Sourcegraph describes keyword, embedding and graph retrievers working together in lessons from building AI coding assistants, because each one surfaces code the others miss.
How does a text index work?
A text index stores small pieces of text and the places they appear, so a search can jump straight to matches. Zoekt, the engine behind Sourcegraph's code search, is built on what its README calls "trigram indexing": it records every three character sequence in the code, so a substring or regular expression search only opens files that can match.
Text indexes are the right tool for exact names, log messages and config keys. They know nothing about structure. Searching for handler returns definitions, calls, comments and unrelated functions with the same name, all in one list. We wrote about what that costs an agent in why AI agents grep.
How does an embedding index work?
An embedding index splits code into chunks, turns each chunk into a vector with a model, and stores the vectors so the closest ones to a question can be found fast. It finds code that means something similar, even when the words differ.
The limit is that similarity is not a relationship. A function and the code that calls it can look nothing alike, so a vector search for one rarely returns the other. Our comparison of a knowledge graph vs a vector database for code goes into when each one fits, and the vector embedding glossary entry has the short definition.
How does a syntax index or code graph work?
A syntax index parses every file into a syntax tree, then records the declarations and the relationships between them. With a parser such as tree-sitter, functions, classes and files become nodes, and calls, imports and inheritance become typed edges. Our post on what an AST is and how tree-sitter builds one shows that first step.
The result is a code graph. It answers structural questions in one lookup: the callers of a function, the shortest path between two symbols, or every file a change can reach. Our guide to call graphs vs dependency graphs covers which edges answer which question.
How does a compiler grade index work?
A compiler grade index runs each language's own analysis and stores every resolved reference. Language servers do this live through the Language Server Protocol. SCIP stores the same kind of result ahead of time, and its README describes it as a protocol "to power code navigation functionality such as Go to definition, Find references, and Find implementations".
It is the most precise kind of index for typed languages. It needs a working build per language and per project, so it costs more to produce and to keep current across a large, mixed repository.
How do you keep a codebase index current?
Keep an index current by rebuilding it whenever the code changes in a way an agent will rely on. A stale index is worse than none, because the agent trusts it.
Two triggers cover most setups:
- On every commit. A git hook rebuilds the index after each commit, so the map matches what is checked in.
- While you work. A file watcher rebuilds when files are saved, so the map matches the working tree.
An incremental index reparses only the files that changed, which keeps frequent rebuilds cheap. With the Graphify CLI, both triggers are one command each:
graphify hook install # rebuild after each commit
graphify watch . # rebuild while you editHow does Graphify index a codebase?
The Graphify CLI builds a code graph index on your machine. The code pass parses files with tree-sitter and makes no model calls, then writes the graph to graphify-out/graph.json in your repository.
uv tool install --python 3.12 graphifyy
graphify update .Reruns reuse a parse cache, so only files that changed are parsed again. Each run ends by reporting the node, edge and community counts of the rebuilt graph. Every edge from the code pass carries one of two provenance tags: EXTRACTED when the relationship was read straight from the syntax tree, and INFERRED when it was derived by static name resolution, with no model involved. The confidence tags glossary entry explains both.
Agents query the graph over MCP or the CLI, which gives them the structure layer of codebase context without reading every file. The install guide covers every way to set it up.
Use Graphify in your agent
Add Graphify to ChatGPT, Claude, Cursor and more in one click.