Back to articles
10 min read

The Evolution of RAG and Retrieval: From Chunking to Agentic Search

#RAG#Retrieval#AI#Embeddings#LLM

Following the complete journey of a retrieval pipeline, from end to end.

0. The Problem at Heart

The fundamental challenge with retrieval-augmented generation (RAG) is simple: models of embeddings work better with short fragments, but a loose fragment loses its context.

Consider this: the phrase "Revenue grew 3%" tells you nothing about which company or which quarter it refers to. To solve this properly, we need to answer two separate questions:

  1. Where to cut the document? (Chunking)
  2. How to give each piece back the context it lost when cut? (Contextualization)

This is the core tension that drives nearly every evolution in retrieval techniques.

The core problem: short chunks lose context, long context is expensive - balancing precision and recall

1. Where to Cut: Chunking Strategies

Chunking strategies range from crude to sophisticated, each with different trade-offs. Let's go from coarse to fine:

StrategyDescriptionBest For
Fixed-size with overlapCut by N characters/tokens with overlap between chunks. Crude but simple.Quick prototypes, uniform text
Recursive chunkingTry to cut by paragraph first, then sentence, then word. Respects natural boundaries when possible.General-purpose documents
Sentence-based (with a model)Use a sentencizer like spaCy that handles abbreviations, decimals, and URLs correctly.Legal, technical, or messy text
Semantic chunkingCut where the topic changes by measuring embedding distance between consecutive sentences.Thematic documents where topics shift naturally
Structural chunkingRespects headings, lists, and tables. For tables, embed each row with its column headers and containing section titles.Documentation, reports with tables and clear structure
AST-based chunkingFor code, cut by functions and classes using tree-sitter.Source code repositories (as used by Cursor)

Chunking strategies progression from fixed to semantic to structural

2. How to Return Context to Chunks

This is the family that has seen the most innovation recently. All of these solve the same problem, just at different points in the pipeline and with different costs.

The Main Approaches

TechniqueWhenNeeds LLMCost at ScaleContext ScopeNotes
Contextual Retrieval (Anthropic, 2024)At index timeYes, per chunkHigh (mitigated by prompt caching)Entire documentPrepend LLM-generated context to each chunk for both embedding and BM25.
Late Chunking (Jina, 2024)At index timeNoLowModel window (~8K)Embed the whole document first with long-context model, then average token embeddings per chunk.
Statement Chaining (Wilson Lin)At index timeNo (small classifier)Very lowStructure + local chainUses a DistilBERT classifier to follow dependency chains between sentences, includes only necessary context.
Parent-Document / Small-to-BigAt query timeNoLowParent blockSearch on small precise chunks, return larger parent blocks to the model.
Sentence WindowAt query timeNoLowN neighboring sentencesRetrieve a sentence, expand with surrounding context on return.
PropositionsAt index timeYesHighLLM-resolved referencesRewrite to atomic, self-contained assertions with pronouns resolved.
Doc2Query / Hypothetical QuestionsAt index timeYesMedium-HighGenerated questionsGenerate questions each chunk answers, index those questions instead.

Contextual Retrieval Results

Anthropic's benchmarks showed dramatic improvements when combining techniques:

ConfigurationFailure Rate (chunk not in top-20)Reduction
Base5.7%—
Contextual embeddings only3.7%35%
+ Contextual BM252.9%49%
+ Reranking1.9%67%

Two key insights from Anthropic:

  • A generic document summary gave minimal improvement. Specific, chunk-tailored context is what works.
  • Much of the total gain comes from BM25 and reranking—standard components, not just contextualization.

Comparison of context enrichment techniques over time

3. How to Represent Text

Once chunks are prepared, we need to choose how to encode them for search.

Representation Types

TypeDescriptionStrengthsWeaknesses
Dense (Bi-encoder)Single vector per chunk. The standard approach.Great semantic understanding, fast ANN searchStruggles with exact matches (names, codes, SKUs)
Sparse (BM25/SPLADE)Term-frequency based or learned sparse vectors.Excellent for exact matches, interpretableLess semantic understanding
Late Interaction (ColBERT)Vector per token, compare each query token to each document token.Very precise, more nuanced matchingMuch more storage space (often 10x+). Different from late chunking!

Compression Techniques

To make vector search scalable, several compression methods are used:

  • Matryoshka Representations: Truncate vectors at different dimensions while maintaining performance
  • float16: Half-precision, 2x memory savings with minimal loss
  • Binary Quantization: 1 bit per dimension, extremely compact, surprisingly effective
  • Product Quantization (PQ): Used by CoreNN to fit billions of vectors in RAM

Many modern models like BGE-M3 generate both dense and sparse representations simultaneously—a best-of-both-worlds approach.

Dense vs Sparse vs Late Interaction representation comparison

4. How to Search

With representations ready, we need efficient search strategies.

Hybrid Search

The modern default is hybrid search: run dense and sparse searches in parallel, then fuse results with Reciprocal Rank Fusion (RRF). This combines semantic understanding with exact-match capabilities.

Approximate Nearest Neighbor (ANN)

For scale, we use ANN indexes:

  • HNSW (Hierarchical Navigable Small World): Great for in-memory search, fast and accurate
  • DiskANN/Vamana: Optimized for disk-based search, enables much larger corpora

Filtering Strategy

A practical finding: filter after searching is often faster than filtering before. Pre-filtering can constrain the search space in ways that hurt ANN performance, while post-filtering is often more efficient in practice.

Hybrid search pipeline with RRF fusion

5. On the Query Side

The query itself can be optimized before search.

TechniqueDescriptionEffectiveness
Query rewritingClean up the query, expand acronyms, fix grammarUsually helpful, low cost
HyDE (Hypothetical Document Embeddings)Have an LLM write a hypothetical answer, embed and search with thatMixed results; Anthropic found limited gains in their tests
Multi-queryGenerate multiple reformulations of the question and search all of themGood for ambiguous or broad queries
Query decompositionBreak complex questions into simpler sub-questions, search each, then synthesizeGreat for multi-part questions

6. Reranking

After initial retrieval, reranking reorders results for maximum relevance. This is where the biggest precision gains often come from.

ApproachDescriptionTrade-offs
Cross-encoderReads query and document together, scores relevance directly.Much more accurate but expensive (can't precompute like bi-encoders)
LLM as rerankerUse an LLM to reorder and filter results.Very flexible, can do complex reasoning. Wilson serves this via Groq for sub-second latency.
Non-semantic signalsCombine with votes, recency, authority, etc.In Hackerverse, they combined similarity with post score (social proof) and age penalties.

Going back to Anthropic's numbers: reranking took them from 49% to 67% failure reduction—nearly as much as all the contextualization combined!

Reranking with cross-encoder vs LLM reranker

7. Global Corpus Structure

Sometimes, top-k retrieval isn't enough. For questions that require understanding the entire corpus, we need global structures.

RAPTOR (Recursive Abstractive Processing for Tree-Organized Retrieval)

RAPTOR builds a tree by clustering chunks, summarizing each cluster, then clustering those summaries up the tree. This allows retrieval at different levels of granularity—from specific details to high-level themes.

Use case: Questions like "What are all the key arguments made across this 200-page document?"

GraphRAG

GraphRAG takes a different approach:

  1. Extract entities and relationships using an LLM
  2. Build a knowledge graph
  3. Detect communities within the graph
  4. Generate summaries for each community
  5. Use these community summaries to answer global questions

Use case: Questions like "What are the main themes of this entire corpus?" that no amount of top-k chunks can answer.

RAPTOR tree vs GraphRAG knowledge graph comparison

8. Agentic Search

The frontier is moving beyond single-shot retrieval to iterative, agentic search. Instead of one search query, the model becomes an active researcher.

The agent follows a loop:

  1. Search - Formulate queries based on what it knows
  2. Read - Examine returned results
  3. Decide - Determine what else it needs to know
  4. Repeat - Search again until it has sufficient context

Key optimization: Launch multiple searches in parallel rather than sequentially. As Wilson Lin predicts, agentic search is likely to replace simple retrieval for complex tasks.

Agentic search loop with parallel exploration

9. Code Search in Practice

Different tools take very different approaches to code retrieval. Let's look at how the leading tools solve this.

Cursor: Semantic Index + grep

Cursor builds a sophisticated indexed approach:

  • Change detection: Uses a Merkle tree where each file and directory hash is computed from its children. Only changed branches are re-indexed.
  • Syntactic chunking: Uses tree-sitter to cut by functions and classes. Embeddings are cached by chunk content.
  • Cloud storage: Chunks stored in Turbopuffer, namespaced per codebase.
  • Team reuse: New team members compute a simhash of their Merkle tree to find similar indexes and reuse them. Results are verified against the local Merkle tree to prevent cross-contamination.
  • Privacy-first: Retrieved chunks are decrypted client-side only.
  • Learned embeddings: Trained on actual agent traces—what files did agents open/search before finding the right code? This optimizes for "what the agent actually needs" not "code that's similar".
  • Hybrid with regex: For monorepos where ripgrep gets slow, they built a local regex index.

Results: Semantic + grep gives 12.5% better precision than grep alone (6.5%-23.5% depending on model). For repos >1000 files, the improvement in code users keep rises to 2.6%.

Claude Code: No Index, Just Agentic Search

According to Boris Cherny, Claude Code tried a local vector DB early on but switched to iterative agentic search with glob and grep, and it performed better.

  • Philosophy: "Glob and grep" wins over RAG
  • LSP integration (since Dec 2025): Go to definition, find references, call hierarchy across 11 languages
  • Advantage: Always sees live files—no stale index issues after commits
  • Trade-off: In multi-million-line monorepos, starting fresh each query can get expensive

Aider: Repo Map with PageRank

Aider takes yet another approach—no embeddings, no grep-centric search:

  • Parse everything: Uses tree-sitter to parse the entire repo
  • Build a call graph: Maps which definitions reference which
  • Rank with PageRank: Custom PageRank biased toward identifiers mentioned in the current chat
  • Budget-aware trimming: Cuts the result to fit the token budget
  • Output: A structural "map" of the repo rather than raw code chunks

Convergence

The industry is converging on a layered approach:

  1. grep/ripgrep - Broad, cheap search
  2. LSP - Precise symbol-level navigation and understanding
  3. Semantic search - When you don't know the exact name of what you're looking for

Comparison of Cursor, Claude Code, and Aider approaches

10. How to Choose

With so many options, here's a practical decision matrix based on your constraints:

SituationRecommended Approach
Prose documents, moderate budgetStructural chunking + hybrid search (dense+sparse) with RRF + cross-encoder reranking
Context-critical, small/medium volumeContextual Retrieval with prompt caching
Massive volume, cost-sensitiveLate chunking or statement chaining
Global questions across entire corpusRAPTOR or GraphRAG
Code, small/medium repoAgentic search with grep + LSP
Code, large monorepoAgentic search + semantic index (Cursor-style)
Many proper names, IDs, or codesEnsure strong sparse/BM25 component is present

The key insight is that most techniques attack the same problem from different points in the pipeline. You're not choosing one over all others—you're composing the right pieces for your specific constraints of accuracy, latency, cost, and scale.

Decision tree for choosing retrieval strategy