Lesson 3 of 4 Input and Output Safety Controls

Reducing and Detecting Hallucinations

This lesson covers grounding, verification techniques and deterministic alternatives that reduce and detect hallucinations, which cannot be eliminated entirely.

Learning outcomes

After completing this lesson, you will be able to:

  • Ground answers in knowledge bases with citations.
  • Verify responses with grounding checks and other techniques.
  • Use structured outputs and deterministic tools for facts.

Grounding with knowledge bases

A model asked a question without sources answers from what it learned in training, which may be outdated, incomplete or simply invented. Grounding gives the model authoritative sources to answer from instead. Amazon Bedrock Knowledge Bases retrieve the passages most relevant to each question and return citations that link statements in the answer to the source chunks they came from, so users and reviewers can check them.

The prompt reinforces grounding by instructing the model to answer only from the provided passages, to cite them, and to say that it does not know when the passages do not contain the answer, rather than filling the gap from memory. A low temperature reduces creative embellishment. Grounding also makes verification possible: each claim in a draft answer can be checked against the retrieved evidence, by a contextual grounding check in Bedrock Guardrails or by a separate verification step.

Select all that apply

Reducing invented answers

Exam skills 3.1.3

Choose the measures that reduce hallucinations in a RAG assistant.

Log in to try this activity Interactive activities are free with an account.

Verification techniques

The guardrail contextual grounding check blocks responses whose grounding or relevance score falls below thresholds. Automated Reasoning checks validate answers against formal rules derived from policy documents. Semantic similarity verification splits a response into claims and computes the embedding similarity between each claim and the retrieved passages, flagging claims with no close support. Confidence scoring combines signals: retrieval scores, grounding scores, agreement across several sampled responses (self-consistency) and an LLM judge's faithfulness rating. Low-confidence answers can be withheld, labelled or sent for review.

The following diagram compares the original design with the corrected one.

Stopping a finance assistant from inventing figures. Reading report excerpts let the model state numbers that appear nowhere. A text-to-SQL tool that runs validated, read-only queries returns the figure from the data warehouse for the model to explain, and a contextual grounding check blocks unsupported narrative answers.
The model explains the figure; it never produces it.
Match the pairs

Verification techniques

Exam skills 3.1.3

Match each hallucination control with how it works.

Log in to try this activity Interactive activities are free with an account.

Structured outputs and deterministic results

JSON Schema enforcement, through forced tool use or structured output features plus validation, prevents the model from inventing fields or returning malformed data, and enums prevent invented categories. For numerical or factual questions about structured data, text-to-SQL lets the model generate a query, which is validated (read-only, approved tables, row limits) and executed with least privilege credentials against the database; the model then explains the result. The numbers come from the data, so they are deterministic and traceable. Tools for calculations serve the same purpose for arithmetic.

The following table summarises the techniques.

TechniqueReduces or detects
RAG with citations and "answer only from context"Unsupported claims; enables user verification
Contextual grounding checkUngrounded or irrelevant responses (blocks)
Automated Reasoning checksAnswers that contradict formal policy rules
Semantic similarity per claimIndividual unsupported statements
Self-consistency and judge scoresLow-confidence answers
JSON Schema enforcementMalformed or invented fields
Text-to-SQL and calculation toolsInvented numbers and arithmetic errors
Worked example

Invented figures

A finance assistant answers questions such as "What was revenue in the Nordic region last quarter?" by reading report excerpts, and sometimes states figures that do not appear anywhere.

Numerical facts belong in the data warehouse, not in generated text. The assistant should use a text-to-SQL tool that runs validated, read-only queries and returns the figure, with the model explaining the result; for narrative questions, a contextual grounding check blocks unsupported answers.

Check yourself

Which guardrail check validates answers against policy rules with formal logic?

Show answer

Automated Reasoning checks. The contextual grounding check compares answers with retrieved sources.

Exam Tips

Remember for the exam

  • Ground answers in retrieved sources and allow "I don't know".
  • Block ungrounded RAG answers: contextual grounding check.
  • Policy logic validation: Automated Reasoning checks.
  • Numbers from data: text-to-SQL with validation; arithmetic with tools.