AI explained

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.

By the Askocto team · · 5 min read

Retrieval augmented generation, or RAG, is a pattern where your system finds the passages most relevant to a question and gives them to a language model along with the question, so the answer comes from your content and not from the model's memory. A minimal version is a small amount of code. A good one takes care in chunking, retrieval and testing. This article builds the minimal version in six steps and shows where it usually breaks.

In short: Split documents into chunks, turn each into an embedding, store them, retrieve the closest chunks for each question, put them in the prompt and test with real questions. Most failures trace back to chunking and retrieval, not the model.

The idea in one picture

Think of two phases. Before any question, you prepare your content: split it, index it and store it. At question time, you search the index, build a prompt from the best matches and ask the model to answer only from them.

PREPARE (once, and again when content changes)
  documents -> clean text -> chunks -> embeddings -> index

ANSWER (every question)
  question -> embedding -> search index -> top chunks -> prompt -> model -> reply

The six steps

Step 1: collect and clean your content

Gather the pages, PDFs and notes that hold the answers. Convert them to plain text and remove navigation, cookie banners and repeated footers. Keep titles, headings and the source link, because you will want them later. Messy input is the most common cause of poor retrieval, so structuring a knowledge base for AI is worth reading before you start. PDFs with tables or scans need extra care before they become clean text.

Step 2: split into chunks

A model reads a limited amount at a time, and a search works better on focused pieces than on whole documents. Split text into chunks that each hold one idea.

Practical guidelines:

  • Split on headings and paragraphs, not at a fixed character count in the middle of a sentence.
  • Keep each chunk short enough to be about one topic, and long enough to make sense alone.
  • Let neighboring chunks overlap a little so a sentence at a boundary is not lost.
  • Attach metadata to each chunk: document title, section heading, URL and date.

The right size depends on your content, so try a few and compare results on your test questions.

Step 3: create embeddings

An embedding is a list of numbers that represents the meaning of a piece of text, so that texts with similar meaning end up close together. You send each chunk to an embedding model and store the result. Check the current documentation of the embedding provider for the available models and their terms. Vector embeddings explains the idea in more depth.

Store each chunk with its embedding and metadata in a vector index or a database that supports similarity search. At question time:

  1. Embed the question with the same model.
  2. Find the chunks whose embeddings are closest.
  3. Return the top few, with their scores.

Many systems add a keyword search alongside, since exact terms such as product codes match better by words than by meaning. Combining the two and then re-ordering the results, as described in reranking, often improves quality.

Step 5: build the prompt

Put the retrieved chunks and the question into one prompt with clear instructions:

You answer customer questions for a small shop.
Use only the passages below. If they do not contain the answer,
say you are not sure and offer to pass the question to the team.
Quote policy numbers and time limits exactly as written.

Passages:
[1] Returns (updated page): Items can be returned within 30 days...
[2] Shipping: Orders leave the warehouse within two working days...

Question: Can I return a gift after six weeks?

Include the source of each passage so the reply can point to it. The "if the passages do not contain the answer" line matters as much as the passages themselves, because it gives the model an honest way out.

Step 6: test and measure

Build a test set of thirty to fifty real questions, with the passage that should answer each one and the expected answer. Measure two things separately:

  • Retrieval: did the right passage appear in the top results?
  • Answer: given the passages, was the reply correct and properly declined when needed?

Separating them tells you where to work. If the right passage is missing, fix chunking or search. If it is present but the answer is wrong, fix the prompt or the source wording.

Where it usually breaks

Symptom Likely cause Fix
Answer is right but outdated Old chunk still indexed Re-index on content change, remove old versions
Right page, wrong section retrieved Chunks too large or too small Re-split on headings, adjust size
Product codes not found Pure meaning search misses exact terms Add keyword search
Two sources contradict Duplicate or stale content Resolve at the source
Model ignores the passages Prompt too long or unclear Shorten the instructions, put the passages near the question
Confident answer, no source No relevant chunk, no honest way out Add the "say you are not sure" rule and a score threshold

Maintaining it

A minimal pipeline needs ongoing care: re-indexing when content changes, monitoring questions that found nothing, versioning your prompt and rerunning the test set after every change. For a customer-facing system you also need logs, handoff to people and access control, which turns a weekend project into a continuing one. The build RAG chatbot guide covers the build in detail, including what to maintain yourself.

In Askocto, the same flow is built in. You add a website, files or text under Build > Sources, each source moves from Queued to Training to Ready, and Test > Playground shows which sources supported an answer. You can schedule Auto-retrain to keep content fresh. The data sources docs cover the options, and starting a free workspace lets you compare your pipeline with a ready one on your own content. Askocto also records each conversation under Activity, so you can read what was retrieved and said.

Frequently asked questions

What is RAG in simple terms?

RAG means looking up relevant passages from your own content first and then asking a language model to answer using them. It keeps answers tied to your sources.

Do I need a vector database for RAG?

You need some way to search by meaning, which can be a dedicated vector database or a database with vector search. For a small content set, a simple local index can be enough to start.

Is RAG better than fine-tuning for support?

For facts that change, usually yes, because you update the source and the next answer changes. See RAG vs fine-tuning for the comparison.

How big should chunks be?

There is no universal size. Start with paragraph-sized chunks split on headings, test on your own questions and adjust.

Train an agent on your own help center

Askocto answers the repeat questions across chat, email and voice and hands the hard ones to your team.

Keep reading

All articles
AI explainedreranking
Sep 9, 20245 min read

Reranking in RAG: what it does and when to add it

Retrieval 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 article
AI explainedvector embedding
Sep 1, 20245 min read

Vector embedding explained: how text becomes searchable

An 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