// Section 4 — Sandbox · 3 MIN READ
Hallucination★
A hallucination is an operational failure mode where a model outputs text that is grammatically correct and plausible-sounding, but factually false. In software development, this manifests as inventing non-existent package dependencies, generating phantom API parameters, or referencing codebase files that do not exist.
You encounter hallucinations when:
- An agent recommends installing an npm package that does not exist in the public registry.
- The model invokes a library method (e.g.
fs.readAllSync()) that does not exist in the official API. - The agent claims to have run a command or fixed a file when it hasn't.
The Probabilistic Trap
Hallucination is not a "bug" in the traditional sense; it is a fundamental property of Next-Token Prediction. Language models are probability calculators, not databases or search engines. They select the next token based on statistical likelihood:
- Plausible Generation: If the model reads a prompt containing a custom library, it predicts tokens that look like standard library code (e.g. predicting a
.close()method because most connection objects have a close method, even if your custom class doesn't). - Parametric Cutoff: If the model's parameters do not contain a specific fact (due to knowledge cutoffs), it fills the gap by generating a plausible completion based on its training distribution.
Mitigating Hallucinations
You cannot eliminate hallucinations entirely, but you can reduce them using two core architectural patterns:
- RAG (Retrieval-Augmented Generation): Instead of letting the model guess from its parameters, query your local database and load the exact API documentation directly into the Context Window.
- Automated Validation (The Compiler Check): Run the generated code in a Sandbox execution loop and feed the compilation errors back to the model as a tool result so it can self-correct.
# AVOID
Do not trust code generated by models without verifying it through compiler checks, type validation, or runtime tests.
- Avoid: Committing generated code to production without executing a test suite.
- Write: Configure your agent harness to run
npm run testortsc --noEmitautomatically after file writes to catch hallucinated methods.
# USAGE
Developer A: "The model generated a function call to client.fetchProjectMetadata() but the compiler is throwing 'property does not exist'."
Developer B: "That's a hallucination. The model guessed the method name based on general patterns because we didn't inject the actual client class definitions. Let's load the class types into the context window so it reads the correct API schema."
// SEE_ALSO
// SOCRATIC_VALIDATION
Interactive Concept Quiz
Concept Mastered!
You have successfully completed all Socratic validation questions for this term.