To reduce LLM hallucinations, give the model the facts it needs at answer time, make it clear that saying "I don't know" is acceptable, constrain its output, and verify claims before users see them. Hallucinations cannot be eliminated completely, because language models generate plausible text rather than look up truth, but a layered design can make them rare and easy to catch. This guide covers why hallucinations happen and the techniques that work in production.

What is an LLM hallucination?

A hallucination is output that sounds confident but is not supported by facts or by the context the model was given. Common forms include:

  • Fabricated facts: invented product specs, dates, or policies.
  • Fake citations: references to documents, URLs, or papers that do not exist.
  • Unfaithful summaries: a summary that adds or changes details from the source.
  • Wrong tool use: calling an API with made-up IDs or parameters.

It helps to separate two cases. Factuality errors contradict the real world. Faithfulness errors contradict the context you supplied. In applications, faithfulness is usually what you can control and measure.

Why do LLMs hallucinate?

A large language model is trained to predict likely next tokens. It has no built-in notion of whether a statement is true, only whether it resembles text it has learned. Hallucinations become more likely when:

  1. The question needs information the model never saw, such as your private data or recent events.
  2. The prompt implies an answer must exist, so the model fills the gap rather than admitting uncertainty.
  3. Retrieved context is missing, irrelevant, or contradictory.
  4. The task demands precise details like numbers, names, or IDs that are easy to get slightly wrong.
  5. Long outputs give small errors more room to compound.

Techniques to reduce LLM hallucinations

Technique What it addresses Effort Notes
Grounding with RAG Missing or stale knowledge Medium Quality depends on retrieval
Allow abstention Pressure to always answer Low Pair with a helpful fallback
Require citations Unsupported claims Low to medium Verify that citations are real
Structured output Free-form drift and invented fields Low Validate against a schema
Tool calls for exact data Wrong numbers, IDs, dates Medium Fetch facts instead of recalling them
Verification step Claims not backed by sources Medium Code checks or a second model pass
Evals and monitoring Regressions over time Medium Measure groundedness continuously

Ground answers in retrieved context

The single most effective step is to stop asking the model to answer from memory. Retrieve relevant documents and instruct the model to answer only from them. What is RAG explains the pipeline. Invest in retrieval quality: good chunking, hybrid keyword and vector search, and re-ranking. If the right passage is not retrieved, the model has nothing correct to ground on.

Make "I don't know" an acceptable answer

Tell the model explicitly what to do when the context does not contain the answer, and give it an exact fallback phrase. Then treat abstention as a success in your evals when the answer truly is not available. A model that never abstains will guess.

Require citations and check them

Ask the model to attach source IDs to each claim. Citations make answers verifiable for users and checkable by code. The key is to validate them: every cited ID must be one you actually passed in, and ideally the quoted text must appear in that source.

Use tools for exact facts

Prices, inventory, order status, dates, and calculations should come from systems of record through tool calls, not from the model's memory. Let the model decide which tool to call and how to phrase the result, but not what the number is. How to Build an AI Agent covers designing tools the model uses reliably.

Constrain the output

Structured output reduces the space where the model can improvise. Ask for JSON that matches a schema with explicit fields such as answer, citations, and confidence, then parse and validate it. Lower temperature helps for factual tasks, although it does not prevent hallucinations by itself.

Verify before you show

Add a verification layer between generation and the user. The Python example below checks that each claim cites a real source and that its supporting quote actually appears in that source. Claims that fail are dropped or flagged.

import json
import re


def normalize(text: str) -> str:
    return re.sub(r"\s+", " ", text).strip().lower()


def verify_answer(raw_output: str, sources: dict[str, str]) -> dict:
    """sources maps source_id -> document text that was given to the model."""
    data = json.loads(raw_output)
    verified, rejected = [], []

    for claim in data.get("claims", []):
        source_id = claim.get("source_id")
        quote = claim.get("quote", "")
        source_text = sources.get(source_id)

        if source_text is None:
            rejected.append({**claim, "reason": "unknown source"})
        elif not quote or normalize(quote) not in normalize(source_text):
            rejected.append({**claim, "reason": "quote not found in source"})
        else:
            verified.append(claim)

    return {"verified": verified, "rejected": rejected, "grounded": not rejected}


if __name__ == "__main__":
    sources = {"doc-1": "Returns are accepted within 15 days of purchase with a receipt."}
    output = json.dumps({
        "claims": [
            {"text": "You can return items within 15 days.", "source_id": "doc-1",
             "quote": "within 15 days of purchase"},
            {"text": "No receipt is needed.", "source_id": "doc-1", "quote": "no receipt needed"},
        ]
    })
    print(json.dumps(verify_answer(output, sources), indent=2))

Exact quote matching is strict but cheap and deterministic. For paraphrased claims, you can add a second pass with a judge model that answers a narrow question such as "Is this claim fully supported by this passage? yes or no".

Measure hallucination rates with evals

You cannot improve what you do not measure. Build a test set that includes questions answerable from your sources, questions that are not answerable, and questions with tempting wrong answers. Track groundedness, correct abstention, and citation validity on every change. LLM Evaluation: How to Test AI Features Before Production shows how to set that up, and Fine-Tuning vs RAG vs Prompt Engineering explains why grounding usually beats fine-tuning for factual accuracy.

In production, log the retrieved context alongside each answer. When a user reports a wrong answer, you can immediately tell whether retrieval failed or the model ignored good context, which call for very different fixes.

Design the UX for imperfect answers

Even with every safeguard, some errors will get through. Design for that:

  • Show sources next to answers so users can verify quickly.
  • Signal uncertainty honestly instead of hiding it.
  • Require confirmation before acting on generated details in high-stakes flows.
  • Make it easy to report a wrong answer, and turn each report into a test case.

Key takeaways

  • Hallucinations come from models generating plausible text, not retrieving truth, so they cannot be fully eliminated.
  • Grounding with good retrieval is the highest-impact fix, and retrieval quality sets the ceiling.
  • Allow and reward abstention when the answer is not in the context.
  • Fetch exact facts through tools and constrain output with schemas.
  • Verify citations and claims in code before showing them to users.
  • Measure groundedness with evals and keep monitoring in production.

Frequently asked questions

Can LLM hallucinations be completely eliminated?

No. Because language models predict likely text, there is always some chance of an unsupported statement. Grounding, verification, and good UX can make hallucinations rare and catchable, which is what production systems aim for.

Does RAG stop hallucinations?

RAG greatly reduces them when retrieval returns the right documents, but it does not stop them entirely. Models can still misread, over-generalize, or add details beyond the context. Pair RAG with citation checks and evals for groundedness.

Does lowering temperature reduce hallucinations?

Lower temperature makes output more consistent and can help on factual tasks, but it does not make the model more knowledgeable. A model can confidently repeat the same wrong answer at temperature zero. Treat it as a minor adjustment, not a fix.

How do I measure hallucination rate?

Create a labeled test set with answerable and unanswerable questions, run your system on it, and score each answer for whether its claims are supported by the provided sources. Use deterministic checks where possible and a calibrated judge model or human review for the rest.