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.
Ingest
Extract clean text from PDFs, pages and tickets. Keep titles, dates, owners and access rights as metadata.
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?
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.