Skip to content

Claude Code invents functions, flags or files: how to stop it

By the Toolbay team · Checked against the sources below on

Claude Code invents an API when it writes from memory instead of from your code or the library's docs. Tell it to open the files it talks about before answering, give it the documentation for the versions you use, and run a typecheck and tests after every change so an invented call fails at once.

Why it happens

  • It answers about code it has not opened. Anthropic's prompting guide has a whole section on this for agentic coding.
  • The library changed after the model learned it, so the model remembers an older or a made-up signature.
  • Nothing checks the result. Without a typecheck or a test run, an invented function looks just as good as a real one.

Fixes

  1. Put Anthropic's own wording in CLAUDE.md: "Never speculate about code you have not opened. If the user references a specific file, you MUST read the file before answering."
  2. Give it the real docs for your versions: bundled docs where the framework ships them (Next.js 16.2 and later has them in node_modules/next/dist/docs/), or an MCP server such as Context7 that fetches version-specific docs.
  3. Make checking automatic: a PostToolUse hook that runs your typechecker after each edit, or a CLAUDE.md rule that a task is done only when the typecheck and tests pass.
  4. Ask for a plan first in plan mode (Shift+Tab, or claude --permission-mode plan). A plan that names functions is easy to check before any code is written.

When a skill or vault helps, and when it does not

Helps

  • A vault or skill that holds accurate, versioned reference for your stack gives the agent something true to read instead of guessing.
  • A skill that ends with "run the typecheck and fix every error" builds the check into the workflow.

Does not help

  • A skill written from memory can carry the same invented APIs. Check its claims against the docs before you trust it.
  • Nothing replaces running the code. A typecheck and tests catch what no instructions can.