Similarity search finds information related to a query even when the words differ. In an agent, it is a way to retrieve selected parts of an external knowledge base instead of placing every document in the LLM’s context. It is especially useful when a file is too large to read whole or there are too many documents to inspect one by one. section 5.1.3 section 5.2.2
The practical form here is vector search. An embedding model converts text into numerical vectors that place similar meanings near one another. Convert documents before searching, convert the query with the same model, then compare its vector with the stored vectors. Cosine similarity gives higher scores to more similar vectors; sorting those scores yields the closest matches. This differs from keyword search, which depends on matching words. section 5.2.2 section 5.3.3
Build a minimal search pipeline in order:
- Split long source text into chunks so a search can return relevant portions rather than entire files. section 5.3 section 5.3.5
- Generate embeddings for the chunks and retain each chunk beside its vector; the Python implementation obtains vectors through get_embeddings. section 5.3.1 section 5.3.3
- Embed the query, calculate cosine similarity against all chunk vectors, sort scores in descending order, and return the top_k chunks with their scores. section 5.3.3
Listing 5.5 Implementing vector search to find chunks
def vector_search(query, chunks, chunk_embeddings, top_k=3):
"""Find the most similar chunks to the query."""
query_embedding = get_embeddings(query)
similarities = cosine_similarity(query_embedding, chunk_embeddings)[0]
top_indices = similarities.argsort()[::-1][:top_k]
results = []
for idx in top_indices:
results.append({
'chunk': chunks[idx],
'similarity': similarities[idx]
})
return results
The vector_search function exposes the complete ranking step: one query embedding, a comparison with precomputed chunk embeddings, descending score order, and scored results. section 5.3.3
Finding relevant passages in web results
A web search for 2025 Nobel Prize winners produces pages spanning several fields. To find content about quantum computing, collect full page content, split it into 500-character chunks with 50-character overlap, embed the chunks, and request the three closest matches to “quantum computing.” In the exercise, selecting those chunks reduced 37,312 tokens of collected content to 304 tokens. The retrieved passages can then supply context for an answer without sending all the pages. section 5.3.5
Similarity search is one retrieval choice, not a replacement for every search method. Use keyword search when exact matching matters, graph search for relationships, and structure-based search when file organization is useful. Retrieved results enter the LLM’s context as references for generation. The same retrieval pattern can bring back past problem-solving records: an agent may call a memory tool when needed, or inject relevant memories before each LLM request. section 5.6 section 6.4.5 section 5.2





















