I Built a RAG System Without LangChain. Overlap Chunking Made It Worse, Not Better.
Every RAG tutorial hands you the same four lines: chunk your documents, embed them, store the vectors, retrieve the closest ones. Nobody shows you what actually goes wrong, because the demo corpus is always three paragraphs of Wikipedia and the questions are always softballs. I wanted to see the real failure modes, so I built retrieval from scratch, no LangChain, no embeddings API, just chunking and classical information retrieval, and ran it against a small but genuinely tricky documentation set. What broke wasn’t what I expected going in, and one popular fix, overlapping chunks, made the specific failure I found measurably worse.
The setup
Ten fictional API documentation pages, the kind any developer platform ships: authentication, rate limiting, retry policy, webhooks, idempotency keys, pagination, error codes, sandbox versus production, deprecation policy, data export. I deliberately built in real overlap between some of them, rate limiting and retry policy both discuss HTTP 429, authentication and the sandbox page both discuss API keys, because that’s exactly the kind of collision that makes real documentation hard to retrieve from, and a clean, non-overlapping toy corpus would never surface it.
Twenty test questions, each with a known correct source document, gave me something to actually measure against, rather than eyeballing whether results looked plausible.
Chunking and BM25, with no library standing between me and the mechanism
Fixed-size chunking, forty words per chunk, is the simplest thing that could work:
def chunk_fixed(text, doc_id, size=40, overlap=0):
words = text.split()
chunks = []
step = size - overlap
for start in range(0, len(words), step):
chunk_words = words[start:start + size]
if chunk_words:
chunks.append({"doc_id": doc_id, "text": " ".join(chunk_words), "start": start})
return chunks
def chunk_fixed(text, doc_id, size=40, overlap=0):
words = text.split()
chunks = []
step = size - overlap
for start in range(0, len(words), step):
chunk_words = words[start:start + size]
if chunk_words:
chunks.append({"doc_id": doc_id, "text": " ".join(chunk_words), "start": start})
return chunks
For ranking, I skipped an embeddings API entirely and implemented BM25 by hand, the same scoring function search engines used for decades before embeddings existed:
class BM25:
def __init__(self, chunks, k1=1.5, b=0.75):
self.doc_tokens = [tokenize(c["text"]) for c in chunks]
self.doc_lengths = [len(t) for t in self.doc_tokens]
self.avg_doc_length = sum(self.doc_lengths) / len(self.doc_lengths)
self.doc_freqs = [Counter(t) for t in self.doc_tokens]
df = Counter()
for toks in self.doc_tokens:
for term in set(toks):
df[term] += 1
n = len(self.doc_tokens)
self.idf = {t: math.log((n - f + 0.5) / (f + 0.5) + 1) for t, f in df.items()}
def score(self, query, index):
score, freqs, doc_len = 0.0, self.doc_freqs[index], self.doc_lengths[index]
for term in tokenize(query):
if term not in self.idf:
continue
f = freqs.get(term, 0)
num = self.idf[term] * f * (self.k1 + 1)
denom = f + self.k1 * (1 - self.b + self.b * doc_len / self.avg_doc_length)
score += num / denom if denom else 0
return score
class BM25:
def __init__(self, chunks, k1=1.5, b=0.75):
self.doc_tokens = [tokenize(c["text"]) for c in chunks]
self.doc_lengths = [len(t) for t in self.doc_tokens]
self.avg_doc_length = sum(self.doc_lengths) / len(self.doc_lengths)
self.doc_freqs = [Counter(t) for t in self.doc_tokens]
df = Counter()
for toks in self.doc_tokens:
for term in set(toks):
df[term] += 1
n = len(self.doc_tokens)
self.idf = {t: math.log((n - f + 0.5) / (f + 0.5) + 1) for t, f in df.items()}
def score(self, query, index):
score, freqs, doc_len = 0.0, self.doc_freqs[index], self.doc_lengths[index]
for term in tokenize(query):
if term not in self.idf:
continue
f = freqs.get(term, 0)
num = self.idf[term] * f * (self.k1 + 1)
denom = f + self.k1 * (1 - self.b + self.b * doc_len / self.avg_doc_length)
score += num / denom if denom else 0
return score
Nothing here is a black box. Every term’s contribution to a chunk’s score is a number you can trace by hand, which turned out to matter a great deal once things started going wrong.
The headline numbers
BM25 beating plain TF-IDF cosine similarity by ten points on identical chunks is a real, reproducible result, and it matches BM25’s actual design advantage: it saturates term frequency (a word appearing eight times doesn’t score eight times higher than appearing once) and normalizes for document length in a way raw TF-IDF doesn’t. That part matched my expectations going in.
What didn’t match my expectations is everything the overlap configuration did on the right side of that chart.
The failure I expected never showed up
My working theory going in was that fixed-size, non-overlapping chunks would break retrieval whenever the answer to a question sat across a chunk boundary. I built a query specifically to test this: “What’s added to the backoff delay to stop clients from retrying in lockstep after an outage?” The word “jitter” and its explanation sit right at the boundary between chunk two and chunk three of the retry policy document.
It scored rank 1, top-1, on every single configuration. So did every other boundary-sensitive query I constructed. A forty-word chunk retains enough surrounding vocabulary on either side of a split to still match a query correctly, at least at this corpus size. That’s a real, useful negative result: the failure mode everyone warns you about in tutorials wasn’t the one that actually hurt this pipeline.
The failure that did show up: keyword frequency doesn’t know what a document is about
“How can I tell a sandbox key from a production key just by looking at it?” The correct answer lives in exactly one sentence of the sandbox documentation page: sandbox keys are prefixed sk_test_, production keys sk_live_. That sentence exists once.
The word “key,” meanwhile, appears constantly throughout the authentication page, in “API key,” “keys are generated,” “each key is scoped,” “old key remains valid,” seven or eight times in a single short document. BM25 and TF-IDF both do exactly what they’re built to do: count term overlap between the query and each chunk. The query contains “key” twice. The authentication document is saturated with “key.” The correct answer, buried in a document that says “key” exactly once, lost.
Rank for the correct document: third, on both TF-IDF and plain BM25, still technically inside the top three, but no longer the first result a user would see.
Then overlap made it worse, not better
Overlapping chunks (forty words, fifteen-word overlap) is the standard advice for fixing chunking problems. I applied it, expecting improvement or at worst no change.
The sandbox query didn’t just stay wrong. It dropped out of the top three entirely, retrieving three authentication chunks and nothing from the sandbox document at all.
The mechanism, once I traced through the actual chunk counts: overlap doesn’t add information, it duplicates it. A document that already dominates a query through sheer repetition of a term gets more chunks under an overlapping scheme, since a longer document produces proportionally more sliding windows than a short one. Every one of those extra chunks still says “key” repeatedly. The authentication document’s presence in the corpus grew. The sandbox document’s one relevant sentence didn’t get any more copies of itself, because it was never repeated to begin with. Overlap chunking, applied to a keyword-frequency ranking method, amplifies whichever document was already keyword-dense. It does nothing to help a document whose answer is short, specific, and stated exactly once.
A second real example of the same underlying issue: “What happens if I automatically retry a 400 error?” The correct answer is in the retry policy document, phrased as “a 4xx error other than 429.” The query says “400.” The error codes reference document literally contains the digits “400” as a status code listing. BM25 ranked the errors document above the actual answer, because “400” as a literal token match beat “4xx” as a paraphrase, even though the errors page never actually answers the question asked.
What this actually tells you about when you need embeddings
None of this is an argument that classical retrieval is broken or useless. Top-3 accuracy stayed at 95% or better across every configuration, and for many real applications, showing three candidate chunks and letting a downstream step (or a human) pick the right one is a completely workable design. The honest finding is narrower and more useful than “keyword search is bad”: BM25 and TF-IDF have no mechanism for recognizing that “key prefix” and “sandbox versus production” are related concepts unless the literal words overlap, and no mechanism for recognizing that a short, precise answer deserves to outrank a long document that happens to repeat the query’s exact words more often. Semantic embeddings exist specifically to close that gap, by scoring meaning rather than token overlap, and this experiment is exactly the kind of failure case that motivates reaching for them. I didn’t test embeddings here, deliberately: isolating the classical layer first is what made these specific failure mechanisms visible and traceable, rather than hidden inside a vector similarity score. That comparison, run on this same corpus and these same twenty questions, is the natural next experiment.
The corpus, the queries, and the full BM25 and chunking implementation used here are available(use this) for anyone who wants to reproduce or extend this, including running it against real embeddings on the same twenty questions to see exactly how much of this gap closes.


