Why AI invents functions that never existed, and how to ground it
A model that calls a function nobody wrote is not malfunctioning. It is doing the one thing it was built to do: produce likely text. I build Settl, an expense-splitting app, solo with Claude, and I once lost an afternoon to a date helper that existed in exactly zero libraries. Here is the mechanism, and the three ways to shut it down.
Why plausible beats true
There is no symbol table inside the model. At every step it produces a probability distribution over its entire vocabulary and samples one token. Nothing in that step asks "does this identifier resolve". Existence is not a variable the sampler can read.
Two properties make invented names close to inevitable:
- Names are compositional. The tokenizer splits identifiers into common pieces.
format,DateandShortare each high-frequency fragments, so the model can emit a name it never saw once in training, assembled from parts, and every token in it still scores as likely. - The weights have no clock. Training data holds v2, v3 and v4 of a library simultaneously, with no timestamps. A method deleted two major versions ago is still in there, sitting next to its replacement, and neither one is labelled current.
This is why the failure survives a quick read: the import, the argument order and the comment around the invented call are all correct. One token is wrong, surrounded by evidence that the block is right.
Where it invents the most
Density of training examples is the whole story. Expect fabrication where the data thins out:
- Just past the popular surface. The top five methods of a library are safe. The sixth, in some niche submodule, is where prediction takes over from recall.
- Config keys and CLI flags. They read like plain English, so plausibility is near total and
no compiler catches them.
--no-verify-sslversus--insecureis a coin flip. - Your own private code. It never read your helpers, but it read a million codebases sharing your naming conventions, so it predicts the helper you should have written.
- File paths and line numbers inside explanations. Cheap to generate, expensive to trust.
The three grounds
Grounding is not a lookup bolted onto the model. It is putting the real answer into context so the true token outscores the invented one. Three sources, weakest to strongest:
The docs. Paste the actual API reference for the version in your lockfile. Not a link, the text. A link is a promise the model cannot check.
The code. Point it at the file that defines the thing. Real exports in context outrank imagined
ones, and being precise about which file matters (settl.fyi/social/tip-point-at-code).
Execution. Let it run the code and read the error. A failed import is a fact, and facts have no probability distribution. This is the only ground that catches what you forgot to paste.
Copy these prompts
1. Inventory before it writes
Before writing any code, list every export available to me from date-fns at
the exact version in my lockfile, with full signatures. Read the installed
package under node_modules, not your memory of the library.
Do not suggest fixes yet. Do not write any calling code.
2. Make it cite every symbol
For every library function, method and config key you use, add a trailing
comment naming the file where you verified it exists, like:
formatISO(d) // verified: node_modules/date-fns/formatISO.d.ts
If you cannot verify one against a real file on disk, do not use it. Write
UNVERIFIED next to it and stop rather than guessing an alternative.
3. Make it run
Write the smallest script that imports every symbol you just used and prints
the type of each. Run it, then paste the raw output back here unedited
before continuing with the actual task.
The cheapest checks in your editor
- Typecheck, not tests. An invented symbol dies at the type layer. Your tests may never reach that line.
- Grep the exact name. If it appears once in your whole tree, in the line the model just wrote, it is invented.
- Read the import block first. Invented calls arrive with a confident import of a module that genuinely exists. The module resolving is not evidence the member does.
When grounding is not worth the ceremony
Skip it while sketching: invented names in a throwaway plan get replaced anyway. Skip it on tiny
ancient surfaces, because JSON.parse is not going to be imaginary. Skip it when the compiler is
already your grounding, since running the typechecker beats a paste-the-docs ritual.
Do the full ceremony where failure is silent: config files, SQL, CLI flags, dynamic languages, anywhere a wrong name surfaces in production at 3am instead of at build time.
Run this on your codebase
Paste this into a session, with the file you most recently let AI write:
Audit this file for symbols that do not exist.
1. List every imported symbol, library method, config key and CLI flag used
in this file.
2. Verify each against a real file on disk: the package under node_modules,
its type declarations, or my own source. State the exact path you checked.
3. Mark each item VERIFIED with that path, or UNVERIFIED.
4. Change no code yet. I want the list first.
Then, for every UNVERIFIED item, give me the shortest command I can run to
prove it exists or does not, and the one prompt I should use to regenerate
that line with the real API pasted into context.