Why AI-Generated Code Doesn't Work, and What to Check

When AI-generated code does not work, the cause is usually one of a small number of things: the code was copied incompletely, the file was saved with the wrong name or type, a required library did not load, the request was ambiguous, or the code contains a genuine mistake. Checking those in order solves most cases within a few minutes, before you ask the assistant anything.
A calm checklist beats frustration. Here are the common causes, roughly from most to least frequent.
Copy and paste problems
The simplest cause is an incomplete copy. Code blocks in chat windows can be long, and it is easy to miss the first or last lines. The page may then be missing its closing tags or its final bracket, which breaks everything below that point.
Check that the file starts with <!DOCTYPE html> and ends with </html> if you asked for an HTML page. Also check that you pasted the code only, without the chat’s explanatory text, and that “smart” quotation marks did not replace straight ones. Word processors do this automatically, and code with curly quotes will not run. Always use a plain text editor, not a word processor.
Wrong file name or type
If you double-click a file and see raw code in the browser, the file probably saved as .txt or as index.html.txt. Many systems hide file extensions by default. Turn on the option to show them, then rename the file to end in .html.
If the browser downloads the file instead of showing it, or opens a blank tab, the extension or the encoding may be wrong. Re-save the file as plain text with UTF-8 encoding.
A required file or library did not load
Some code depends on files that you do not have. It might reference an image, a stylesheet or a script that lives elsewhere. If the assistant wrote a link to something that does not exist, the part of the page that relies on it will not work.
Open the browser console (right-click, Inspect, Console) and look for red messages mentioning “failed to load” or “not found.” If you see them, tell the assistant: “The page tries to load this file and fails. Rewrite it so everything is inside the one HTML file, or tell me exactly where to get the file.”
Also check your internet connection if the page loads a library from a CDN. Without a connection, it will fail.
An ambiguous request
Sometimes the code runs correctly, but not as you hoped. Words like “simple,” “nice” and “modern” mean different things to different readers. If the assistant’s version differs from what you pictured, the code is not broken, the request was unclear.
Restate the behavior with specifics: inputs, outputs, edge cases and layout. Our guide on writing prompts for code has a template you can reuse. Include the prior result, so the assistant knows what to change.
Outdated or invented details
Assistants are trained on data up to a certain date, and tools change. They may suggest an old method for a library, an install command that no longer works or a setting that has moved. Occasionally they invent a function that does not exist, and the code looks plausible but fails.
When you see an error like “x is not a function” or “unknown option,” paste it back and ask: “Does this function exist in the current version of the library? Show me the documentation page where it is described.” If you cannot find it in the official documentation, assume it was invented, and ask for another approach.
Genuine logic mistakes
Even correct-looking code can compute the wrong thing. This is why testing with known answers matters. If the page loads and the buttons respond, but the result is off, give the assistant concrete numbers: the input, the expected output and the actual output. Wrong results are easier to fix when you point to a specific case.
Environment differences
The code may work in one browser and fail in another, or work on a laptop and fail on a phone. Older browsers lack some features, and some settings block certain behavior, such as playing sound before a click. If a problem appears in only one place, say so in your report: “Works in Chrome on my laptop, fails in Safari on my iPhone.”
Try a smaller version
When you cannot find the cause, shrink the problem. Make a copy of the file and delete features until the bug disappears. The last thing you removed is the likely source. Alternatively, ask the assistant to produce the smallest possible page that demonstrates only the failing feature, then test that alone.
Small test pages are easier to read, and a failure there is hard to blame on anything else. Once the small version works, you can compare it with the larger file and see what differs. This approach feels slow at first but often finds in ten minutes what random edits would not find in an hour.
A quick diagnostic routine
Run through this list in order:
- Does the file end in
.htmland open in a browser? - Does it start and end with the expected tags?
- Is there red text in the console? Copy it exactly.
- Does the page need an outside file or an internet connection?
- Does the output match a hand-calculated answer?
- Does the problem appear in more than one browser?
Whatever you find, include it in your next message. A report with the results of these checks usually leads to a correct fix on the first reply.
What to do next
Keep this checklist in your notes file and use it before you ask the assistant for help. For your next failure, record which step revealed the cause. After a few cases you will see which problems you hit most often, and you can write your prompts to prevent them, for example by always asking for a single self-contained file.