Skip to content
Agent Engineering / Workflow

Prevent and catch hallucinations

  • BP-0052
  • v1.0.0
  • draft
  • guideline
  • role: any
  • stage: build
  • stage: verify
  • updated 2026-11-24
  • reviewed 2026-11-24
  • next review 2027-05-24

A hallucination is output that is stated as fact but is not supported by the inputs or the world: an invented function, package, citation, file, number or event. Techniques reduce it; none remove it. Design so the ones that get through are caught before they do harm.

  1. MUST look up, rather than recall, anything specific or current: API signatures, versions, config keys, prices, who holds a role, what a file contains. Use the code index, docs, search or the file itself (BP-0010, BP-0028).
  2. SHOULD restrict the agent to the supplied sources when the task is about those sources (“answer only from these documents”), and for long documents have it extract the relevant quotes first and reason from those.
  3. MUST give the agent explicit permission to say “I don’t know” or “not found”, and treat that as a valid result, not a failure to retry until it guesses.
  1. MUST attach a source to every factual claim that matters: URL, document and quote, or file:line. Remove or mark any claim the agent cannot support.
  2. MUST mark the status of what is reported: verified (checked against a source or by running it), unverified, or assumption. Do not present unverified as verified.
  1. MUST check that a package exists in the registry, is the one intended, and is trustworthy (publisher, age, downloads) before adding or installing it. Code-generating models invent package names, and attackers register those names (BP-0044).
  2. MUST check functions, flags and options against the installed version (types, --help, docs for that version); check that paths exist and URLs resolve before citing them.
  3. MUST compute numbers with code, not in prose, and re-derive any figure copied between documents.
  4. MUST run what can be run: type-check, build and test code before claiming it works (BP-0002).
  1. SHOULD for high-stakes answers, sample more than once and treat disagreement between runs as a warning that needs a source.
  2. SHOULD have a reviewer with fresh context check claims against the cited sources, not against the author’s reasoning (BP-0036).
  3. SHOULD measure hallucination rate in evaluations (unsupported claims, non-existent identifiers, broken citations) and keep every real one as a regression case (BP-0006).

Every agent that states facts, writes code or cites sources; strictest where output is acted on without a human reading it.

Package check before install (Bun):

// author: Stephen Eaton
// pkg-check.ts — usage: bun pkg-check.ts <npm-package> [minAgeDays]
export async function checkNpm(name: string, minAgeDays = 90) {
const res = await fetch(`https://registry.npmjs.org/${encodeURIComponent(name).replace('%40', '@')}`);
if (res.status === 404) return { ok: false, reason: 'does not exist (possible hallucination)' };
if (!res.ok) return { ok: false, reason: `registry error ${res.status}` };
const meta: any = await res.json();
const created = new Date(meta.time?.created ?? 0);
const ageDays = (Date.now() - created.getTime()) / 86_400_000;
if (ageDays < minAgeDays) return { ok: false, reason: `only ${Math.floor(ageDays)} days old — review before use` };
return { ok: true, latest: meta['dist-tags']?.latest, created: created.toISOString().slice(0, 10) };
}
if (import.meta.main) {
const [name, min] = Bun.argv.slice(2);
const r = await checkNpm(name, min ? Number(min) : undefined);
console.log(name, r);
process.exit(r.ok ? 0 : 1);
}

Instruction snippet for an agent file:

- If you cannot find a source, say "not found" — do not guess.
- Cite every factual claim as URL + quote or file:line.
- Mark each result verified / unverified / assumption.
- Check package names in the registry before adding them.
  • Retrying until the agent produces an answer instead of accepting “not found”.
  • Citations that were never opened, or that do not contain the claim.
  • npm install of a package name the model suggested, unchecked.
  • Prose arithmetic in reports.
  • “Tested” with no test run in the log.
  • Specific and current facts looked up, not recalled
  • “I don’t know” allowed and accepted
  • Claims cited; status (verified / unverified / assumption) shown
  • Packages, APIs, paths, URLs and numbers checked mechanically
  • Code run before claimed working; high-stakes output independently verified