Technical Debt in AI-Generated Code: Duplication
On this page
Learning Objective
Recognize how AI-assisted development can accumulate structural and comprehension debt, measure duplication and architectural drift, enforce boundaries with automated fitness functions, and keep the team's understanding of the system alive as more code is generated.
Concept Explanation
Most of the risks so far show up quickly: a failing test, a vulnerability, a bad dependency. Technical debt shows up slowly. Each AI-assisted change can be locally correct and still make the codebase a little worse: a new helper next to an existing one, a second way to handle errors, a shortcut across a layer boundary, a pattern that differs from its neighbors. Individually harmless, together they produce a system that is harder to change, which is exactly when teams rely on agents most.
The data shows the trend. GitClear's 2025 analysis of 211 million changed lines found an eightfold increase in duplicated code blocks during 2024 and, for the first time, more copy-pasted than moved lines, meaning less refactoring and more repetition. Agents gravitate to adding new code because their context rarely contains every existing helper, and because writing a fresh function is easier than finding and adapting one. Thoughtworks' Technology Radar has repeatedly warned against complacency with AI-generated code, including in its April 2025 edition, for exactly this reason.
A second, subtler debt is comprehension debt: code that works but that no one on the team understands. When large parts of a system were generated and skimmed, the team loses the mental model needed to debug incidents, estimate changes and make good design decisions. Anthropic's 2026 learning study (Day 5) suggests this is not hypothetical: delegating the thinking reduces what people learn.
The countermeasures combine automation and habits. Detect duplication automatically in CI with tools such as jscpd or PMD's copy-paste detector, and tell agents to search for existing helpers before writing new ones. Enforce architecture with fitness functions, automated tests of structural rules, using tools such as dependency-cruiser for JavaScript, import-linter for Python or ArchUnit for Java: the UI must not import the database layer, domain code must not depend on frameworks. Keep conventions explicit in instruction files and examples, so agents reproduce one way of doing things. Reserve time for refactoring, and use agents for it with the characterization-test discipline from Day 13.
For comprehension, make understanding part of the workflow. Ask agents to explain non-obvious changes in pull request descriptions and to record significant decisions in short architecture decision records. Have authors walk reviewers through generated code they will own. Rotate who works in which area, and occasionally write critical code by hand or with the agent as a tutor rather than an author. The goal is a team that can still reason about its system without the agent.
| Debt | Symptom | Countermeasure |
|---|---|---|
| Duplication | Several helpers doing the same thing | jscpd in CI, "search before writing" rule |
| Architectural drift | Imports across forbidden boundaries | Fitness functions such as dependency-cruiser |
| Inconsistent patterns | Several error-handling or data-access styles | Conventions and examples in instruction files |
| Missing refactoring | Growing files and functions | Planned refactoring with characterization tests |
| Comprehension debt | Nobody can explain how a module works | Explanations in PRs, decision records, walkthroughs |
Worked Example: Fitness Functions for Duplication and Boundaries
These checks run in CI next to the tests. The first, a .jscpd.json file, fails the build when duplicated code exceeds 3 percent of the codebase; the second encodes architectural rules that agents and humans must both respect.
{
"threshold": 3,
"minLines": 8,
"minTokens": 60,
"path": ["apps", "packages"],
"ignore": ["**/*.test.ts", "**/generated/**"],
"reporters": ["consoleFull"],
"exitCode": 1
}// .dependency-cruiser.cjs: architecture rules as executable checks
module.exports = {
forbidden: [
{
name: "ui-not-to-db",
comment: "UI code must go through packages/core, never the database layer directly.",
severity: "error",
from: { path: "^apps/web" },
to: { path: "^packages/db" },
},
{
name: "core-is-framework-free",
comment: "Domain logic must not depend on Next.js or React.",
severity: "error",
from: { path: "^packages/core" },
to: { path: "node_modules/(next|react)" },
},
{
name: "no-circular",
severity: "error",
from: {},
to: { circular: true },
},
],
};
// CI: npx jscpd && npx depcruise apps packages --config .dependency-cruiser.cjsWhen an agent adds an import from the UI straight into the database package, the build fails with the rule's comment, which tells the agent what to do instead. The rules turn architecture from tribal knowledge into feedback every contributor receives.
Real-World Examples
- GitClear's 2025 AI code quality research reported that duplicated code blocks of five or more lines rose eightfold in 2024 and that copy-pasted lines exceeded moved lines for the first time in its dataset, signalling declining refactoring.
- Thoughtworks' Technology Radar (volume 32, April 2025) cautioned against complacency with AI-generated code, stressing that AI coding tools still need steering and vigilant code review rather than being trusted by default.
Review Questions & Answers
Their context rarely includes every existing helper, and writing a new function is easier than finding and adapting an existing one, so they often reimplement logic that already exists.
An automated check of a structural rule, such as "the UI must not import the database layer", that runs in CI and fails the build when the rule is violated.
The gap between what the codebase does and what the team understands about it, which grows when large amounts of generated code are accepted without being understood, making debugging and design harder.
Requiring explanations of non-obvious changes in pull requests, recording significant decisions in architecture decision records, author walkthroughs of generated code, and occasionally writing critical code by hand or using the agent as a tutor.