Simple Agent
Blog
taxonomy-of-hallucinationgrounding-techniquetemperature-and-samplingpost-processing-validation

LLM Guardrails That Actually Work: Controlling Hallucination in Production AI

June 5, 20266 minSimple Agent Team

Hallucination isn't a single bug. Developers treating it as one build naive filters that miss attribution drift or schema rot. In production traffic, you need to separate three distinct failure modes. Factual errors are claims that contradict your knowledge base or external ground truth—stating a wire transfer clears in 24 hours when policy specifies three business days. Attribution errors occur when the claim happens to be true somewhere on the internet, but the model assigns it to the wrong document, invents a section header, or implies a source chunk supports it when it does not. Format failures are model-generated structs with hallucinated keys, ignored enums, or pseudo-JSON that passes a regex but breaks downstream parsers. Each class demands a different guardrail. Treating them as generic "bad output" burns engineering time.

Grounding technique: forcing the model to cite specific passages before answering

The strongest single intervention is citation-before-synthesis. Do not ask the model to "consider the context" and trust it. Force it to show its work. Retrieve top-k chunks via vector search using an embedding model like OpenAI's text-embedding-3-small or an open alternative. Inject chunks into the prompt with stable, parseable identifiers like chunk_0, chunk_1. Instruct the model to complete two ordered steps: first, list the chunk IDs and verbatim snippets it will rely on; second, generate the answer using only those passages. If the model cites a chunk ID outside the injected set, or if it skips Step 1 entirely, your parser blocks the response before it reaches a user. This design uses chain-of-thought to surface attribution early, before polished prose buries the evidence. It directly targets both factual and attribution errors.

Temperature and sampling settings that reduce (but don't eliminate) hallucination

Sampling settings are often misunderstood as a dial for truth. They are not. Greedy decoding with temperature=0.0 eliminates token variance, but it does not eliminate hallucination; a greedy model will confidently repeat a pre-training fabrication. For factual retrieval pipelines, set temperature between 0.1 and 0.3. Keep top_p at 0.95 or 1.0. Avoid high frequency_penalty or exotic nucleus sampling tricks—they distort the token distribution and can push the model toward lower-probability completions when your retrieved context is sparse. Based on typical production deployments, these settings reduce spurious deviations by roughly 10–15%, but they do not touch root-cause retrieval or reasoning gaps.

Post-processing validation: checking that every factual claim maps to a source chunk

Citations are necessary but not sufficient. A model can quote a passage and still contradict it in the summary. You need post-hoc validation. Split the generated answer into atomic factual claims. For each claim, run an NLI check against the cited chunk using a model such as facebook/bart-large-mnli. Label the relationship as entailment, neutral, or contradiction. Reject claims that fall below an entailment threshold—typically an NLI entailment score below 0.65 or an embedding cosine similarity below 0.75. Independently, run the raw output through jsonschema (Python library) against your API contract to catch format failures like invented keys or type mismatches. If any check fails, return a deterministic fallback, not the raw model output. This closes the gap between "looks grounded" and "is grounded."

Worked example: support bot for a fintech KB — before/after guardrails, concrete failure modes caught

Consider a fintech support bot handling ~2,000 inbound questions daily against 8,000 internal policy documents.

Before guardrails: The pipeline used naive RAG (top-3 chunks via cosine retrieval, temperature=0.7). In production audits, approximately 18% of answers contained at least one hallucination. Three recurring failures drove that number:

  1. Factual error: The bot stated that international wire transfers clear in 24 hours. The KB actually specified three business days when FX review applies. The model blended pre-training knowledge with retrieval.
  2. Attribution error: The bot cited "Section 4.2" of a fee schedule. The retrieved chunk was Appendix B; no Section 4.2 existed. The prompt allowed free-form references, so the model invented a structural identifier.
  3. Format failure: The output contained escalation_required: true, but the downstream automation expected needs_escalation: boolean per the API contract. The malformed payload stalled the ticket queue.

After guardrails: We required chunk UUIDs and direct quotes before synthesis. Retrieval expanded to top-5 chunks with a similarity floor of 0.72. temperature dropped to 0.1 and top_p to 0.95. Post-processing added NLI validation (facebook/bart-large-mnli) and strict jsonschema checking. In audit, the hallucination rate dropped to approximately 4%. The same three failure modes were caught differently: the 24-hour wire claim scored "contradiction" against its cited chunk and triggered a human handoff; the model attempted a chunk UUID not present in the context window, which the parser rejected before user delivery; and jsonschema caught the invalid key before it touched downstream automation. The remaining ~4% were subtle paraphrase distortions on edge cases absent from the retrieval index.

The honest answer: guardrails reduce hallucination, they don't eliminate it — and how to communicate this to stakeholders

Stakeholders inevitably ask if the system is "hallucination-free." The honest engineering answer is no. Guardrails are risk mitigators, not proofs. Even with forced grounding, low temperature, and NLI validation, hallucinations persist when retrieval misses the relevant chunk, when the NLI model itself errs, or when parametric training knowledge overrides the context. Communicate in precision/recall terms, not absolutes. Run red-teaming on held-out adversarial questions and report a measured hallucination rate. Define a residual risk budget: if your guardrails catch roughly 75% of fabrications, you still need a human fallback for the tail. An explicit "I don't know" path is a system feature, not a defect. If zero error is the requirement, the component belongs in a classical rules engine, not an LLM.

Production checklist

  1. Force chunk citation with verbatim quotes in the prompt before generating the final answer.
  2. Set temperature between 0.1 and 0.3; keep top_p near 0.95 and avoid aggressive penalties.
  3. Run an NLI or entailment check against every atomic claim and its cited source chunk.
  4. Validate output structure against a strict jsonschema or Pydantic contract before returning to the client.
  5. Log every citation misfire and contradiction score to telemetry for continuous refinement.
  6. Maintain a deterministic fallback (human handoff or static response) when validation thresholds fail.

Simple Agent handles this automatically.

Ready to build your AI agent?

Guided setup in 40 seconds. Unlimited messages.

Create my agent