Skip to content
NEXVYRA LABS home

AI Engineering

Retrieval that holds up in production

Retrieval-augmented generation is easy to demo and hard to get right. The decisions that matter most — chunking, ranking, permissions and citations — and how to make each one measurable.

NEXVYRA Labs Engineering2 min read

Retrieval-augmented generation (RAG) gives a language model the right information at the moment it answers, so it can respond from your documents instead of from memory. The idea is simple. The quality of the result depends almost entirely on the retrieval half, which is where most production problems start.

Chunk by meaning, not by character count

Splitting documents into fixed-size pieces is the default in many tutorials, and it routinely cuts a policy in half or separates a table from its heading. Prefer boundaries that follow the document's structure — sections, headings, list items — and keep useful context such as the document title and section path with every chunk.

  • Respect headings and sections; keep tables and their captions together.
  • Attach metadata to each chunk: source, section, last-updated date, access level.
  • Test chunking choices against your evaluation set rather than by inspection.

Combine search methods, then re-rank

Semantic (vector) search is good at meaning but can miss exact terms such as product codes, names and clause numbers. Keyword search is the opposite. Running both and merging the results — then re-ranking the top candidates — is usually more robust than either alone.

Carry permissions into the index

If a person cannot open a document in your systems, the assistant must not quote it to them. Store access information alongside each chunk at indexing time and filter on it at query time, before anything reaches the model. Retrofitting permissions later is far harder than designing them in.

Make every answer checkable

Ask the model to cite the passages it used, and show those citations to users. Citations do two jobs: they let people verify answers, and they give you a direct signal for evaluation — an answer whose claims are not supported by its citations is a failure, however fluent it sounds.

ts
// Shape the retrieval step so evaluation can inspect it later.
type Retrieved = { chunkId: string; source: string; section: string; score: number; text: string };

function selectContext(results: Retrieved[], floor = 0.6, max = 6) {
  const kept = results.filter((r) => r.score >= floor).slice(0, max);
  return {
    context: kept,
    // Nothing relevant enough: hand off instead of guessing.
    shouldEscalate: kept.length === 0,
  };
}

Measure retrieval separately from answers

When an answer is wrong, you need to know whether retrieval missed the right passage or the model misused it. Log what was retrieved for every request and evaluate the two stages separately: did the right source appear in the top results, and was the answer faithful to what was retrieved?

  1. Retrieval: for each test question, is the correct source in the top results?
  2. Faithfulness: are the answer's statements supported by the retrieved text?
  3. Behaviour: did the system escalate when nothing relevant was found?

Written by the NEXVYRA Labs engineering team. Talk to us about applying this to your systems.

  • AI Engineering3 min read

    Evaluate your LLM feature before it ships

    “It looked good in the demo” is not a release criterion. A practical way to build an evaluation set, choose graders and turn quality into something you can measure on every change.

Start a conversation

Your next big idea starts here.

Tell us what you’re trying to achieve. We’ll help you work out what to build, how to build it and what it will take.