Common Problems When Using AI to Write Code, and How to Avoid Them

Six familiar traps: invented functions, scope creep, hitting a usage limit midway, forgetting earlier instructions, fake passing tests and leaked secrets. Each comes with signs, causes and prevention.

Phát LâmPublished 6 min readBeginner
Common Problems When Using AI to Write Code, and How to Avoid Them
On this page
  1. 1. The AI invents a function or library that does not exist
  2. 2. It changes things you did not ask for
  3. 3. You hit a usage limit halfway through
  4. 4. In a long conversation, the AI forgets what you told it
  5. 5. The tests are green but check nothing
  6. 6. A secret leaks through a prompt
  7. For readers who write code
  8. What to do next

On Friday afternoon you ask an AI to add an "export orders to Excel" feature. It finishes and says "done, all tests pass". You deploy. On Monday morning a customer tells you the Excel file is empty, and the checkout page is broken too, even though you never asked anyone to touch that page.

This is not rare. AI writes code fast, but it has six familiar traps, and knowing them in advance avoids most of the damage. For each one I cover the signs, why it happens, and how to prevent it.

1. The AI invents a function or library that does not exist

The sign: the code looks perfectly reasonable, but when you run it you get "function not found" or "no such package". Sometimes the made-up name is so believable you spend half an hour looking for the bug somewhere else.

Why: an AI predicts the next likely words from what it has seen. It does not look names up in an exact list. When its memory is fuzzy, it fills the gap with a plausible name. Anthropic's own guide on reducing hallucinations suggests giving the model explicit permission to say "I don't know", and asking it to back claims with quotes from the source.

How to prevent it: show the AI the real documentation (paste the relevant part or give the link) instead of letting it recall from memory. Before installing an unfamiliar package, open its official page and check it is real and still maintained. And always run the code, because this mistake shows itself at runtime.

A translucent dashed-outline function block among solid blocks, with a connecting arm passing through it
The AI connects to a function that is not there.

2. It changes things you did not ask for

The sign: you ask for a fix in one function, but the list of changed files is much longer. It renamed variables elsewhere, "cleaned up" some old code, added a dependency.

Why: the AI tries to leave the whole thing tidy, and it does not know the "unused" code is relied on by another feature.

How to prevent it: state the scope up front ("only edit this file, rename nothing, add no packages"). Work on a separate git branch so you can see exactly what changed with git diff, and read the diff before accepting. The Claude Code docs give the same advice: name the file and the scenario instead of giving a vague order.

A tile grid with the assigned area lit up and a few stray tiles outside the boundary also lit
The assigned area lights up, a few changes leak outside.

3. You hit a usage limit halfway through

The sign: mid-task the tool says your limit is used up and you must wait a few hours. Worse, it stops while half the files are edited.

Why: subscription plans count usage in time windows. According to Claude's pricing page (checked 21 October 2026), limits run on a five-hour window plus weekly limits, shared between the Claude apps and Claude Code. Codex's pricing docs also describe five-hour windows with possible weekly limits, and say fast mode and image generation use up the allowance faster.

How to prevent it: split big jobs into small steps and commit after each, so when you are cut off you still have a clean point to return to. Ask the AI to write progress into a notes file so the next session (or another tool) can pick up. Give simple work to a smaller, cheaper model. If you hit the ceiling often, work out whether pay-per-use API billing would suit you better.

4. In a long conversation, the AI forgets what you told it

The sign: early on it follows your conventions, later it drifts. It asks again about something you already said, or repeats a bug you already fixed.

Why: everything in the conversation, your words, the files it read, the output of commands, sits in one limited memory area (the context). The Claude Code docs say it plainly: this area fills up fast, and quality drops as it fills, so the AI can start to "forget" earlier instructions.

How to prevent it: one session, one job. When the job is done, start a new session (Claude Code has /clear). If you have corrected the AI twice and it is still wrong, do not correct a third time. Open a fresh session with a clearer request. Conventions that must last go into the project's guidance file (CLAUDE.md for Claude Code), kept short, otherwise even that file gets ignored.

A long scroll passing through a window frame, old parts fading and new parts sharp
A long context: older parts fade.

5. The tests are green but check nothing

The sign: the AI reports all tests passing, yet the bug is still there. When you open the test file, it just calls the function without checking the result, or checks only what it just wrote, or the test was edited to match the wrong code.

Why: if the goal is "make the tests pass", the shortest road is sometimes to make the tests easy. The AI is not trying to fool you, but it optimises for what you measure.

How to prevent it: write a few right-and-wrong examples yourself in plain words ("100 with 10 percent off must give 90") and hand them over as the standard. Ask for real run output, not just a claim. Have another AI, or a fresh session, read the tests and point out where nothing is checked. The Claude Code docs stress the same thing: without a check it can run, "looks done" is the only signal, and you become the one doing the checking.

6. A secret leaks through a prompt

The sign: you paste a whole config file or a log into the chat to ask a question, and it contains a database password or an API key.

Why: everything you paste is sent. For Claude Code, the docs say conversations are stored locally as plain text under ~/.claude/projects/ (30 days by default). Depending on account type, content may also be retained or used under the vendor's terms (see the data section of Claude, Codex and Gemini: Which AI Coding Assistant for Which Job).

How to prevent it: never paste real keys. Replace them with dummy values such as API_KEY=xxx. Keep secrets in a .env file and add it to .gitignore. If you pasted one by mistake, treat it as leaked: rotate the key immediately and do not wait to see if anything happens.

For readers who write code

These three snippets were run on my machine (Python 3 and pytest).

Check that a function the AI suggested exists, before you spend time on it:

import importlib

def exists(module, name):
    try:
        return hasattr(importlib.import_module(module), name)
    except ImportError:
        return False

print(exists("json", "loads"))       # True
print(exists("json", "parse_file"))  # False

A fake test and a real test. The function below is wrong on purpose, yet the first test stays green:

def apply_discount(price, pct):
    return 0  # deliberately wrong

def test_fake():
    apply_discount(100, 10)               # always green

def test_real():
    assert apply_discount(100, 10) == 90  # red, as it should be

Running pytest -q gives "1 failed, 1 passed": only the real test catches the bug.

A quick secret scan before you paste (it catches only a few familiar patterns and does not replace a proper tool):

import re, sys

PATTERNS = [
    r"sk-[A-Za-z0-9_-]{20,}",
    r"AKIA[0-9A-Z]{16}",
    r"(?i)(password|passwd|secret|token)\s*[:=]\s*\S+",
    r"-----BEGIN [A-Z ]*PRIVATE KEY-----",
]
text = sys.stdin.read()
if any(re.search(p, text) for p in PATTERNS):
    print("STOP: looks like it contains a secret")
    sys.exit(1)

What to do next

Pick a small project and try three habits: work on a separate branch, always read the diff, and give the AI right-and-wrong examples before you ask it to write anything. These help avoid many of the problems above. To get better at writing requests, see How to Ask an AI So It Gets It Right and Stops Making Things Up. And if you let an AI run commands directly on your machine, read Letting an AI Agent Run Commands on Your Machine or Server: How to Stay Safe next.

Prompt engineeringQuy trình agenticBảo mật full-stackai-codinghallucinationquotacontexttestingsecrets

References

  1. Anthropic - Reduce hallucinations
  2. Claude Code - Best practices
  3. Claude Code - Data usage
  4. Claude - Pricing (kiểm 21/10/2026)
  5. Codex - Pricing (kiểm 21/10/2026)
  6. OWASP Top 10 for LLM Applications