Sub topic 3
Checking Answers Against Source Material
Assume you know basic Python, JSON, prompts, and how an LLM API returns text. This chapter focuses on checking whether an answer is supported by supplied source material.
Grounding checks
A grounding check compares an answer with trusted source material, such as retrieved documents or database records. It asks whether the answer is supported, contradicted, or absent from those sources.
A simple design is:
Useful checks include requiring citations, comparing claims with retrieved passages, and asking a separate evaluator to label each claim as supported or unsupported. A citation alone is not proof. The cited passage must actually entail the claim.
Handling a failed check
Choose the response based on the risk:
- Retry with clearer instructions and the source material.
- Redact unsupported claims and keep supported text.
- Fall back to a safe response such as “The sources do not provide enough information.”
- Block the answer when unsupported content could cause serious harm.
Do not silently present an ungrounded answer as reliable. Record the failure so repeated problems can be investigated.
> callout warning > A grounding check can miss a subtle contradiction. Treat it as a risk reduction step, not as a guarantee of truth.
Latency and cost
Every extra retrieval, classifier, or evaluator call adds time and may add API cost. Use inexpensive deterministic checks first. Run expensive checks only for high-risk requests or when a cheaper check finds a concern. Caching stable source material can reduce repeated retrieval work.
Measuring errors
A false positive occurs when a guardrail rejects a valid, supported answer. A false negative occurs when it accepts an invalid or unsupported answer.
Measure both on a labeled test set. For high-risk applications, reducing false negatives usually matters more. For customer-facing search, excessive false positives can make the system appear unhelpful. Review examples, not only aggregate percentages, because errors often cluster around particular topics or document types.
Logging and monitoring
Log the check type, decision, model version, source identifiers, latency, and failure reason. Avoid storing raw prompts or answers when they contain personal or confidential data. Monitor rejection rates, retry rates, unsupported-claim rates, and changes after deployments.
Layered defense and limits
Use layers: validate inputs, retrieve trusted sources, constrain the prompt, validate output structure, check grounding, and apply human review where needed. Each layer covers different failure modes. Guardrails cannot make unreliable sources trustworthy, eliminate every prompt injection, or prove that an answer is true. Keep permissions narrow and provide a safe fallback.
Exercise
An answer includes a plausible claim that does not appear in the retrieved documents. What is the safest default response?
Show answer
Mark the claim unsupported and return a fallback explaining that the available sources do not provide enough information.