AI Engineering
Build a Practical RAG Pipeline: Ingestion, Retrieval, and Evaluation
Updated December 1, 2026
Map the data lifecycle first
Retrieval-augmented generation (RAG) gives a model selected external context at request time. A document pipeline prepares the source collection before a question arrives; a query pipeline retrieves a subset and supplies it to the model. Keeping these two flows visible makes failures easier to locate.
Before choosing a vector database, identify document formats, update frequency, access rules, expected query types, and deletion requirements. A PDF knowledge base with tenant-specific policies has different needs from a public documentation search.
Step 1: ingest and normalize sources
Extract text from source files while preserving useful structure such as headings, tables, page references, and identifiers. Normalize encoding and remove boilerplate carefully; over-aggressive cleanup can erase important meaning. Keep a durable reference to each source so an answer can point back to it.
Record ownership, visibility, version, and update timestamps as metadata. When a source changes, update or delete the corresponding indexed passages. Build ingestion as an idempotent process so rerunning it does not create duplicates.
Step 2: choose chunk boundaries
Models have finite context budgets, and retrieving a whole large document can bury the relevant detail. Chunking divides content into passages that can be independently searched. Fixed token windows are simple, while splitting on sections or paragraphs can preserve meaning better for structured documents.
Overlap may help when a relevant sentence spans a boundary, but too much overlap creates near-duplicate results. Test chunk sizes on real questions and inspect whether retrieved text contains enough context to answer. Preserve the parent document and section metadata with each chunk.
Step 3: index and retrieve
Create embeddings for passages when semantic search is appropriate, then store vectors with the text or a source reference. At query time, apply the same compatible embedding model to the user’s question. Combine the vector ranking with filters for permissions and, where useful, exact-term search.
Retrieval should return a small, useful set rather than every approximate match. A reranker can improve ordering when initial retrieval produces candidates, but it cannot recover evidence that was never retrieved. If no good evidence exists, design the application to say so instead of forcing an answer.
Step 4: construct a grounded generation request
Give the model the question, selected passages, and a clear instruction about how to use the evidence. Keep untrusted source text separated from system policy. Ask for citations or source identifiers in a format the application can validate against the retrieved set.
Do not treat a citation written by a model as proof. Verify that every cited identifier came from the retrieval result and that the cited text supports the claim. Limit context length and remove irrelevant passages to reduce distraction and cost.
const passages = await retrieve({
query,
userId: user.id,
limit: 6,
});
const answer = await model.generate({
question: query,
evidence: passages.map(({ id, text }) => ({ id, text })),
instruction: 'Answer from the supplied evidence; say when it is insufficient.',
});
return validateCitations(answer, passages);Step 5: evaluate both stages
Build a test set with questions, expected sources, and acceptable answer criteria. For retrieval, check whether relevant evidence appears near the top. For generation, check correctness, completeness, citation validity, and whether the model admits when the evidence is insufficient.
Track quality by topic and user group, not just an overall average. Add cases for outdated content, conflicting documents, no-answer questions, access restrictions, and unusual phrasing. Review failures after source updates and embedding or prompt changes.
Common operational pitfalls
A RAG prototype can fail because the parser lost table data, chunking separated a definition from its subject, metadata filters are inconsistent, or documents were never refreshed. These are data-pipeline issues, not necessarily model weaknesses. Keep ingestion logs and a way to inspect each retrieved passage.
Protect the full lifecycle: source permissions must survive indexing, retrieved data should be minimized, and deletions should propagate. Treat retrieved content as potentially hostile instructions. RAG can make knowledge accessible; it does not make the source accurate or safe.
Frequently asked questions
Does RAG prevent hallucinations?
No. It can provide relevant evidence, but retrieval can miss or return poor content and a model can still misinterpret it. Evaluate and validate the full pipeline.
Do I always need a vector database?
No. Small collections or exact queries may work with simpler search. Choose the index based on measured query needs and operational constraints.
Related reading
References
About Ali Raza
Ali is a software engineering student at Riphah International University and the founder of the digital agency Ionix Growth. He writes about modern web architecture, developer tooling, and integrating AI workflows into web applications.