Most of my day runs through Claude Code. It reads files, runs tests and runs shell commands on my machine. When a command fails, the agent reads the error and tries again. That looks cheap, but every retry is one more turn, and each turn reads the whole conversation again. In my sessions that was about 310,000 tokens per extra turn.

So in late September I counted. I went through 511 sessions and found 1,424 shell commands that failed. When I grouped the errors, two thirds of them came from three causes:

  • 322: zsh globs. A command like rg -g *.php with no quotes. When a pattern matches no file, zsh stops and prints “no matches found”, and the command never runs. Bash would pass the pattern through, but my shell is zsh.
  • 243: long scripts inside a heredoc. The agent wrote a 30-line Python script inline with <<EOF. Somewhere on the way the script broke and Python threw a syntax error.
  • 363: wrong place. 271 commands used a path that didn’t exist, and 92 ran git in a folder that wasn’t a repository.

A terminal window with the three most common failures. Wrong path or repo: 363. Unquoted glob: 322. Long heredoc: 243. Together they are 928 of 1,424 failed commands, or 65%.

None of these are hard problems. They are habits. So I added three short rules to CLAUDE.md, the file Claude Code reads at the start of every session:

  1. Quote globs.
  2. If a script is longer than about 20 lines, write it to a file first, then run the file.
  3. Check that a path exists, or that a folder is a git repo, before using it.

After. I ran the same count on the next month: 160 sessions from 14 October to 14 November, 3,717 commands, 200 failed. That’s 5.4%.

The three causes went from 65% of failures to 22%. Heredoc errors almost disappeared, with one in a month. Paths are still the biggest of the three, with 29.

The part I didn’t expect
Not every failure that’s left is a bad one. Some are real problems, like a script that crashed or an API that answered 401, and I want the agent to find those. Others aren’t failures at all: rg exits with 1 when a search finds nothing. So I don’t want this number to go to zero.

The numbers have limits. The two periods are different work from two different setups, and I don’t have the total command count for the first one, so I can only compare shares, not rates. I can say the boring failures shrank a lot. I can’t say exactly by how much.

If you run an agent all day, count the failures before you change any prompt. Two thirds of mine came from three boring habits, and a habit is something you can write a rule for.

Sources