Skip to main content

Hallucination

Hallucination is when an AI model gives you a confident, well-formatted answer that is completely wrong — like a friend who'd rather make something up than admit they don't know. The output reads as if an expert wrote it, but the facts are fabricated.

How It Works

Large language models generate text by predicting the next most probable token. They have no concept of "truth" — they have a concept of "what text usually follows this pattern." This means they can produce content that sounds plausible but is factually incorrect: fake citations, invented API methods, wrong dates, and nonexistent libraries.

As the diagram illustrates, the same model answering the same question can produce both a correct and an incorrect response. The output looks authoritative in both cases because the model is optimized for fluency, not accuracy. It generates text that reads well, regardless of whether the content is true.

Common types of hallucination include factual errors (wrong dates, numbers, or attributions), fabricated sources (fake research papers, URLs that don't exist), API hallucination (inventing function names or parameters that a library doesn't have), and confident extrapolation (claims that sound plausible but have no basis in reality). The more obscure or specific the question, the more likely the model is to hallucinate — it has less training data to draw from.

Why It Matters

Hallucination is the number one risk when using AI for coding, research, or decision-making. Blindly trusting model output can introduce bugs through nonexistent APIs, reference libraries that don't exist, or spread misinformation in documentation. The danger is amplified because hallucinated output looks indistinguishable from correct output at first glance.

A particularly risky scenario is when AI generates code that imports packages or calls methods that don't exist. The code looks syntactically correct, the variable names make sense, and the logic seems sound — but it won't run because the underlying functions are fabricated.

Understanding hallucination changes how you work with AI. Instead of treating model output as fact, you learn to verify claims, cross-check code suggestions, and test imports before committing. This verification mindset is what separates productive AI use from dangerous AI reliance. It's also why grounding techniques exist — to anchor the model's responses in real, verifiable data instead of relying on pattern completion.