RAG from scratch: build a minimal pipeline in six steps
RAG gives a model the right passages at question time. Build a minimal version in six steps, see where it breaks and decide when to use a platform instead.
Read articleA RAG chatbot answers from your documents by retrieving the right passages first. See each stage of the pipeline, a small code sketch and the failures to test for.
By the Askocto team · · 4 min read
A RAG chatbot answers questions by first retrieving relevant passages from your own documents and then asking a language model to write an answer using only those passages. RAG stands for retrieval-augmented generation. It is the usual way to make a chatbot answer from your content instead of from general knowledge. This article walks through the pipeline stage by stage, shows a small code sketch and lists the failures to test for.
In short: Clean documents, split them sensibly, index them, retrieve the best passages, instruct the model to answer only from them and show the source. Most quality problems come from the first three stages.
The next sections cover each stage in the order where it most often goes wrong.
Everything downstream depends on clean text. Tables, scanned pages and navigation menus can turn into noise. Strip repeated headers and footers, keep headings, and preserve the title of each page, because the title helps retrieval later. The article on document parsing explains why formats behave differently.
A passage should hold one idea and enough context to make sense alone. Too small, and a passage loses its meaning. Too large, and it mixes topics, so retrieval returns text that is only partly relevant.
Practical rules:
An embedding model converts text to vectors so that similar meanings land near each other. You store the vector with the passage and its metadata in a vector index. For background, read vector embeddings explained. Use the same embedding model for passages and questions, and note which one you used, since changing it means re-embedding everything.
At question time, embed the question and fetch the nearest passages, perhaps the top ten to twenty. Then rerank: a second model scores each candidate against the question and you keep the best few. This step often fixes cases where the right passage was retrieved but ranked low. Reranking describes how it works.
The prompt tells the model what to do with the passages. A sketch:
You are a support assistant for a bicycle shop.
Answer only from the passages below.
If the passages do not contain the answer, say you do not know
and offer to connect the customer with the team.
Name the page each fact came from.
Passages:
{{passages}}
Question: {{question}}
The same flow in code, with placeholder functions standing in for whichever services you use:
async function answer(question) {
const qVector = await embed(question)
const candidates = await index.search(qVector, { limit: 15 })
const best = await rerank(question, candidates, { keep: 4 })
if (best.length === 0) return handover(question)
const reply = await generate({ question, passages: best })
return { text: reply.text, sources: best.map((p) => p.url) }
}
Note the early exit. When retrieval finds nothing useful, the bot should hand over instead of letting the model guess.
| Failure | What you see | Likely stage |
|---|---|---|
| Right fact, wrong answer | The passage was retrieved but the model ignored it | Generate: tighten the instruction |
| Missing answer | The document has it, but the bot says it does not know | Split or retrieve: passages too small or too large |
| Mixed answer | Two products blended together | Split: passages cover several topics |
| Stale answer | Old policy repeated | Ingest: outdated documents still indexed |
| Confident invention | A fact appears in no passage | Generate: loosen limits, no handover rule |
| Slow replies | Long wait before the first words | Retrieve or rerank: too many candidates |
Keep a test set of real questions with the expected source for each, and run it after every change. The article on RAG from scratch goes deeper into the mechanics.
RAG brings in current facts at question time, so updating an answer means updating a document. Fine-tuning changes how a model behaves, which suits style and format more than facts. Many support setups use RAG for knowledge and instructions for behavior. See RAG vs fine-tuning for the comparison.
Building the pipeline teaches you a great deal and gives you full control, but you also own ingestion, indexing, refresh schedules, access control, evaluation, a chat interface and a way for humans to take over. A hosted platform provides these. With Askocto, you add sources under Build > Sources, such as a website crawl, files or Q&A pairs, and retrieval, retraining and the chat run for you. The data sources documentation lists the source types. If you want the result without the plumbing, Askocto has a free plan to try it on.
Retrieval-augmented generation. The system retrieves passages from your content and the model writes its answer using them.
You need an index that can find passages by meaning. A dedicated vector database is one option, and some general databases offer vector search too.
Instruct it to answer only from the passages, hand over when retrieval finds nothing useful, show sources and test with trap questions.
Whenever the source content changes. Many teams refresh on a schedule and trigger a manual refresh after a policy change.
Askocto answers the repeat questions across chat, email and voice and hands the hard ones to your team.
RAG gives a model the right passages at question time. Build a minimal version in six steps, see where it breaks and decide when to use a platform instead.
Read articleRetrieval finds candidates and reranking puts them in the right order. See how the second pass works, when it is worth the extra step and how to measure the gain.
Read articleAn embedding turns a sentence into a list of numbers so search can match meaning, not just words. Learn how it works, what it misses and how to check it on your own content.
Read article