How does retrieval over a codebase differ from retrieval over prose?
Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.
Model answer: the part to lead with
Enough that most prose RAG intuitions actively mislead. Four differences matter.
Full worked answer with diagram
Enough that most prose RAG intuitions actively mislead. Four differences matter.
Chunking must follow syntax, not tokens. Splitting a file every 512 tokens cuts functions in half and produces chunks that are semantically meaningless. Parse to an AST and chunk on structural boundaries: function, method, class. If a function exceeds the budget, split it but repeat the signature and enclosing class name in each piece. A chunk of loop body with no signature is unretrievable.
Chunks need synthetic context. A function body rarely contains the words a developer searches for. def _hdl(p) does not embed anywhere near 'how do we validate webhook payloads'. So enrich at index time: prepend the file path, the class, the docstring, imports, and often an LLM-generated one-line summary of what the function does. The summary is what bridges intent-shaped queries to implementation-shaped code.
Lexical retrieval matters more than in prose. Developers search for exact identifiers, error strings and function names. Dense retrieval is bad at this and BM25 is excellent, so hybrid is not optional here, and I would weight the lexical leg higher than I would for prose.
Structure is a first-class retrieval signal that vectors cannot express. 'Who calls this function?' and 'what does this import?' are graph queries, not similarity queries. Build a call graph or use an index like tree-sitter plus LSP data, and expose it as a separate retrieval path. Vector search finds semantically similar code, the graph answers dependency questions, and they are complementary rather than substitutes.
The broader point for 2026: for code specifically, agentic retrieval often beats vector search outright. Coding agents do very well with grep, file listing and go-to-definition in a loop, because the codebase is a navigable structure with exact names, not an unstructured blob. I would not build a vector index for code before checking whether ripgrep plus an agent loop already clears the bar.
Worth sketching. Three retrieval paths off one repository shows you know a vector index is one index among several, not the system.
flowchart LR
R["Repository"] --> A["AST chunking:<br/>function, method, class"]
A --> E["Prepend path, class, docstring,<br/>LLM one-line summary"]
E --> V["Vector index"]
R --> B["BM25 over identifiers<br/>and error strings"]
R --> G["Call graph from<br/>tree-sitter plus LSP"]
Q["Developer query"] --> D{"Intent"}
D -->|"how do we do X"| V
D -->|"exact symbol or error"| B
D -->|"who calls this"| G
What a good answer covers
- Enough that most prose RAG intuitions actively mislead
- Chunking must follow syntax, not tokens
- Splitting a file every 512 tokens cuts functions in half and produces chunks that are semantically…
- Parse to an AST and chunk on structural boundaries: function, method, class
- If a function exceeds the budget, split it but repeat the signature and enclosing class name in each piece
Key terms: Chunks need synthetic context
Interviewer follow-ups to expect
When does an embedding index beat an agent with grep over a repository?
How would you evaluate code retrieval, given that 'the relevant chunk' is often a set of files rather than one?
Worked answer from AI-Engineer-Interview-Questions by Om (MIT), reformatted for LLM Prep · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep