Skip to main content
Now that the Retrieval-Augmented Generation (RAG) pipeline is running, the next step is systematic testing: send a variety of questions, inspect retrieved context, identify failure modes (hallucination, poor recall, or ambiguous answers), and then tune ingestion and retrieval parameters.
A slide titled "Testing your RAG System" on a light background with a large dark curved shape on the right featuring the word "Demo" in blue. Small copyright "KodeKloud" appears in the bottom-left corner.
This demo used a small mixed corpus containing two classic books: The Adventures of Sherlock Holmes and Frankenstein. Below are the queries we used, how we interpreted the outputs, and which knobs we adjusted to improve results.

Quick overview of core code (abbreviated)

These helper functions are used throughout the examples below.

Simple interactive test — mixed corpus

Run the interactive test against the hybrid retriever (vectors + BM25):
Example answer when both books were indexed:
Because both books were present, the RAG system returned mixed evidence from multiple documents. To focus queries on Sherlock Holmes alone, we reset the Chroma DB and re-ingested only the Sherlock Holmes texts.

Re-ingest only Sherlock Holmes

Reset the vector DB and re-ingest a single book to narrow retrieval scope:
Rerunning the narrator question after re-ingestion produced a clearer answer:
Example improved answer:

Testing character and setting questions

  • Some direct fact questions work well (e.g., identifying Scotland Yard inspectors).
  • Other queries are sensitive to phrasing. Example: the query What is Holmes' address? initially returned “I don’t know.” Rephrasing produced the expected result.
Failing then succeeding queries:
Successful response example:

Key knobs to tune (what they do and why they matter)

Use these controls to influence retrieval and final LLM prompt composition.
Tips for tuning:
  • Start with small k_each and final_k for fast iteration; increase if you need more candidate evidence.
  • Adjust chunk_size and overlap per document type (narrative vs. technical).
  • Always re-ingest after changing chunking parameters.

Reciprocal Rank Fusion (RRf) — merging ranked lists

We used RRf to merge BM25 and vector ranking lists. This is a simple but effective fusion technique:

Chunking implementation

A simple chunker used when ingesting plain text documents:
Remember: if you change chunk_size or overlap, you must re-ingest the corpus for changes to take effect.

CLI options for runtime tuning

The script exposes flags to tune retrieval at query time.
Examples of adjusting retrieval budgets:
Note: Increasing k_each and final_k provides the LLM with more context but does not guarantee a correct answer. Often phrasing or chunking changes are necessary.

Testing methodology — query categories to run

Use a consistent test suite that includes:
  • Known-answer queries: verify correct retrieval and supporting chunks.
  • Paraphrase/rephrasing: test semantic robustness.
  • Out-of-corpus queries: check that the system abstains instead of hallucinating.
  • Ambiguous/referential questions: ensure the system either clarifies, anchors, or abstains.
Examples that should return abstention (not in corpus):
Abstention response example:

Dealing with ambiguity and partial support

If the LLM returns partial or uncertain answers:
  • Add anchors in the query (story title, character name).
  • Increase k_each and final_k to add more evidence.
  • Tune chunking at ingestion to ensure relevant facts are grouped.
  • Use synonyms and paraphrases in tests to probe retrieval sensitivity.
Examples:
A focused fact-check example (Irene Adler):
Example result:

Ingestion tuning — practical tip

Chunk size and overlap are applied at ingestion time. To experiment:
Changing chunking parameters requires re-ingesting the data and resetting the vector store (e.g., Chroma). Always backup or version your embeddings/database before resetting.

Testing summary and next steps

  • Create a test suite with known answers, paraphrases, ambiguous, and out-of-corpus queries.
  • Use that suite to evaluate whether the system retrieves relevant chunks, abstains appropriately, or hallucinates.
  • Iterate on ingestion (chunking), retrieval budgets (k_each, final_k), reranking/fusion strategy (e.g., RRf), and prompt templates.
  • Compare results across embedding models and LLMs to determine the best combination for your corpus and use case.
Testing approach summary:
  • Create a suite of queries that includes known facts, paraphrases, ambiguous questions, and clearly out-of-corpus questions.
  • Use these to evaluate whether the system retrieves relevant context, abstains properly, or hallucinates.
  • Tune ingestion (chunk_size, overlap) and retrieval (k_each, final_k) and compare results.

Final notes and references

  • RAG systems reduce hallucination risk by grounding answers in retrieved context, but they remain sensitive to retrieval quality, chunking strategy, and prompt engineering.
  • Use the testing methodology above to find failure modes, then iterate on ingestion, retrieval budgets, reranking, and prompts.
  • The full example code and repository for this lesson are included with the course materials — reproduce, tune, and extend the pipeline with your own data.
Useful references: Thanks for following this lesson — experiment with the knobs, and reach out to the course community if you want help troubleshooting specific retrieval failures.

Watch Video