Catch RAG's made-up answers before your client does
Let the model say it doesn't know, demand a verbatim quote for every claim, and have code retract whatever the source document fails to support.
In brief
- A model does not know what is true or false, so every claim in an answer needs a verbatim quote from the document
- Prompt instructions are not enough: use code to check quotes against the source, compare numbers and dates, and retract claims that don't match
- These techniques reduce hallucinations without eliminating them, so high-risk answers still need a human reviewer
Anthropic acknowledges in its official documentation that even an advanced model such as Claude sometimes generates content that is false or inconsistent with the context it was given.
IBM defines AI hallucinations in similar terms: outputs that sound plausible but are factually wrong. A wrong answer that sounds reasonable does more damage than an “I don’t know.”
For an FDE, this bears directly on whether a product is trusted. Once a client’s employee catches a single invented statement about a contract term, they stop believing the whole system. This guide builds a layer of defense: make the model cite a source for every claim, then use code to check each citation before the answer goes out.
What you will build, and what you need
You will build a five-step pipeline. The model is allowed to say it doesn’t know, must quote verbatim before answering and must attach a quote to every claim. A verifier written in code then checks those quotes against the source document. Any claim that fails is retracted or passed to a human reviewer.
You need Python 3, a plain-text document and access to any LLM. The running example is hypothetical: an insurer wants an assistant that answers employees’ questions from product rules that run to several dozen pages.
The check is necessary because, as IBM explains, generative AI does not “know” what is true or false. Supplying the right document is no guarantee that the model will stick to it.
Step 1: Allow “I don’t know” and ask for quotes first
Anthropic’s guide to reducing hallucinations sets out a basic technique: give the model permission to admit uncertainty. The documentation calls it a simple method that can sharply reduce false information. For documents longer than 20k tokens, Anthropic recommends asking the model to extract relevant passages verbatim before it starts the main task.
Answer only from the document inside the <document> tag.
If the document is not sufficient to answer, write "Not found in the document".
Step 1: extract the relevant passages VERBATIM into the <quotes> tag.
Step 2: answer. Every claim must carry a verbatim quote from step 1.
If you cannot find a quote for a claim, retract that claim.
Return JSON: {"claims": [{"text": "...", "quote": "..."}], "final": "..."}
The retraction instruction follows a verification pattern Anthropic describes: every claim must have a supporting quote, and any claim without one must be retracted.
Check: ask a question the document definitely cannot answer. If the model still produces a number, either the prompt is too loose or the document you supplied is mixed with other content.
Step 2: Write a verifier that checks quotes against the source
In the Step 1 prompt, the same model writes the answer and copies out the quotes. It is quite capable of “quoting” a sentence that appears nowhere in the document, or of returning broken JSON. The checking therefore has to be done by code.
import json, re
def norm(s):
return re.sub(r"\s+", " ", s).strip().lower()
def numbers(s):
return set(re.findall(r"\d+(?:[.,/]\d+)*", s))
def verify(answer_json, document):
try:
claims = json.loads(answer_json)["claims"]
except (json.JSONDecodeError, KeyError, TypeError):
return [], [{"error": "invalid_json", "raw": answer_json}]
doc = norm(document)
kept, retracted = [], []
for c in claims:
q = norm(c.get("quote", ""))
text = c.get("text", "")
if q and q in doc and numbers(text) <= numbers(q):
kept.append(c)
else:
retracted.append(c)
return kept, retracted
This is a simplified version. The function only matches strings after normalizing whitespace and case; it does not handle a model that changes punctuation or Unicode characters. When the JSON is broken, it makes no attempt to guess and returns an error entry for Step 3 to deal with.
Stay skeptical of the verifier itself. A quote that really is in the document may still fail to support the claim attached to it: the model can copy a sentence about the waiting period correctly and pin it to a claim containing a different number. The function therefore adds one cheap check: every number and date in the claim must appear in the quote.
The number check does not understand what a sentence means; it catches only mismatched figures. A claim with no numbers will pass straight through it, so important categories of question still need another form of checking or a human reviewer.
In the insurance example, an employee asks about the waiting period for pre-existing conditions and the model returns three claims. Two have quotes that match the rules verbatim. The third adds a number, and its quote is not in the document, so the verifier retracts it and the user receives only the two sourced claims.
Now take a variant in which the third claim’s quote does appear in the rules, but the number in the claim is missing from the quoted passage. String matching alone would let it through. The number check retracts it.
Check: deliberately alter one quote in a sample JSON response, then change a number in the text of another claim, and run it again. Both claims should end up in retracted. Also try a truncated JSON string: the function should return an error entry and raise no exception.
Step 3: Decide what happens when a claim is retracted
The verifier tells you only which claims lack support. How to handle them is a product decision you need to settle with the client.
kept, retracted = verify(raw_answer, document)
if not kept:
log_for_review(retracted)
reply = "This information was not found in the document."
elif retracted:
log_for_review(retracted) # your own logging function
reply = render(kept) # show only sourced claims
else:
reply = render(kept)
log_for_review and render are functions you write yourself; they belong to no library. Broken JSON triggers the first branch. You may choose to call the model once more before falling back to the “not found” message, but never show the raw string to the user.
The log of retracted claims deserves a close read. A claim may be retracted because retrieval failed to supply the right passage, or because the model made something up even though the document was sufficient. Separate the two when you review the log, because they need different fixes.
Step 4: Run it several times to see where the model wavers
Anthropic suggests a more advanced technique, best-of-N: run the same prompt several times and compare the results, since inconsistency between runs can be a sign of hallucination.
def consistent(answers):
finals = {norm(a["final"]) for a in answers}
return len(finals) == 1
This too is simplified: exact string comparison is too strict for long answers. In practice, compare the key facts, such as numbers or dates, by reusing the numbers function from Step 2. The technique multiplies the cost of model calls by N, so reserve it for high-risk categories of question.
Step 5: When should you move to the Citations API?
Prompting for quotes has the weakness described in Step 2. Anthropic’s Citations API handles that part on the API side: it returns the exact passages that support each claim, so you can verify them and show sources to end users.
Anthropic’s documentation says the API parses and extracts cited_text itself, so citations are guaranteed to be valid pointers to the documents provided.
The limit from Step 2 remains. A “valid pointer” means the quoted passage exists; it may still not support what the model wrote. With the Citations API you can drop the string matching, but keep the check on numbers and dates between the claim and cited_text.
Common mistakes
The first mistake is to rely on prompt instructions with no code to check them. The second is to normalize text too aggressively, such as by stripping out all digits, so that quotes with the wrong numbers slip through. The third is to treat this pipeline as an absolute guarantee.
Anthropic states clearly that these techniques significantly reduce hallucinations but do not eliminate them, and that critical information must still be validated, especially for high-stakes decisions.
IBM also stresses human oversight wherever hallucinations could cause serious harm. For the insurer in the example, answers about payout amounts should go through a human reviewer even when the verifier has passed them.
On site, the retraction log is a measure of trust
LlamaIndex describes the evaluation stage of RAG as a way to measure objectively how accurate, faithful and fast responses are. On a client site, the log from Step 3 can serve as that measure. Where you might have said “the chatbot is fairly accurate,” you can report the percentage of claims retracted in each category of question.
When you read job descriptions for FDE or AI engineer roles, look for terms such as “evaluation,” “guardrails,” “grounding” and “citations.” If they appear, prepare a concrete example of a checking layer you have built to discuss in the interview.
On a CV, a line such as “built a citation verifier that cut the rate of unsourced claims from X% to Y%,” with your own real figures, carries far more weight than “built a RAG chatbot.”
The next time a client asks whether the chatbot makes things up, don’t answer from gut feeling. Open the log of retracted claims and show them.
Was this article useful?
Thanks for the feedback!