RAG PIPELINE

Production RAG System API

Problem

Retrieval-Augmented Generation (RAG) systems often hallucinate or fail on long documents. While commonly attributed to the LLM, in practice, many failures occur earlier in the pipeline when relevant context is fragmented during chunking and never retrieved. Given the same embeddings and LLM, how much does chunking strategy alone affect RAG performance?

Architecture Diagram

RAG Architecture Diagram

Workflow

The system ingests PDF/DOCX/TXT files and passes them through a Document Processor. It generates four independent vector indexes in ChromaDB, each using a distinct chunking strategy (Fixed, Sentence, Paragraph, and Recursive). When a query is made, the Retriever fetches chunks from all collections side-by-side. The responses and retrieval accuracy are then evaluated to measure the direct impact of the chunking algorithm.

Tech Stack

Python FastAPI ChromaDB Sentence Transformers Groq API NumPy

Challenges

Establishing a fair benchmarking environment was challenging. I had to ensure that the evaluation questions accurately reflected the semantic content of the documents without favoring a specific chunk length. Processing over 1,000+ chunks synchronously caused bottlenecks, which required optimizing the embedding pipeline and adopting asynchronous FastAPI routes.

Trade-offs

Recursive chunking proved to be the most accurate by preserving semantic structure, but it introduced a more complex implementation and slightly higher preprocessing overhead compared to simple Fixed-Size Chunking (which is fast but breaks semantic boundaries). The decision to use independent ChromaDB collections increased storage requirements but was necessary for strict side-by-side strategy comparison.

Benchmarks

Evaluation Document

Infosys Integrated Annual Report 2024–25 — a 400+ page, long-form enterprise document chosen specifically to stress-test chunking strategies beyond toy-sized text.

58% → 68%

Retrieval Coverage Improvement

Recursive chunking vs fixed chunking

< 1.0s

P95 End-to-End Latency

Across 100+ evaluation questions

Chunking Strategy Answer Accuracy Retrieval Coverage
Fixed 18% 58%
Sentence 12% 60%
Paragraph 16% 66%
Recursive 18% 68%

Key Finding

Recursive chunking matched Fixed chunking on raw answer accuracy (18%) but retrieved meaningfully more of the relevant source content (68% vs 58% coverage). On a 400+ page enterprise document, that gap matters: even when the final answer scores the same, higher coverage means fewer relevant passages are silently dropped before they ever reach the LLM — a distinction that only shows up when you evaluate retrieval and generation separately instead of just eyeballing final answers.

GitHub Repository

The benchmarking framework, FastAPI implementation, and chunking strategy code are available on GitHub.

sowndappan5/RAG-System