Skip to content

Claude Code writes bad code: what causes it and what helps

By the Toolbay team · Checked against the sources below on

Most bad agent output is code that looks right and fails later. In Stack Overflow's 2025 developer survey, 66% of developers named "AI solutions that are almost right, but not quite" as their top frustration. The usual cause is not the model: the agent had no way to check its work.

Why it happens

  • No test or type-check command in CLAUDE.md or AGENTS.md, so the agent never runs one.
  • No definition of done. The agent stops when the code compiles in its head.
  • The task is vague, so the agent fills the gaps with guesses.
  • The change touches code the agent has not read.

Fixes, most effective first

  1. Put the exact test, type-check and lint commands in CLAUDE.md, and say they must pass before a task is done.
  2. Ask for a plan first on anything bigger than a small change, and correct the plan before any code is written.
  3. Give one task at a time with the files it touches and what done looks like.
  4. Ask for a test that fails before the fix and passes after it.
  5. Review the diff with a code-review skill or a second session before you accept it.

A definition of done to copy

CLAUDE.md
# Done means
- npm test passes (paste the summary line).
- npx tsc --noEmit has no errors.
- Only the files named in the task changed; list them.
- If a test looked wrong, say so instead of changing it.

To make the check automatic, add a Stop hook that runs the tests and exits with code 2 when they fail: Claude Code then keeps working and gets your error output as feedback. Start bigger changes in plan mode (Shift+Tab) so you can fix the plan before any code exists.

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

Helps

  • A review or testing skill gives the agent a checklist it would not apply on its own.
  • A vault with your codebase's conventions cuts the guessing on vague tasks.

Does not help

  • No skill helps if nothing runs the tests; add the commands first.
  • On everyday tasks a plain agent with a good CLAUDE.md often does as well as one with skills.