Catalog My learning Blog
AI-Assisted Coding Course: Agentic Development in 30 Days · Phase 5 · Lesson 25 · 5 min read

Technical Debt in AI-Generated Code: Duplication

On this page
  1. Learning Objective
  2. Concept Explanation
  3. Worked Example: Fitness Functions for Duplication and Boundaries
  4. Real-World Examples
  5. Review Questions & Answers

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.

DebtSymptomCountermeasure
DuplicationSeveral helpers doing the same thingjscpd in CI, "search before writing" rule
Architectural driftImports across forbidden boundariesFitness functions such as dependency-cruiser
Inconsistent patternsSeveral error-handling or data-access stylesConventions and examples in instruction files
Missing refactoringGrowing files and functionsPlanned refactoring with characterization tests
Comprehension debtNobody can explain how a module worksExplanations 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.

JSON
{
  "threshold": 3,
  "minLines": 8,
  "minTokens": 60,
  "path": ["apps", "packages"],
  "ignore": ["**/*.test.ts", "**/generated/**"],
  "reporters": ["consoleFull"],
  "exitCode": 1
}
TypeScript
// .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.cjs

When 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.

GitClear's 2025 analysis of 211 million changed lines found duplicated code blocks rose about eightfold in 2024 while moved, refactored lines fell by about 40 percent, a pattern associated with growing maintenance cost.
GitClear's 2025 analysis of 211 million changed lines found duplicated code blocks rose about eightfold in 2024 while moved, refactored lines fell by about 40 percent, a pattern associated with growing maintenance cost.

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.
Key TakeawaysAI assistance can steadily add duplication, architectural drift, inconsistent patterns and comprehension debt, even when every change works. Detect duplication and enforce boundaries automatically with fitness functions in CI, keep conventions explicit, plan refactoring, and preserve the team's understanding through explanations, decision records, walkthroughs and deliberate hands-on work. The aim is a codebase the team can still reason about without the agent.

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.