Search⌘ K
AI Features

Agentic Search with Hybrid Retrieval and AST Code Chunking

Explore how to build an agentic search pipeline that combines BM25 keyword ranking, embedding-based retrieval, reciprocal rank fusion, SQL filters, and AST-based code chunks. Learn to return relevant notes, exact changes, and complete functions in one search call.

We'll cover the following...

In the previous lesson, the incident knowledge base was 57 times larger than the context window, so retrieval was the only option. Since Chapter 1, our assistant has retrieved notes with search_documents(), which counts how many words a note shares with the question. That was enough for eight short notes. A real corpus is messier. It has runbooks, postmortems, a change log with exact times, and the code of the services themselves.

In this lesson, we’ll build the search that search_documents() was standing in for. This is the Select move from Chapter 1 in practice: bringing only the notes, code, and changes a question needs into the context. We’ll rank notes by keywords with BM25, rank them by meaning with embeddings, and combine the two rankings into one list. Then we’ll answer exact questions about times with SQL and split code files along their functions. At the end, all four parts become one hybrid_search() call.

Why isn’t word overlap enough?

Let’s take a question an engineer might ask mid-incident: “Is it safe to clear the cache right now?” The best answer is in note-8, the cache guideline, which says a Redis restart empties the cache and can double database load. note-1, the current runbook, also says not to restart Redis during business hours.

Word overlap has two problems with this question:

  • Every word counts the same: A match on “the” counts as much as a match on “cache,” even though “the” appears in every note.

  • Only exact words match: note-8 talks about a “restart” that “empties” the cache. The question says “clear”. Word overlap sees no connection.

We’ll fix the first problem with a better keyword ranker and the second with search by meaning.

Rank with BM25

BM25 is a standard formula for ranking documents by keywords. It gives more weight to rare words than to common ones, and it keeps long documents from winning just because they contain more words. The rank_bm25 package implements it as BM25Okapi.

We’ll also drop stop words, common words like “the,” “is,” and “to” that carry little meaning. Let’s rank the notes both ways and compare.

bm25_rank.py · Python 3.14.0
import re
from rank_bm25 import BM25Okapi
from note_store import NOTE_STORE
QUESTION = "Is it safe to clear the cache right now?"
STOP_WORDS = {"a", "an", "the", "to", "is", "it", "of", "in", "on", "and", "or", "we", "if",
"do", "be", "for", "at", "by", "that", "this", "can", "should", "what", "how", "any"}
def words(text, drop_stop_words=True):
found = re.findall(r"[a-z0-9_]+", text.lower())
return [w for w in found if not (drop_stop_words and w in STOP_WORDS)]
def bm25_rank(question, docs, drop_stop_words=True):
index = BM25Okapi([words(doc["text"], drop_stop_words) for doc in docs])
scores = index.get_scores(words(question, drop_stop_words))
ranked = sorted(zip(scores, docs), key=lambda pair: pair[0], reverse=True)
return [(doc["doc_id"], round(float(score), 2)) for score, doc in ranked if score > 0]
if __name__ == "__main__":
print("all words: ", bm25_rank(QUESTION, NOTE_STORE, drop_stop_words=False)[:4])
print("stop words dropped:", bm25_rank(QUESTION, NOTE_STORE)[:4])

In the code above:

  • Lines 5–7: We set ...