How to build reliable RAG with semantic chunking
Learn practical engineering steps to prevent AI hallucinations by splitting texts by context and removing vector noise.
Summary
- Semantic chunking preserves complete sentences by detecting actual context shifts in the source text.
- Cross-encoder models re-rank retrieved excerpts by examining their direct relationship with the user prompt.
- Cosine similarity filters help discard vague documents before feeding the language model.
- Robust ingestion pipelines require stress testing with complex and ambiguous queries.
- Optimized RAG systems reduce hallucinations and cut operational cloud processing costs.
What is a RAG system and why traditional chunking fails
When chatting with a corporate AI, we often need it to consult internal company documents. This mechanism is known as RAG (Retrieval-Augmented Generation), which retrieves relevant text excerpts before formulating an answer. In practice, this works like an assistant who goes to a file cabinet, reads the right pages, and then summarizes everything for you. However, the biggest Achilles' heel of this process happens during data ingestion, specifically when we slice long documents into smaller pieces called chunks.
The traditional method of text splitting is usually purely mechanical: cutting the file every five hundred characters, regardless of what is written. In practice, this means whole sentences and crucial explanations are cut in half, separating the subject from the predicate or isolating a conclusion from its main argument. When the system turns these truncated pieces into vector numbers to store in the database, the original meaning gets lost. As detailed when analyzing how to mitigate hallucinations in RAG with cross-validation, poorly structured input data causes cascading failures in generated answers.
How semantic chunking works in practice
To solve the blind cutting problem, we use semantic chunking, a technique that analyzes the actual shift in subject between paragraphs of a document. Instead of counting characters, the algorithm measures the mathematical distance of meaning between consecutive sentences using a smaller language model. When it notices the topic has shifted abruptly, it inserts a natural context break. In practice, this ensures that a text block contains only one complete idea, making search tasks much easier for the AI.
Implementing this logic in Python requires combining text manipulation libraries with compact embedding models. Below, we show a functional snippet that calculates similarity variation between sentences to decide exactly where to apply the semantic cut:
import numpy as np
def semantic_chunking_split(sentences, embeddings, threshold=0.75):
chunks = []
current_chunk = [sentences[0]]
for i in range(1, len(sentences)):
sim = np.dot(embeddings[i], embeddings[i-1]) / (
np.linalg.norm(embeddings[i]) * np.linalg.norm(embeddings[i-1])
)
if sim >= threshold:
current_chunk.append(sentences[i])
else:
chunks.append(' '.join(current_chunk))
current_chunk = [sentences[i]]
if current_chunk:
chunks.append(' '.join(current_chunk))
return chunksThis code loops through a list of sentences and their respective numerical vectors. When the mathematical similarity between the current and previous sentence drops below the set threshold, the script closes the current block and starts a new one. This surgical care in data preparation is the first step toward building a robust vector memory infrastructure for AI agents without context loss.
Reducing vector noise with relevance thresholds
Even with flawless semantic chunking, vector databases tend to return more results than necessary when a user asks a question. If we ask for a security procedure and the system pulls excerpts about vacation policy simply because they use similar words, we get vector noise. In practice, this informational garbage confuses the final model, causing it to ignore the correct answer or invent data to fill the gap.
To eliminate this noise, we apply a cosine similarity threshold filter right after the initial database search. We set a minimum relevance floor, discarding any excerpt that falls below this cutoff score, no matter how hard the database tries to display it. This step acts like a bouncer at an exclusive party: if the document does not show genuine compatibility with the user's doubt, it is summarily barred before reaching the generator model.
Refining precision with Cross-Encoder models
After filtering the best blocks by traditional vector search, a hidden challenge remains: relevance ranking. Fast search models calculate vectors in isolation, which does not always reflect the subtlety of the user's question. To fix this distortion, we insert a re-ranking step using a specialized model called a cross-encoder. In practice, the cross-encoder reads the question and each candidate document simultaneously, comparing word by word with total attention to context.
Although this cross-verification is computationally heavier and slower for large data masses, it is applied only to the top ten or twenty results brought by the fast vector search. The precision gain widely compensates for the processing cost, ensuring the most useful excerpt stays rigorously in the first position of the prompt. As seen when discussing advanced persistence strategies for autonomous agents, refining the retrieval workflow eliminates ambiguities and accelerates system decision-making.
Validating the ingestion pipeline in production
Building the perfect pipeline on paper does not guarantee success when real data starts flowing. Poorly formatted PDF reports, complex tables converted into raw text, and lost footnotes can sabotage all engineering efforts. Therefore, continuous ingestion validation must be treated as an integral part of the application code, with automated tests that simulate hard questions and check whether the correct chunk was retrieved.
In addition, monitoring memory usage and response latency helps spot bottlenecks before they affect end users. Small adjustments in maximum chunk size parameters and text overlap between neighboring blocks make a huge difference in system accuracy. With a clean architecture, RAG stops being a constant source of frustration and becomes a reliable business asset.
Final thoughts on retrieval engineering
Investing time and effort in proper data ingestion modeling separates fragile prototypes from high-performance enterprise applications. Semantic chunking, combined with rigorous noise filters and cross-encoder re-ranking, turns piles of disorganized documents into a surgical knowledge base. By understanding and controlling each step of this workflow in Python, the engineer gains autonomy to solve real hallucination problems and ensure total reliability in AI-generated answers.