// LOADING
// LOADING
// LOADING_ARTICLE
LLMs generate plausible-sounding text. Without constraints, that plausibility extends to confidently stated falsehoods: wrong dates, invented citations, and subtly incorrect code. In a production application, hallucinations erode user trust and, in high-stakes domains, cause real harm.
This post covers the practical techniques available today for reducing hallucinations to a level where you can ship with confidence.
A language model predicts the next token based on patterns in its training data. It has no concept of "I don't know" unless it has been trained to express uncertainty — and even then, it can express false uncertainty or false confidence situationally. The model does not distinguish between facts it has seen repeatedly and facts it has inferred or confabulated.
Reduction strategies fall into three categories: grounding (give the model the right information), validation (check what the model returns), and design (structure the task to reduce the opportunity for hallucination).
RAG is the single most effective intervention for factual accuracy. Instead of relying on the model's parametric memory, you retrieve relevant documents at query time and instruct the model to answer using only the supplied context.
tsimport Anthropic from "@anthropic-ai/sdk"; const client = new Anthropic(); async function answerWithContext( question: string, contextChunks: string[] ): Promise<string> { const context = contextChunks.join("\n\n---\n\n"); const response = await client.messages.create({ model: "claude-opus-4-8", max_tokens: 1024, system: "Answer the user's question using only the information in the provided context. " + "If the context does not contain enough information to answer, say so explicitly. " + "Do not use any knowledge beyond the provided context.", messages: [ { role: "user", content: `Context:\n${context}\n\nQuestion: ${question}`, }, ], }); return response.content[0].type === "text" ? response.content[0].text : ""; }
The critical element is the system instruction: "do not use knowledge beyond the provided context." This alone reduces factual drift substantially. Pair it with a high-quality retrieval step — see Retrieval-Augmented Generation (RAG), Explained and Embeddings: The Foundation of AI Search for the retrieval mechanics.
When the model needs to return data your application will act on, ask for a structured format and validate it immediately. Unvalidated freeform text is where format hallucinations hide: the model may invent field names, return the wrong type, or fabricate values for fields it cannot determine.
tsimport { z } from "zod"; const ProductSchema = z.object({ name: z.string(), category: z.enum(["electronics", "clothing", "food", "other"]), inStock: z.boolean(), }); async function extractProduct(rawText: string) { const response = await client.messages.create({ model: "claude-opus-4-8", max_tokens: 512, system: "Extract product information as JSON. Use null for any field you cannot determine from the text.", messages: [{ role: "user", content: rawText }], }); const text = response.content[0].type === "text" ? response.content[0].text : "{}"; const parsed = JSON.parse(text); return ProductSchema.parse(parsed); // throws ZodError if shape is wrong }
Zod validation catches shape hallucinations at runtime. See Runtime Validation in TypeScript with Zod for the full validation pattern.
Ask the model to review its own output before returning it to the user. A two-step pipeline — generate, then critique — catches a surprising number of factual and logical errors:
tsasync function generateWithCritique(prompt: string): Promise<string> { const draft = await client.messages.create({ model: "claude-opus-4-8", max_tokens: 1024, messages: [{ role: "user", content: prompt }], }); const draftText = draft.content[0].type === "text" ? draft.content[0].text : ""; const critique = await client.messages.create({ model: "claude-opus-4-8", max_tokens: 1024, system: "Review the draft response below. Identify any factual errors, unsupported claims, or logical inconsistencies. " + "Then produce a corrected version. If the draft is accurate, return it unchanged.", messages: [{ role: "user", content: `Draft: ${draftText}` }], }); return critique.content[0].type === "text" ? critique.content[0].text : draftText; }
This doubles the API call count and cost, so use it selectively for high-stakes outputs rather than every response. For cost management strategies, see Cost and Latency Optimization for LLM Apps.
Instruct the model to express its uncertainty rather than guess. System prompts like "If you are not certain of a fact, say 'I am not sure' and explain what you do know" shift model behaviour toward calibrated responses.
For numerical or verifiable claims, prompt the model to cite the source within the provided context: "Include the specific paragraph number or section title from the context that supports each claim." Citations make hallucinations easier to audit.
Guardrails are automated checks that run before the LLM call (input) or after it (output):
A lightweight output guardrail:
tsasync function isSafe(response: string): Promise<boolean> { const check = await client.messages.create({ model: "claude-haiku-4-5", max_tokens: 10, system: "Reply with only 'yes' or 'no'.", messages: [ { role: "user", content: `Does this response contain harmful, false, or off-topic content?\n\n${response}`, }, ], }); const result = check.content[0].type === "text" ? check.content[0].text.trim().toLowerCase() : "yes"; return result === "no"; }
Using a smaller, faster model (like Claude Haiku 4.5) for guardrail checks keeps latency and cost manageable.
Beyond retrieval and validation, the structure of your prompt influences hallucination rates:
For a deeper treatment of prompt structure, see Prompt Engineering for Developers.
Hallucinations you cannot measure you cannot reduce. Log model inputs and outputs, and run periodic human or automated audits on a sample. Track user correction signals — thumbs down, re-asks, or rephrasing — as a proxy metric. See Monitoring and Observability for Web Apps for the instrumentation infrastructure.
For more on building reliable AI features, explore /interview-prep where LLM reliability is a common topic in system design interviews.
No single technique eliminates hallucinations entirely. The most reliable production systems combine retrieval grounding, structured output validation, and conservative prompt design, with automated guardrails catching edge cases. Apply the right level of rigor for the stakes involved — a creative writing assistant tolerates more variation than a medical information tool — and instrument everything so you can see the failure modes before users do.