Skip to main content
Research / Journal / Archive
PROTOCOL.READ / 10 min read

How to Implement RAG: A Step-by-Step Guide from Prototype to Production

A practical guide to implementing retrieval-augmented generation: data preparation, chunking, embeddings, retrieval, evaluation, and the operational work that separates a demo from a production system.

What RAG Is and When to Use It

Retrieval-augmented generation (RAG) gives a language model relevant source material at question time instead of relying only on what it memorized in training. You retrieve passages from your own documents and place them in the prompt, so answers are grounded, current, and traceable to a source. Choose RAG when answers must come from private or changing knowledge, such as policies, contracts, product documentation, and support history.

Step 1: Define the Questions and the Success Criteria

Before writing code, collect 50 to 100 real questions your users would ask, and note the correct answer and source for each. This set becomes your evaluation benchmark. Without it you cannot tell whether a change helped.

Step 2: Prepare and Clean the Data

Garbage in, garbage out applies strongly here.

  • Convert documents to clean text, preserving headings, lists, and tables.
  • Remove boilerplate such as repeated headers, footers, and navigation.
  • Keep metadata: title, source, date, version, owner, and access level.
  • Decide how you will handle updates and deletions from the start.

Step 3: Chunk the Content Sensibly

Split documents into passages the model can use on their own. Chunk on structure (sections, clauses, paragraphs) rather than a fixed character count, keep a small overlap between neighbors, and prepend the document title and section heading to each chunk so context survives. Chunks that are too small lose meaning, and chunks that are too large dilute relevance and waste tokens.

Step 4: Create Embeddings and Index Them

Convert each chunk to a vector with an embedding model and store it in a vector database alongside its metadata. Pick one embedding model and record its version, because changing it later means re-embedding everything. For corpora with identifiers, part numbers, or exact terminology, also build a keyword (BM25) index. Combining both is covered in our hybrid retrieval article.

Step 5: Retrieve, Rerank, and Assemble the Prompt

At query time, embed the question, retrieve the top candidates, and optionally rerank them with a cross-encoder to keep only the best few. Then build the prompt: instructions first, then the retrieved passages with their source IDs, then the question. Deduplicate near-identical passages and cap the total context size.

Step 6: Instruct the Model to Stay Grounded

Tell the model to answer only from the provided passages, to cite the passage IDs it used, and to say it cannot find the answer when the context does not contain it. Validate in code that every citation refers to a passage that was actually retrieved.

Step 7: Evaluate Retrieval and Generation Separately

Measure retrieval first: for each benchmark question, is the correct passage in the top results? Then measure generation: is the answer faithful to the passages and correct? Splitting the two tells you where to fix problems. Poor retrieval needs better chunking, embeddings, or hybrid search. Poor generation needs prompt changes or a stronger model.

Step 8: Prepare for Production

  • Access control: filter results by the user’s permissions before retrieval results reach the prompt.
  • Freshness: re-index changed documents automatically and remove deleted ones.
  • Latency and cost: cache frequent queries, limit context size, and monitor token usage.
  • Observability: log queries, retrieved passages, and answers so you can investigate bad responses.
  • Feedback: let users flag wrong answers and feed them into your benchmark.

Common Mistakes

  • Skipping the evaluation set and judging quality by a few hand-picked examples.
  • Using one chunk size for every document type.
  • Ignoring metadata, then being unable to filter by date, version, or permission.
  • Stuffing too many passages into the prompt in the hope that more is better.

Build It With Us

If you want a production RAG system without the trial and error, see our document intelligence and OCR service, the enterprise RAG capability page, and the hybrid RAG case study. Or book a consultation to scope your use case.