RAG · Lesson 79 of 120
Error Analysis
Read the failures.
A score tells you how often you fail, but not why. Error analysis means reading each failure, and sorting it into a cause. The most common cause tells you what to fix next.

- Scores say how often
- Failures say why
- Fix the top cause
What came back instead
We find every question whose fact wasn't in the top three. For each one, we print what the top chunk was instead. Run it. Some misses found the right page but the wrong chunk, like a lone heading. Others found a different page entirely.
errors.pypython
import json
from rag_kit import load_docs, make_chunks, Index
tests = json.load(open("eval_set.json"))
index = Index(make_chunks(load_docs()))
for t in tests:
hits = index.search(t["question"], 3)
if not any(t["fact"] in c["text"] for s, c in hits):
top = hits[0][1]
print(t["question"])
print(" wanted", t["source"], "| got", top["source"], "|", top["text"][:30])Output
What does E03 mean? wanted ebike_errors.md | got ebike_errors.md | # E-bike display error codes What is the APR? wanted finance.md | got finance.md | # Paying monthly Can I spread the payments? wanted finance.md | got finance.md | # Paying monthly How many hours until my e-bike is full again? wanted ebike_battery.md | got warranty.md | ## Parts and electrics Compone
Also uses rag_kit.py
import warnings; warnings.filterwarnings("ignore")
from pathlib import Path
import numpy as np
from sentence_transformers import SentenceTransformer
import faiss # after torch, so the two libraries load safely together
faiss.omp_set_num_threads(1)
from langchain_text_splitters import RecursiveCharacterTextSplitter
def load_docs(folder="data/rag/northwind"):
return [{"source": p.name, "text": p.read_text()} for p in sorted(Path(folder).glob("*.md"))]
def make_chunks(docs, size=300, overlap=50):
splitter = RecursiveCharacterTextSplitter(chunk_size=size, chunk_overlap=overlap)
return [{"source": d["source"], "text": t.replace("\n", " ")}
for d in docs for t in splitter.split_text(d["text"])]
embedder = SentenceTransformer("all-MiniLM-L6-v2", device="cpu")
def embed(texts):
return embedder.encode(texts, normalize_embeddings=True)
class Index:
def __init__(self, chunks):
self.chunks = chunks
self.vectors = embed([c["text"] for c in chunks])
def search(self, query, k=3):
scores = self.vectors @ embed([query])[0]
top = np.argsort(-scores)[:k]
return [(round(float(scores[i]), 2), self.chunks[i]) for i in top]Also uses eval_set.json
[
{"question": "What does E03 mean?", "source": "ebike_errors.md", "fact": "speed sensor"},
{"question": "What is the APR?", "source": "finance.md", "fact": "9.9"},
{"question": "Can I spread the payments?", "source": "finance.md", "fact": "spread the cost"},
{"question": "How many hours until my e-bike is full again?", "source": "ebike_battery.md", "fact": "4 hours"},
{"question": "How long until my money comes back?", "source": "returns.md", "fact": "7 working days"},
{"question": "Can I get next-day delivery on a bike?", "source": "shipping.md", "fact": "Next-day"},
{"question": "How much is a rental deposit?", "source": "rentals.md", "fact": "£150"},
{"question": "When is the York shop open?", "source": "stores.md", "fact": "5:30pm"},
{"question": "How much is a Basic service?", "source": "servicing.md", "fact": "£49"},
{"question": "Do you deliver bikes to France?", "source": "shipping.md", "fact": "complete bikes"}
]Sorting the failures
Lone headings point to chunk headers. Codes point to keyword search. Odd wording points to multi query or HyDE. And a different page entirely may need better embeddings, or a reranker.
| Cause | Example | Fix |
|---|---|---|
| Lone heading | # Paying monthly | chunk headers |
| Exact code | E03 | hybrid search |
| Odd wording | full again? | multi-query, HyDE |
| Wrong page | warranty | reranker |
Summary
Read every failure, sort it by cause, and fix the most common cause first. Next, regression tests.
Key takeaways- Read failures
- Sort by cause
- Fix the top one
Next up: Regression Tests — don't go backwards