n4nAI

Why do LLMs hallucinate? The technical reasons

A deep technical breakdown of why LLMs hallucinate, covering training objectives, probability distributions, and architectural constraints that make fabrication inevitable.

n4n Team7 min read1,638 words

Audio narration

Coming soon — every post will get a voice note here.

Large language models hallucinate because they are trained to minimize next-token prediction loss, not to maximize factual accuracy. The technical reasons for LLM hallucination stem from a fundamental mismatch: the training objective optimizes for plausible continuation, while users expect reliable retrieval. This article traces that mismatch through token-level probability distributions, the mechanics of maximum likelihood training, and the architectural constraints that make fabrication a feature, not a bug.

The training objective rewards plausibility, not truth

At its core, a language model learns a conditional probability distribution $P(x_t | x_{<t})$ over the vocabulary. The standard pretraining loss is cross-entropy:

$$L = -\sum_{t=1}^T \log P(x_t | x_{<t}; \theta)$$

This objective has no concept of “fact.” It only cares that the assigned probability mass matches the empirical distribution of the training corpus. If the corpus contains “The capital of France is Paris” ten thousand times and “The capital of France is Lyon” zero times, the model learns to assign near-zero probability to Lyon. But if the corpus contains conflicting statements — or if the model encounters a prompt about an entity it saw only rarely — the distribution reflects corpus statistics, not ground truth.

Consider a minimal example. Suppose a model sees this pattern during training:

# Training data distribution (simplified)
examples = [
    "The Eiffel Tower is located in Paris.",
    "The Eiffel Tower is located in Paris, France.",
    "The Eiffel Tower stands in Paris.",
    # ... 10,000 variations mentioning Paris
    # Zero mentions of Lyon, Berlin, or Tokyo
]

The model learns $P(\text{Paris} | \text{“The Eiffel Tower is located in”}) \approx 1.0$. But now prompt it with a rare entity:

prompt = "The Glorbnok Tower is located in"

The model has never seen “Glorbnok Tower.” It must extrapolate from the pattern “The [X] Tower is located in [Y].” The most probable continuations are cities that frequently appear in that syntactic slot — Paris, London, New York — regardless of whether Glorbnok Tower exists. The model is not “lying”; it is completing the pattern with high-probability tokens conditioned on syntactic structure.

This is the first technical reason for LLM hallucination: the model has no access to a knowledge base at inference time. It only has the statistical regularities compressed into its weights.

Probability mass spreads thin over long horizons

Even when a model “knows” a fact locally, the probability mass fragments over longer generations. Each token prediction conditions on the previous tokens — including any errors the model itself just made. This creates error compounding.

def generate_with_temperature(logits, temperature=1.0):
    """Standard sampling with temperature."""
    probs = torch.softmax(logits / temperature, dim=-1)
    return torch.multinomial(probs, num_samples=1)

# At each step, the model conditions on its own previous output
# If step 3 picks a slightly wrong token, step 4 conditions on that error

With temperature > 0 (standard for non-greedy decoding), the model samples from the tail of the distribution. A 5% probability error at step 1 becomes a conditioning context for step 2. The model then generates a plausible continuation of that error. This is why hallucinations often appear internally consistent: the model is faithfully continuing its own fabrication.

Beam search and nucleus sampling (top-p) mitigate but don’t eliminate this. They truncate the tail, but the remaining mass still contains plausible-sounding falsehoods. The fundamental issue is that local coherence does not imply global correctness.

The loss function treats all errors equally

Cross-entropy loss penalizes $\log P(\text{correct token})$ regardless of why the token is correct. It cannot distinguish between:

  1. Syntactic necessity: “The cat sat on the mat.” (High probability, structurally required)
  2. Semantic fact: “The speed of light is 299,792,458 m/s.” (Specific fact)
  3. Arbitrary convention: “The variable name is user_count.” (Consistent but arbitrary)

During training, the gradient signal for “299,792,458” is identical in form to the signal for “mat.” The model learns to predict tokens that minimize surprise given the training distribution. It has no mechanism to flag “this token represents a verifiable fact” versus “this token completes a common phrase.”

This becomes acute with numbers, citations, and proper nouns. The vocabulary size for numbers is large (each digit or tokenized chunk), so probability mass is diffuse. A model might assign:

P("299,792,458" | context) = 0.12
P("299,792,459" | context) = 0.08
P("300,000,000"  | context) = 0.15  # Rounded, more common in corpus
P("186,000"      | context) = 0.05  # Miles per second, also appears

Sampling yields the wrong exact value with high probability. The model “knows” the order of magnitude but not the precise digits — because the training objective never required it to distinguish precision from approximation.

Context window limits create retrieval gaps

Transformers attend over a fixed context window (typically 4K–128K tokens). Knowledge not in the context must be stored in weights. But weights are a lossy compression of the training corpus.

The parameter-to-token ratio is revealing. A 70B parameter model trained on 2T tokens has ~35 parameters per training token. That is not a database; it is a highly compressed representation where interference between facts is inevitable. This is superposition: multiple facts share the same weight subspaces.

# Conceptual: weight matrix stores many facts in overlapping subspaces
# W @ x retrieves a superposition; the model must disentangle
def forward(x):
    # x: input embedding
    # W: weight matrix (compressed knowledge)
    # Output is a blend of all facts activated by x
    return W @ x

When you prompt “What is the population of [small town]?”, the activation pattern overlaps with patterns for other small towns, other population statistics, and generic “population of X is Y” templates. The model interpolates. This is not a failure of reasoning — it is the expected behavior of a compressed, interpolative system.

Retrieval-augmented generation (RAG) addresses this by putting relevant text in the context window, where attention can operate directly on source tokens rather than weight-compressed approximations. But RAG introduces its own failure modes: retrieval errors, chunking artifacts, and context pollution.

RLHF amplifies confident-sounding errors

Reinforcement learning from human feedback (RLHF) optimizes for human preference, which correlates with confidence, fluency, and apparent helpfulness — not factuality. The reward model learns that:

  • “I don’t know” responses score low (unhelpful)
  • Detailed, structured answers score high (helpful-looking)
  • Hedging language (“it seems,” “possibly”) often scores lower than direct assertions
# Simplified RLHF reward signal
def reward_model(response, prompt):
    # Trained on human comparisons
    # Learns: length + structure + confidence ≈ quality
    features = extract_features(response)
    return reward_head(features)

# Result: model learns to hallucinate detailed answers
# rather than admit uncertainty

This is a documented phenomenon: RLHF increases hallucination rates on unknown topics because the model is penalized for refusing and rewarded for plausible elaboration. The technical reason is straightforward: the reward model is a proxy, and the proxy is gamed.

Some post-training techniques (e.g., process supervision, citation-required training) mitigate this, but they require expensive human annotation of reasoning steps, not just final answers. Most deployed models use outcome supervision, which cannot distinguish a correct answer derived from hallucinated reasoning from one derived from actual knowledge.

Tokenization artifacts create systematic blind spots

Byte-pair encoding (BPE) and similar tokenizers split words into subword units. This creates edge cases where the model cannot “see” the spelling or structure of a token.

# GPT-4 tokenizer example (tiktoken)
import tiktoken
enc = tiktoken.encoding_for_model("gpt-4")

# "strawberry" -> tokens
print(enc.encode("strawberry"))  # [496, 7575, 15232]  (str, aw, berry)
print(enc.encode("strawberrry")) # [496, 7575, 15232, 833]  (extra 'r' = new token)

# The model never sees individual letters "r" inside "berry"
# It sees token IDs. Counting letters requires implicit character-level reasoning
# over subword representations — which the architecture does not natively support.

This is why models famously fail at “count the r’s in strawberry.” The token “berry” (ID 15232) is an atomic unit. The model must learn character composition as an emergent capability over subword tokens — which it does imperfectly, because the training objective never explicitly required it.

Similar issues affect:

  • Non-English languages: Tokenizers optimized for English fragment other languages into many tokens, reducing effective context and increasing error rates.
  • Code identifiers: user_count vs userCount vs UserCount may tokenize differently, breaking pattern matching.
  • Numbers: “299792458” may tokenize as [299, 792, 458] or [2997, 924, 58] depending on the tokenizer, scrambling digit-level relationships.

These are not “reasoning failures.” They are representation artifacts baked into the tokenizer design.

The architecture has no ground-truth verification loop

A transformer forward pass is a pure function: $y = f_\theta(x)$. There is no internal “check” step, no retrieval from an external knowledge base, no symbolic reasoning engine. The model cannot pause, query a fact, and resume.

# What a transformer does (simplified)
def transformer_forward(input_ids):
    x = embed(input_ids)
    for layer in layers:
        x = layer(x)  # Self-attention + MLP
    logits = unembed(x)
    return logits  # Distribution over next token

# No intermediate "is this true?" signal exists
# No mechanism to halt and verify
# The output is the prediction

Chain-of-thought prompting simulates verification by forcing the model to generate intermediate tokens that resemble reasoning. But the generated reasoning is itself subject to the same probability distribution — it can be fluent but false. The model has no access to a truth oracle during generation.

This is why tool use (function calling, code execution, search APIs) is the only architecturally sound way to ground generations. The model emits a structured action; an external system executes it and returns a result; the model conditions on that result. The verification happens outside the model weights.

Quantization and deployment distortions

Post-training quantization (INT8, INT4, GPTQ, AWQ) compresses weights further, introducing approximation error. This error is not uniform — it disproportionately affects low-magnitude weights, which often encode rare or specific facts.

# INT4 quantization error example
import torch

# Original weight
w_fp16 = torch.randn(4096, 4096, dtype=torch.float16)

# Quantize to INT4 (simplified)
scale = w_fp16.abs().max() / 7
w_int4 = (w_fp16 / scale).round().clamp(-8, 7).to(torch.int8)
w_dequant = (w_int4.to(torch.float16) * scale)

# Relative error
error = (w_fp16 - w_dequant).abs() / w_fp16.abs()
print(f"Mean relative error: {error.mean():.4f}")
print(f"Max relative error: {error.max():.4f}")

A 2% mean weight error can flip the top-1 prediction for low-probability tokens — precisely the tokens representing specific facts. The model’s “long tail” knowledge degrades first. This is rarely discussed in hallucination analyses but matters for production deployments running quantized models.

What this means for system design

If you are building on LLMs, the technical reasons for LLM hallucination imply concrete design constraints:

1. Never trust the model as a fact store. Treat every generation as a hypothesis. Use RAG with citation enforcement, or tool use with deterministic verifiers (code execution, API calls).

2. Constrain the output space. Structured outputs (JSON schemas, function calling) reduce the opportunity for fabrication by limiting valid token sequences.

# Function calling forces the model into a verifiable action space
tools = [{
    "type": "function",
    "function": {
        "name": "get_population",
        "parameters": {
            "type": "object",
            "properties": {
                "city": {"type": "string"},
                "year": {"type": "integer"}
            },
            "required": ["city", "year"]
        }
    }
}]
# Model MUST emit valid JSON matching schema
# External system executes, returns ground truth

3. Calibrate confidence, don’t trust it. Model log-probs correlate poorly with factual accuracy on out-of-distribution prompts. Use conformal prediction or a separate verifier model if you need calibrated uncertainty.

4. Monitor for degradation. Quantization, distillation, and continual fine-tuning all shift the hallucination profile. Track hallucination rates on a held-out factual benchmark as part of your CI/CD pipeline.

5. Accept that “I don’t know” is a capability, not a failure. Prompt explicitly for abstention. Fine-tune on refusal examples if your use case demands it. The base model will not refuse reliably — RLHF taught it the opposite.

The decisive takeaway

Hallucination is not a bug that will be “fixed” in the next model version. It is the inevitable consequence of:

  • A training objective that matches corpus statistics, not truth
  • A compressed parametric memory that interpolates rather than retrieves
  • An architecture with no external verification loop
  • A post-training process that rewards confidence over accuracy

Every improvement — larger models, better data, RLHF variants, RAG, tool use — attacks one facet. But the fundamental mismatch remains: next-token prediction on a fixed corpus is not the same task as reliable question answering.

Engineers who internalize this stop asking “how do I prevent hallucination?” and start asking “how do I architect a system where hallucination is detected, contained, or irrelevant?” That shift — from model-centric to system-centric reliability — is the only path to production-grade LLM applications.

Tagshallucinationllm-traininganalysis

Written by

n4n Team

The team building n4n — a single OpenAI-compatible API in front of 240+ models, with automatic fallback, load balancing and pay-per-token metering.

More from n4n Team →

All hallucination in llms posts →