Dev.to AI 🤖 Ai 👁 0 📖 2 min read

How to Get a Coding Agent to Reuse Code Instead of Copying It

A coding agent writes a second copy of a function you already have for two reasons. It never saw the first one, or your prompt made it act like a guest in your code. You can fix both. Engineer's view. This is a contrac

A coding agent writes a second copy of a function you already have for two reasons. It never saw the first one, or your prompt made it act like a guest in your code. You can fix both.

Engineer's view. This is a contractor working in code they don't own. Rather than edit your shared function, they add their own copy beside it. A coding agent makes the same call from your prompt: unless the code is called its own, it adds beside. You pay later, fixing the same bug twice.

TL;DR. AI coding agents duplicate code when the original never reached their context, or when the prompt treats the code as someone else's. Name where shared helpers live, and grant ownership per directory where you want the agent to merge.

Explain Like I'm 5

You hire a painter to fix one wall in your house. The painter finds a crack next to the wall. It's not their house, so they paint around the crack and leave you a note.

Now say you tell the painter, "This is your house now. You'll live here." They fix the crack too. They might also move a shelf you liked.

And if a wall is behind a closed door, no painter fixes it. Nobody saw it.

The Problem

You have shipped this bug before, and it had nothing to do with AI.

A date parser had a time zone bug. You fixed it, wrote a test, and closed the ticket. A week later the same bug came back in invoices. A contractor had pasted their own copy of the parser into the billing module. They didn't own your utilities folder, so they didn't touch it. Your fix never reached their copy.

A coding agent is that contractor, working on every task. Each task gets a fresh session. The agent sees only the files its search returns and the instructions you gave it. If the shared parser isn't in what it read, it writes a new one. If the parser is there, but nothing says the code is the agent's to change, it often writes a new one anyway.

The trend shows up at scale. GitClear studied 211 million changed lines from 2020 to 2024. Lines that were moved, its sign of refactoring, fell from 25% of changes to under 10%. Copy-pasted lines rose from 8.3% to 12.3%. That doesn't prove agents caused it, but it is the pattern you would expect.

Anthropic measured the second cause directly. On small coding tasks, its newest model merged repeated code in under a tenth of tries by default.

The fix: put your shared helpers where the agent will read them, and grant ownership of the code where you want it merged.

Read the full post on The AI Commit

How to Get a Coding Agent to Reuse Code Instead of Copying It carries the rest: the fix and why it works, the code to change for each role, how to tell it worked, when it is the wrong tool, and an interactive model you can play with.

Originally published at theaicommit.com, a daily AI engineering lab for software engineers. The original has an interactive version you can play with, and the full runnable code is on GitHub.

📰 Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.