AI Safety, Security, and Governance · Topic 2 of 7
Hallucination Reduction and Answer Verification
Reduce hallucinations with Bedrock Knowledge Bases grounding, contextual grounding checks, confidence scores, semantic similarity and JSON Schema output.
- 5 min
- Bedrock Knowledge Bases grounds answers in your data;
RetrieveAndGeneratereturns citations for source attribution. - Contextual grounding checks score grounding and relevance, with thresholds from 0 to 0.99, and run on model output.
- The check needs a grounding source, a query and the response to guard.
What it is
A hallucination is a fluent answer that is not supported by facts. FMs produce them because they generate likely text, not checked text.
Accuracy verification has three jobs. Ground the answer in trusted content, check the answer against that content before the user sees it, and constrain the shape of the answer so downstream code can validate it.
How it works
Grounding with Amazon Bedrock Knowledge Bases
Amazon Bedrock Knowledge Bases is managed retrieval augmented generation (RAG). It chunks and embeds your documents, stores the vectors, retrieves relevant chunks at query time and passes them to the model as context.
| API | What it returns | Use it when |
|---|---|---|
Retrieve | Matching chunks with a relevance score and source location | You build your own prompt, or want to check the scores |
RetrieveAndGenerate | A generated answer plus citations to the source chunks | You want managed RAG with source attribution in one call |
The citations in the RetrieveAndGenerate response link each part of the answer to the chunks it used. That is your built-in fact-checking trail: a reviewer or a post-processing step can confirm that the cited text supports the claim.
Instruct the model to answer only from the provided context and to say it does not know otherwise. Prompting alone reduces hallucinations but does not guarantee it, so add a check.
Contextual grounding checks
A contextual grounding check is a guardrail policy that scores each response on two measures.
| Score | Question it answers | Low score means |
|---|---|---|
| Grounding | Is the response supported by the source? | The response adds or changes facts |
| Relevance | Does the response answer the user's query? | The response is off topic, even if true |
You set a threshold from 0 to 0.99 for each. A response that scores below the threshold is blocked (or flagged in detect mode). Higher thresholds block more hallucinations and also more acceptable answers, so tune them on real traffic.
The check needs three inputs: the grounding source, the query and the content to guard (the model response). With Converse and ApplyGuardrail you mark them with qualifiers such as grounding_source and query. The check runs on the output only, because it needs the response.
With streaming responses, an irrelevant answer can reach the user before the relevance result arrives. If a blocked answer must never be shown, check before you display it.
Confidence scoring and semantic similarity
FMs on Bedrock do not hand you a reliable probability that an answer is true. Build a confidence signal from what you can measure.
- Retrieval scores from
Retrieve. If the best chunk scores low, the knowledge base probably does not contain the answer, so return "I don't know" instead of generating. - Grounding and relevance scores from the guardrail, logged per response.
- Semantic similarity. Embed each answer sentence and its cited chunk with an embeddings model such as Amazon Titan Text Embeddings, then compare with cosine similarity. A sentence with no close match in the sources is a likely hallucination.
- Self-consistency. Ask the same question more than once and compare the answers. Disagreement signals low confidence.
Route low-confidence answers to a fallback: a safe refusal, a human agent or a clarifying question.
Structured outputs with JSON Schema
Free text is hard to verify. When code consumes the answer, define a JSON Schema for it with required fields, types and allowed values.
- With the
ConverseAPI, define a tool whoseinputSchemais your JSON Schema and require the model to use it. The model returns arguments that follow the schema. - Validate the result in Lambda with a JSON Schema validator. On failure, retry with the validation error in the prompt or fail closed.
- Enums close the door to invented values. A field like
"status": {"enum": ["approved", "rejected", "needs_review"]}cannot return a fourth option that passes validation.
Ask for a source_ids array in the schema. The validator can then confirm that every cited ID really came from the retrieved chunks.
Common mistakes
Fixing hallucinations with fine tuning. Fine tuning changes style and task behavior; it does not keep facts current or cite them. For answers that must match company documents, use RAG with a knowledge base.
Raising temperature to "be more accurate". Higher temperature increases randomness. For factual tasks keep temperature low.
Trusting citations blindly. A citation proves which chunk was retrieved, not that the sentence matches it. Pair citations with a grounding check or similarity test.
Validating JSON by parsing only. Valid JSON can still have missing fields or wrong values. Validate against the schema, not just the syntax.
What to remember for the exam
- Bedrock Knowledge Bases grounds answers in your data;
RetrieveAndGeneratereturns citations for source attribution. - Contextual grounding checks score grounding and relevance, with thresholds from 0 to 0.99, and run on model output.
- The check needs a grounding source, a query and the response to guard.
- Retrieval scores, grounding scores and embedding similarity build a confidence signal; low confidence goes to a fallback.
- JSON Schema plus tool use in
Converseenforces structured output; validate and fail closed. - Prompt instructions alone reduce but do not eliminate hallucinations.
Check yourself
A RAG chatbot sometimes adds facts that are not in the retrieved documents. Which managed control blocks these answers?
A contextual grounding check in an Amazon Bedrock guardrail with a grounding threshold, which blocks responses that are not supported by the source.
What does a low relevance score mean when the grounding score is high?
The answer is supported by the source but does not answer the user's question.
How do you guarantee a downstream system receives only allowed status values from the model?
Define a JSON Schema with an enum for the field, request it through tool use and validate the output, rejecting anything that fails.
Topic 2 of 7 in AI Safety, Security, and Governance
Test yourself on AI Safety, Security, and Governance
The full AWS AIP-C01 question bank has 827 questions, with timed practice exams and an explanation for every answer.
This study guide is independent and is not affiliated with, endorsed by, or sponsored by AWS. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.