Agentic AI System Design / Chapter 3: Retrieval & Perception: Embeddings, Search & Vision

Scenario 3.1RAG · Regulatory Documents

Every New Document Makes the Answers Worse

Difficulty Pressure QualityScaleLatency
Three-panel illustration of the scenario “Every New Document Makes the Answers Worse”

1The Setup & Tipping Point

You have just joined Corvane Pharma, a drug maker selling 9 product families in 14 markets, as Lead AI Engineer on Regulatory Affairs. Eight months ago the team launched an assistant over the regulatory library: standard operating procedures, product labeling, and submission guidance, 120,000 documents averaging 16,000 tokens each.

  • Ingestion: every document is cut into fixed 512-token chunks, embedded as 1,024-d vectors, and stored in one vector index.
  • Answering: each question is compared against every stored vector; the top 5 chunks by cosine similarity go to a frontier model, which writes the answer.

Staff ask it about storage conditions, dosing language, and filing requirements before they update a label or reply to a health authority, and Medical Information specialists use it live, on calls with doctors and pharmacists.

RAG-audit · golden set and production sample
Same 240 questions2-market pilotFull corpus
2,000 docs120,000 docs
Answer accuracy91%58%
1Recall@5 (answer passage among the 5 retrieved)94%61%
2Median time to answer, at peak load1.9 s10.4 s
Audit of 400 wrong answers from production
3Missed a condition stated in the next passage (“except in children under 12”)36%
4Used another market’s near-identical clause, or blended two markets34%
5Question about a whole document, answered from one passage22%
Right passages retrieved, answer still wrong8%

Last week the assistant gave the wrong market’s storage temperature, minus its “unless reconstituted” exception. QA caught it before a label update; a fix plan is due this week.

2Digest & Disarm

The questions worth asking

You

What structure do the documents have?

The team

Nine in ten are native digital files with headings and sections, and every document carries its market, product, type, and effective date.

You

What kinds of questions do staff ask?

The team

About 65% ask for a detail within one market and product, 20% compare markets, and 15% ask about a whole document.

You

What is the latency budget?

The team

Under four seconds end to end, of which retrieval and ranking get 350 ms.

You

What does a wrong answer cost compared with no answer?

The team

No answer sends staff to the source documents for about twenty minutes. A wrong one can end up in a label update or a reply to a health authority.

The instinctive trap

Retune the knobs and swap the parts. One of the bolder answers is simply wrong. The rest help, so they are not wrong, but they are not enough, and trying them reveals the second layer.

Wrong

“Send the top 40 chunks; the context window is huge.” — Recall rises a little, but most of the extra chunks are other markets’ near-identical clauses, exactly what produces blended answers. The one clause that matters gets lost in the middle, and every answer gets slower on a live call.

Not enough

A different chunk size. Chunk size is a trap in both directions. Bigger chunks keep rules and exceptions together, but their embeddings blur toward an average, and cross-market mix-ups get worse. Smaller chunks sharpen matching, but cut more rules away from their conditions.

Not enough

A market filter. Tagging chunks with their market stops most cross-market mix-ups for the 65% of questions that name one market. It helps little with the 20% that compare markets, whose top 5 can still come from just one of them, and nothing for the 15% about a whole document or a rule separated from its exception. It shortens the scan, but every question still compares against every vector in its market. A filter is a good ingredient, not an architecture.

Not enough

A router model. A small model that classifies each question into a market and product works, but it adds a model call to every query, it must be retrained whenever a market or product family is added, and a single label misroutes the 20% of questions that compare markets.

Not enough

A newer embedding model. Better vectors rank near-identical clauses a little better, but no embedding can recover a condition that chunking cut away from its rule.

What this really is

It is a navigation problem. The assistant searches one pile of fragments, where an expert would go to the right document, then the right section. None of the fixes above change that: a question about a whole document still gets one passage, and every new document adds competitors for every question.

It is not a generation problem. In 92% of wrong answers, the problem started in retrieval: the right text was in the index, but the model never received it, or received it beside another market’s near-identical version.

The system today, with the symptoms numbered to match the exhibit

Root-cause map

Symptom
Structural cause
Fixed by
Missed conditions stated in the next passage
Another market’s clause, or two markets blended
91% on the pilot, 58% on the full corpus
1.9 s on the pilot, 10.4 s on the full corpus
Whole-document questions answered from one passage
Fixed-size chunks ignore the documents’ own structure
One flat index ranked by similarity alone, with market and product tags unused
Every new document adds competitors for every query
Every question is compared against every vector, as in the pilot
Only passage-sized units exist in the index
Decision 1
Decision 2
Decision 3

3The Pro Architecture

Find the shelf, then the page, then read the whole page. Every new component traces to a decision.
Find the shelf, then the page, then read the whole page. Every new component traces to a decision.

Decision 1Search small, read big: structure-aware parent–child chunks

Fixes: context fracture: conditions cut away from their rules

Split documents along their own headings, not at a token count. Embed small leaf chunks of about 128 tokens for sharp matching, each carrying its heading path (Label → Storage → Reconstituted). When a leaf matches, hand the generator its whole parent section, capped at a few thousand tokens, so the rule arrives with its exception. The unit you search is no longer the unit you read, and because every passage arrives with its document and heading path, each answer can cite the exact section it came from.

Decision 2Scope before you search, then fuse and rerank

Fixes: semantic crowding: other markets’ clauses, and accuracy falling and latency rising as the corpus grows

Finding the right document and finding the right clause are different tasks, and a flat search tries to do both in one step. Do them in stages. Turn the metadata the documents already carry into filters: when a question names a market, a product, or both, search only those partitions. When it names neither, search the top layer of summaries from Decision 3 first, 120,000 vectors instead of 15 million, and narrow to the documents it points to. There is no router to train: a new document simply adds one more summary. For comparisons, run one scoped search per market the question names, in parallel, and answer market by market, each with its citation; the slowest of those searches sets the latency. If the scope stays ambiguous, ask, and flag the question for review; never blend two markets into one answer. Inside the scope, run keyword search, which catches clause numbers and exact terms, alongside dense search, and merge the two ranked lists with reciprocal rank fusion. Search each partition with an approximate nearest-neighbor index instead of comparing against every vector: partitions keep each search small, and its search time barely grows as documents are added. A cross-encoder then rereads the top 50 candidates against the question and keeps the best 5, inside the 350 ms budget. Store the leaf vectors in int8 rather than float32: four times as many vectors then fit in the memory today’s index uses, and the cross-encoder, which reads the text itself, makes up for the small loss in precision.

Decision 3Add a summary layer for document-level questions

Fixes: global blindness: whole-document questions answered from one passage

At ingestion, summarize each section, then summarize the summaries, up to one summary per document. Index every layer with its depth. Questions about a whole document match the upper layers; detail questions can descend layer by layer to the leaves. The top layer, one summary per document, doubles as the map that Decision 2 searches when a question names no market.

4Beyond: The Signal Ladder and Stretch Round

That was 1 of 18

The Complete Edition has all 18, published progressively on Leanpub. The free Core Edition has 6 scenarios, one from every chapter, plus the framework behind them.