Cost of hallucination retry loop calculator
A common reliability pattern is to verify the model’s output and retry when it hallucinates. It works — but it is not free. Every retry costs another generation, and the verification step itself re-reads the content. This tool models the expected token cost of that loop and shows how much reliability you actually buy.
How it works
Each attempt is a generation plus a verification check. The model fails with probability p (your hallucination rate), so the loop continues until a verified-good answer or until you hit the retry cap. The expected number of attempts is a geometric series:
E[attempts] = 1 + p + p² + ... + p^maxRetries
The initial attempt always runs; each retry runs only if all earlier attempts
failed. Multiply the per-attempt cost (generation + verification) by the
expected attempts to get the expected cost per accepted result. The residual
failure rate — the chance every attempt still hallucinates — is
p^(maxRetries + 1).
Worked example
Suppose your generation produces 500 input and 300 output tokens, your hallucination rate is 15%, your verifier adds 200 tokens per check, and you allow up to 2 retries (3 attempts maximum).
Tokens per attempt = 500 in + 300 out + 200 verification = 1,000 tokens
E[attempts] = 1 + 0.15 + 0.15² = 1 + 0.15 + 0.0225 ≈ 1.17
Expected tokens per accepted result ≈ 1.17 × 1,000 = 1,170 tokens
Residual failure rate = 0.15³ ≈ 0.0034, i.e. about 0.3%
So for a 15% base hallucination rate and 2 max retries, you spend about 17% more tokens than a single generation and drive the failure rate from 15% to 0.3%. Whether that 17% premium is worth it depends on what a wrong answer costs your application.
Designing a cost-effective retry loop
Choose the lightest verifier that works. Verification options range in cost:
| Verifier type | Cost | Suitable for |
|---|---|---|
| Regex / schema validation | Near zero | Structured outputs: JSON, dates, numbers |
| Embedding similarity check | Very low | Checking that the answer is topically relevant |
| Small classification model | Low | Binary “is this factual/on-topic?” judgements |
| Full LLM judge | High | Complex qualitative evaluation, nuanced factual checking |
Using a regex or schema check as a first gate before a more expensive LLM judge can dramatically reduce average verification cost — most valid outputs pass the schema check and never reach the LLM verifier.
Set the retry budget where the residual rate meets your tolerance. For most production use cases, 2–3 retries is sufficient. Going beyond 3 retries usually provides diminishing returns unless the base failure rate is very high.
Do not retry everything. Distinguish between hallucinatable outputs (factual claims, citations, structured data) and non-hallucinatable outputs (summarization of provided text, reformatting). Only apply the loop where the error class genuinely matters.
Tips and notes
The cost multiplier is usually modest when the failure rate is low: at a 10% hallucination rate, the expected attempt count is only about 1.1, so you pay mostly for verification, not for retries. The bigger lever is the verification overhead — a lightweight validator (regex, schema check, or a tiny model) is far cheaper than a full LLM verifier that re-reads everything. Set max retries where the residual failure rate crosses your tolerance: two or three retries usually drives a 10% base rate below 0.1%. And weigh the loop against the cost of a wrong answer — if hallucinations are cheap to tolerate, skip the loop; if they are expensive or unsafe, the multiplier is well spent.