Artificial Intelligence

Retrieval-augmented generation, without the hype

Retrieval-augmented generation sounds simple: find relevant documents, pass them to a language model, get an answer. A demo takes an afternoon. A system people can rely on takes more thought, and very little of that thought is about the vector database.

By BuildQore · · 3 min read

The whole pipeline

Answer quality is limited by the weakest stage. If the right passage is never retrieved, no model can use it, so most improvements come from the steps before generation.

01 of 06

Ingest

Extract clean text from PDFs, pages and tickets. Keep titles, dates, owners and access rights as metadata.

A RAG system has two halves: preparing documents, and answering questions.

Chunking matters more than you expect

Fixed-size splitting is easy, and it cuts tables and procedures in half. Prefer the structure the document already has: headings, sections and list items. Repeat the document title and section heading in each chunk, so a passage that says "this limit applies to all plans" still knows what it is about.

Do not rely on vectors alone

Embeddings capture meaning, but they can miss exact strings such as product codes and error numbers. Hybrid retrieval, which combines keyword search (typically BM25) with vector search, handles both. A reranking step then reorders the top candidates using a model that compares each passage directly with the question.

Apply access control at retrieval time, not in the prompt. If a user may not read a document, it should never reach the model in their session.

Measure before you tune

Build a set of real questions, each paired with the document that should answer it. Then you can compare chunking and retrieval changes with evidence rather than impressions. Hit rate at k is a simple starting point: how often does a correct document appear in the top k results?

evaluate.py
def hit_rate_at_k(
    results: dict[str, list[str]],
    expected: dict[str, set[str]],
    k: int = 5,
) -> float:
    """Share of questions with a correct document in the top k."""
    hits = sum(
        1 for question, doc_ids in expected.items()
        if doc_ids & set(results.get(question, [])[:k])
    )
    return hits / len(expected)


results = {"What is the refund window?": ["doc-9", "doc-2"]}
expected = {"What is the refund window?": {"doc-2"}}
print(hit_rate_at_k(results, expected))  # 1.0

Answer quality needs its own checks. Review a selection by hand for accuracy and for claims that do not appear in the cited passages, and track both over time.

Design the honest fallback

  • When retrieval returns nothing relevant, say so. "I could not find this in the documents" is a useful answer.
  • Show citations that open the source, so people can verify a claim in one click.
  • Treat retrieved text as untrusted data. A document can contain instructions aimed at the model, so never let it trigger actions on its own.
  • Offer a route to a person for anything with legal, financial or medical consequences.

Keep reading

All articles

Artificial Intelligence

AI-generated code needs a quality gate

Veracode found AI-written code passes security tests only 56% of the time. Here is the review process that closes the gap.

BuildQore · · 4 min read

Get new articles by email.

Occasional notes on building and testing software. No filler.

Used only for these updates. See the privacy policy.

Want this done on your project?

Tell us what you are building or fixing, and we will talk through how we could help.