Engineering hallucinations down
Lesson 2 of 3 in Guarding Inputs and Outputs.
A hallucination is a fluent answer without support. At the associate level the fix was "use RAG"; the professional exam wants an accuracy verification system — layers that make unsupported answers progressively harder to ship. Read the ladder bottom-up: ground the generation in retrieved sources, check every draft against those sources with scored thresholds, constrain the output's shape so downstream code never meets a surprise, and for the claims that must be provably right, prove them against formal policy logic. No rung replaces the ones below it — grounding without checking still hallucinates confidently; checking without grounding has nothing to check against.
The accuracy ladder — each layer assumes the ones beneath it
- Ground — Knowledge Bases
Retrieval puts the facts in front of the model: Bedrock Knowledge Bases fetches the relevant passages and the model generates from them, with citations. This is the foundation — the retrieval craft itself (chunking, hybrid search, rerankers) lives in Retrieval Engineering.
- Check — grounding + relevance scores
The contextual grounding check scores every draft twice on 0–1: GROUNDING (is it supported by the source?) and RELEVANCE (does it answer the question?), each against its own threshold you set. Below threshold, the draft is blocked. The scores themselves are confidence scoring — surface them via ApplyGuardrail assessments and log them as CloudWatch metrics.
- Constrain — structured outputs, text-to-SQL
Force the shape: JSON Schema structured outputs make the response parseable by contract, eliminating invented fields and prose wrappers. For quantitative questions, text-to-SQL goes further — the model writes the query, the database computes the answer, and arithmetic becomes deterministic instead of generated.
- Prove — Automated Reasoning checks
Automated Reasoning checks validate drafts with formal logic against a policy compiled from your documents. Unlike every other rung they return structured findings: which rule a claim contradicts, which assumptions went unstated, and verifiable explanations for why valid claims hold.
Structured outputs deserve the close-up, because the exam names JSON Schema explicitly. On supported models, the native path is the Converse API's outputConfig.textFormat of type json_schema with strict mode — the model is constrained to emit JSON that validates against your schema. The portable fallback, which works across models with tool support, is defining a tool whose inputSchema is your schema and forcing the model to call it. Either way, a whole hallucination class dies: no prose preamble around the JSON, no invented field names, no missing required keys — the failures that break the downstream parser rather than the reader.
Two verification signals complete the AIP-C01 exam guide's list. Semantic similarity: embed the generated answer and the retrieved passages, and a low cosine similarity is a cheap, model-free hallucination flag — the same vector machinery as retrieval, pointed backwards at your own output. And abstention: give the schema an explicit way out ("insufficient_context": true) so the model has a legal path other than inventing an answer.
{
"type": "object",
"properties": {
"answer": { "type": "string", "maxLength": 1200 },
"citations": {
"type": "array", "minItems": 1,
"items": { "type": "string", "pattern": "^doc-[a-z0-9-]+quot; }
},
"grounding_confidence": { "type": "number", "minimum": 0, "maximum": 1 },
"insufficient_context": { "type": "boolean" }
},
"required": ["answer", "citations", "grounding_confidence", "insufficient_context"],
"additionalProperties": false
}Strict mode rejects invented fields; minItems: 1 makes an uncited answer invalid by shape; the boolean gives the model a legal way to abstain instead of inventing. Values can still be wrong — the schema is one rung, not the ladder.
Interactive checkpoint quiz (2 questions) — open this page in a browser to take it.