What is an AI hallucination in coding?
By Flavio
An AI hallucination is a plausible but false answer. Learn how to catch invented APIs, verify coding-agent claims, and separate output from evidence.
An AI hallucination is a plausible answer that contains false or invented information. In coding, that can be a nonexistent API, an invented package, or a claim that tests passed when they never ran.
AI fundamentals explains why models make things up, with a local model run you can follow. This entry gives you the term and a way to check a coding answer.
How hallucination happens
A language model generates likely continuations from its input and learned patterns. Plausibility can produce an answer that sounds right while getting a fact wrong. OpenAI explains this in why language models hallucinate.
In coding tools
Claude Code combines a model with tools that read files, edit code, and run commands. Cursor Agent can also edit code and run terminal commands.
For Codex, OpenAI’s agents guide explains the distinction between the model and the tools around it. Grok Build is a coding agent with an interactive terminal and headless operation.
Details checked against each vendor’s documentation in November 2026.
These tool capabilities give you ways to check an answer. Ask for the source file or command result that supports a claim, then inspect it.
A concrete example
Give a coding agent this prompt in your repository:
Find the command that runs this project's tests.
Quote the package.json script or project instructions that define it.
If you cannot find a test command, say so. Do not invent one.
Compare the quoted text with the actual file. If the agent runs a command, inspect its exit status and output too. A correct script name and a successful test run are different facts.
When to check for hallucinations
Check unfamiliar API names, dependency suggestions, and summaries of work. This matters especially when you rely on an answer to change a project. Give coding agents checks they can learn from explains how to make those checks part of the workflow.
Pitfalls
Asking the same model whether it is sure can produce another confident answer. Provide the relevant source and require evidence. Anthropic’s hallucination guidance recommends allowing uncertainty and grounding claims in source material.
An invented dependency name can become a security problem. See slopsquatting before installing a suggested package.
Language Model Builder makes small-model training visible on your Mac. Use it with the fundamentals guide to explore generation before trusting a larger model’s coding answer.
Related glossary entries
- Slopsquatting: Slopsquatting is registering a package name a language model invented, so a later install of that name runs someone else's code. What a 2025 study measured.
- Training data: Training data is what a model learned from, and it stops at a cutoff. Dates on the Grok 4.7, GPT-6.1 Terra and Claude cards, and how an agent reads your repo.
- Evals: An eval is a fixed prompt, a real agent run, and a check you can repeat. How Claude Code, Codex, Cursor and the Grok API score that run.
Want me to talk about your product? You can sponsor this site.