Why your AI coding agent ships confident, slightly-wrong code (and why rewording the prompt never fixes it)
Your AI coding agent writes something that looks right. It compiles in your head. Then you notice it called user.getProfileById() — a method that doesn't exist anywhere in your codebase. You didn't ask it to make that up. It invented it confidently, in the middle of otherwise-fine code. And that's the worst kind of wrong: not obviously broken, just quietly incorrect in a way you have to catch. If you've run Claude Code, Cursor, or any agent on a real repo, you know this one. Here's why it happens — and why the obvious fix doesn't work. The fix everyone tries first (and why it fails) You reword the prompt. You add "Don't make up functions." It behaves… for one file. Then it does it again. So you add "Only use methods that exist in the provided code." Better for a bit. Then two more sentences — and now your prompt is fifteen rules long and it still invents a method the moment the task gets complex. Here's the part nobody tells you: rewording treats a structural problem as a vocabulary problem. A prompt isn't a contract the model reads once and obeys. It's something the model has to hold in working memory while it reasons about your actual task. A flat list of fifteen rules is unholdable. As the work gets harder, the model spends its attention on the code and quietly drops whichever rule wasn't front-of-mind. "Don't invent methods" is usually rule #11. Under load, it falls out. You can't out-word that. A sixteenth rule just gives it one more thing to drop. The actual cause: shape, not wording The agent invents a method because nothing in the prompt's structure requires it to check. You told it what not to do. You never changed what it actually does, step by step. So stop forbidding the bad thing. Remove the opportunity for it. Instead of a rule it has to remember, make grounding a required step it has to perform. Before — a pile of rules:You are an expert engineer. Write clean code. Follow our conventions. Don't make up functions. Only use methods that exist. Handle er