Dashboard

Why Your AI Coding Agent Copies Outdated Code

An AI coding agent copies outdated code because your repo outvotes your intentions. Here is the audit that proves it and the three fixes that work.

Carlo Zuercher
Carlo Zuercher
Staff Engineer, Platform
10 October 20261 min read

An AI coding agent copies outdated code because your repository is the strongest evidence it has about how you write software. Asked for a new feature, it finds the nearest similar code and imitates it. If the nearest neighbour is a pattern you have spent six months migrating away from, you get a faithful copy of the old way, and the diff looks clean. No linter objects. The fix is not a sharper prompt. It is making the old pattern harder to find than the new one.

Why an AI coding agent copies outdated code rather than your current pattern

Agents assemble context by searching for relevance, not recency. A file that matches your request on function names and types ranks highly whether it was written last week or in 2023. Git history, the pull request where you agreed to stop using the old helper, and the Slack thread where you explained why are all invisible unless something puts them in front of the model.

So the agent is doing exactly what you would do on your first day: look around, copy the house style. The trouble is that it has no way to know your house style is mid-renovation.

The vote-counting problem

Imagine you are moving off a formatMoney() helper onto a Money.format() value object. The migration is real but partial. What the agent sees is a tally:

Pattern

Call sites in repo

What the agent concludes

formatMoney() (old)

42

This is how this team formats money

Money.format() (new)

4

An experiment, or a special case

Forty-two against four is not a close call. Every new feature the agent writes reaches for formatMoney(), which adds a forty-third call site, which strengthens the signal for the next task. The migration does not just stall, it runs backwards, and it does so one reasonable-looking pull request at a time.

This is the same mechanism behind a related complaint, and it is worth reading the two together: an agent that ignores a written style guide is usually obeying the unwritten one in your files instead.

Run the audit before you argue about prompts

You can find out in about a minute whether this is happening. Pick the three or four patterns you are actively migrating away from and count both sides with git grep or ripgrep:

bash
# old vs new, per pattern
for pair in "formatMoney:Money.format" "useOldStore:useStore" "api.post:client.post"; do
  old="${pair%%:*}"; new="${pair##*:}"
  o=$(git grep -c -F "$old" -- '*.ts' '*.tsx' | awk -F: '{s+=$2} END {print s+0}')
  n=$(git grep -c -F "$new" -- '*.ts' '*.tsx' | awk -F: '{s+=$2} END {print s+0}')
  printf '%-18s old=%-5s new=%-5s ratio=%s\n' "$old" "$o" "$n" \
    "$(awk -v o=$o -v n=$n 'BEGIN{printf (n?"%.1f":"inf"), o/(n?n:1)}')"
done

Read the ratio, not the counts. Anything above roughly three to one means the old pattern is still your repository's majority opinion, and an agent will keep siding with the majority. Below one to one, the new pattern can defend itself and you can stop worrying about that one.

Three fixes, cheapest first

Fix

Effort

What it actually changes

Name the pattern in the agent instruction file

Minutes

Adds one line of counter-evidence against dozens of call sites

Mark the old path at the definition

An hour

Puts the warning in the file the agent is most likely to read

Finish the migration or delete the old path

Days

Removes the evidence entirely

  1. Write it down where the agent reads. An instruction file at the repo root is the cheapest intervention and the weakest. One sentence saying Money.format() replaces formatMoney(), never the reverse, is a single vote against forty-two. It helps, and it will lose when the surrounding file disagrees loudly enough.

  2. Put the warning at the definition. A deprecation comment on the old helper, plus a @deprecated annotation your tooling understands, means the agent reads the warning in the same breath as the signature. This is markedly more effective than the instruction file, because it travels with the code the agent is already looking at.

  3. Reduce the call sites. The only fix that fully works is making the old pattern rare. You do not need to finish the migration: get the ratio under one to one in the directories the agent touches most and the problem largely dissolves. A codemod over the twenty easiest call sites buys more compliance than any amount of prompting.

If this sounds like ordinary technical debt work, that is because it is. The difference is the compounding: unmigrated patterns now reproduce themselves at the speed the agent works, rather than at the speed your team writes code by hand.

What to put in the task prompt

Prompting is the weakest of the three levers, so spend it where it is strongest: naming the specific trap rather than asking for good judgment in general.

text
Add the refund flow to the orders service.

Two constraints before you start:
- Money formatting goes through Money.format(). formatMoney() is deprecated.
  You will find many existing calls to it. Do not match them.
- If you find yourself copying the shape of an existing file, tell me which
  file you copied and why, in your summary.

Show me the plan before you edit anything.

The second constraint is the one that earns its place. Making the agent name its template turns an invisible choice into a reviewable one, and you will sometimes find it copied a file you forgot existed. A related trick works for the opposite problem, when you want the agent to reach for your internal library rather than reinventing it.

Three kinds of outdated, which need different answers

Lumping these together is why the problem feels unfixable. They are not the same failure.

A deprecated API is the easy case. There is a definition you own, so you can annotate it, and the replacement is one to one. The ratio audit works cleanly and a codemod is usually feasible.

An old idiom is harder: callbacks where you now use promises, class components where you now use hooks, manual loops where you now use the standard library. There is no single definition to annotate because the pattern lives in the shape of the code rather than in a symbol. Here the instruction file genuinely matters, because it is the only place the rule can be stated at all, and so does picking one exemplary file and telling the agent to use that file as the template.

An abandoned experiment is the trap nobody sees coming. A directory from a spike that was never deleted looks exactly like production code to an agent, and it may be the closest match to a new request precisely because it was exploratory. Delete abandoned branches of the codebase, or move them somewhere the agent is told to ignore. This is the one case where deletion is both the cheapest and the best fix.

Catching it at review time

Assume one or two will get through. The cheap net is a check in continuous integration that fails when the count of a deprecated pattern goes up rather than down. A ratchet, not a ban: record the current count, fail the build if a pull request increases it. That tolerates the existing forty-two while making the forty-third someone's explicit decision.

bash
# ratchet.sh: the count may fall, never rise.
pattern="formatMoney("
allowed=$(cat .ratchet/formatMoney 2>/dev/null || echo 0)
actual=$(git grep -c -F "$pattern" -- '*.ts' '*.tsx' | awk -F: '{s+=$2} END {print s+0}')

if [ "$actual" -gt "$allowed" ]; then
  echo "formatMoney() went from $allowed to $actual. Use Money.format()." >&2
  exit 1
fi
if [ "$actual" -lt "$allowed" ]; then
  echo "$actual" > .ratchet/formatMoney   # lock in the improvement
fi

Two properties make this worth the twenty lines. It never blocks unrelated work, because untouched code keeps its existing count. And it turns the migration into a one-way door, so the gains you make do not quietly erode over the following month.

The review habit that pairs with it is asking one question of every agent-written pull request: what did this copy? If the answer is a file you have been meaning to delete, you have found your next codemod. Everything else about picking and configuring these tools sits in the wider guide to choosing AI coding tools.

Frequently asked questions

Why does the agent ignore my deprecation comment sometimes?

Usually because it never read that file. If the agent can satisfy the request by copying a call site, it may never open the definition where your comment lives. That is the argument for the ratchet in continuous integration: it catches the cases where the warning was simply never in context.

Does a bigger context window fix this?

It helps at the margin and does not solve it. A larger window means more of your repository is visible, which means more of the old pattern is visible too. You are feeding the majority opinion, not correcting it.

Should I just delete the old helper and let things break?

On a small codebase with good tests, that is often the fastest honest answer, because a compile error is a better teacher than a comment. On anything with runtime-resolved call sites, do the codemod first and delete second.

Is this different from the agent writing bad code?

Yes, and the distinction matters when you are deciding what to fix. The code is usually correct and idiomatic for the era it imitates. You are not looking at a capability failure, you are looking at your own repository giving confident, outdated advice.

How did this land?

About the author

Carlo Zuercher
Carlo Zuercher

Staff Engineer, Platform

Carlo works on the platform that turns prompts into running apps. He writes the engineering deep dives and the changelog notes worth reading.

Share

Get the next post in your inbox

One email a month. Product updates, engineering posts, and the best of Built with Swarmz.

I agree to receive emails about AI building tips and Swarmz product news. Unsubscribe any time.