claudio.cool
CCDV-FLessons

CCDV-F · Prompt and Context Engineering

Grounding answers in documents, citing the source and saying "not found"

⏱ 3 min readDraft

An internal assistant answers questions about company policies. Asked “how many days of leave do I get for moving house?”, it confidently answers with a number that appears in no document: it filled in what is “usual” at other companies. Grounding is getting the answer to come from the documents you supplied and not from what the model assumes.

What the request needs

Three pieces, together:

  1. The documents, clearly marked. Each in its own tag, with an identifier and its content. If they are long, they go at the top of the request and the question at the end.
  2. An answer rule. “Answer only with what the documents say. If it is not there, answer exactly not found.” Without this permitted exit, the model prefers to fill in rather than admit it does not know.
  3. One citation per fact. Ask for the source citation: the document identifier and the quoted fragment it rests on. A helpful technique: have it extract the relevant quotes first and only then write the answer using just those quotes.

The API also offers native citations: you enable them per document and they return the reference to the exact passage, which is more reliable than asking the model to “cite” in free text, because the cited fragments cannot be made up.

Verify that “not found” works

Add to your evaluation questions whose answer is not in the documents and check that the system answers not found. An assistant tested only with questions that do have answers looks perfect and hallucinates in production. And if you return a value that matches no cited passage, treat it as a hallucination: discard it or flag it.

The whole corpus or only the passages

It is an engineering decision with three variables:

Variable Full context Retrieval
Corpus size Fits comfortably in the window Exceeds the window, or is far larger than what each question uses
Cost You pay for the whole corpus on every request, unless you cache it You pay only for what is retrieved, plus the index cost
Freshness Changes little, or you resend it every request Changes often: update the index without touching the prompt

With a small, stable corpus, passing everything (with prompt caching for the fixed prefix) is simple and loses no information. With a large or changing one, you retrieve only what is relevant and pass that. Retrieval adds its own risk: if it brings the wrong passages, the answer is grounded in the wrong thing.

The typical traps

  • Asking to “be precise” without giving a not found exit.
  • Documents with no identifier, so there is nothing to cite.
  • Accepting a citation the model wrote by hand without checking that the fragment exists.
  • Testing only with questions that do have answers.
  • Always sending a huge corpus when each question uses two paragraphs.
  • Retrieving loose passages from a tiny corpus that fit whole.

On the exam

If the model invents facts, look for the combination of marked documents, a “not found” rule and one citation per fact, not “lower the temperature” or “ask it to be more careful”. If the scenario gives corpus size, cost and change frequency, use them to choose between full context and retrieval.

Quick question

A practice exam question on this domain. It counts towards your progress.

A support platform classifies tickets in Spanish, Portuguese and English with a single prompt. The regression suite has 150 labelled tickets, 50 per language, and the current prompt scores about the same in all three.

Product reports that Portuguese tickets about refunds are often labelled "billing". A developer adds three rules to the prompt about Portuguese refund vocabulary, checks ten Portuguese tickets by hand and sees all ten correct.

After the release, Spanish refund tickets start to be labelled "billing" as well. The developer proposes adding three analogous rules for Spanish.

The platform releases prompt changes twice a week.

How should the team handle this prompt change?

Practise this domain in a practice exam →