DevTools Logo

Hallucination Entropy Analyzer

Hallucination Entropy Analyzer

Compute token-level entropy and perplexity from top-k probabilities. A heuristic, not a hallucination detector.

Runs in your browser
Probabilities within 0.001 of summing to 1 are rescaled.
Try an example

Token distribution editor

3 positions

One block per generated token: the token that was emitted and the top-k candidates with their probabilities.

Sum: 1.0000
Sum: 1.0000
Sum: 1.0000

Metrics

unit: bits
Heuristic only. Token entropy measures how unsure the model was about the next token. It does not detect hallucinations reliably: a confident wrong answer has low entropy and a correct answer can have high entropy.
Formula: H = −Σ p·log2(p) in bits, over the candidates you entered (top-k, so the true entropy can be higher). Perplexity = 2^(mean H).
Cumulative H
0.9147 bits
Average H
0.3049 bits
Perplexity
1.2353
Per-token entropy metrics
TokenEntropyMaxPerplexitySurprisal
cat0.2747 bits1.58501.20980.0589 bits
sat0.4785 bits1.58501.39330.1203 bits
on0.1614 bits1.58501.11840.0291 bits

Entropy by token

Examples

Uniform four-way split

Input
analyzeTokenDistributions([{ token: "x", probabilities: four 0.25 entries }]).metrics[0]
Output
{ entropy: 2, perplexity: 4 }

log2(4) equals 2 bits of entropy and 2^2 gives a perplexity of 4.

Rejected probabilities

Input
analyzeTokenDistributions([{ token: "x", probabilities: [{ token: "a", probability: 0.7 }] }])
Output
{ valid: false, errors: ["Token 1: probabilities must sum to 1 ..."] }

A 0.7 total misses 1 by more than epsilon, so the position is invalid with zero entropy.

About this tool

Hallucination Entropy Analyzer scores top-k token distributions with analyzeTokenDistributions(distributions): each position's Shannon entropy (−Σ p·log2 p), perplexity (2^entropy), and surprise are computed, with cumulative and average entropy rolled up across valid positions.

Validation is strict and deterministic: every probability must be finite within 0–1 and each position must sum to 1 within 1e-9, otherwise the position reports entropy 0 with an explanatory error and the overall result sets valid false.

Example inputs come from scenarioDistributions(kind) — confident, uncertain, or uniform presets with fixed probability tables — so the same kind always analyzes to identical metrics.

How to use this tool

  1. Supply distributions

    Pass token positions with probability lists that sum to 1 — e.g. four 0.25 entries for maximum spread.

  2. Read per-token metrics

    Use entropy for uncertainty, perplexity for effective vocabulary size, and valid/error for rejected positions.

  3. Compare scenarios

    Run scenarioDistributions confident versus uncertain to see low versus high cumulative entropy side by side.

Use cases

Flagging uncertain generations

Treat high per-token entropy as a signal that the model is guessing between alternatives.

Comparing decoding settings

Contrast cumulative entropy across temperatures or prompts with identical distribution shapes.

Teaching entropy basics

Show log2(k) for uniform splits versus 0 for deterministic picks in an interactive demo.

Common mistakes

Mistake:Passing raw logits or unnormalized scores.

Fix:Normalize each position to sum to 1 first — anything else fails validation with a sum-to-1 error.

Mistake:Reading cumulative perplexity as a sum.

Fix:cumulativePerplexity is 2^cumulativeEntropy, not the sum of per-token perplexities.

Frequently asked questions

What does this tool do?

It scores top-k token distributions with analyzeTokenDistributions — per-token entropy, perplexity, and surprise plus cumulative totals — and ships confident, uncertain, and uniform presets via scenarioDistributions.

What do entropy and perplexity mean here?

Entropy is −Σ p·log2 p bits (2 for a uniform four-way split, 0 for a certain token) and perplexity is 2^entropy, the effective number of live alternatives.

Is my model output uploaded or stored?

No. Distribution math and validation run fully client-side.

Why was my distribution rejected?

Every probability must be finite within 0–1 and each position must sum to 1 within 1e-9 — otherwise the position is invalid with a sum-to-1 error, and an empty list is always invalid.

References & standards