Stop Letting AI Write Lazy, Duplicated Code Patterns in Your Repository
Letting LLMs write sloppy or duplicated code creates a feedback loop where the model copies its own bad patterns. Write clean code to train your agent to follow your exact standards.
Impact: Medium
Why it matters
You can immediately improve AI output quality by refactoring your codebase's patterns instead of just writing longer system prompts.
TL;DR
- 01AI reads your codebase to determine how things are done here instead of applying first-principles design.
- 02Accepting poor patterns trains the LLM to generate progressively worse, highly duplicated code.
- 03Subjective agent instructions often fail to prevent bad patterns; clean codebases are the best prompt.
The Feedback Loop Danger
LLMs are sponges that absorb everything in your repository. When you prompt an AI to build a new route or background job, it doesn't reason from first principles; it searches your open workspace and git history. If you previously allowed a four-line duplicated conditional check like if (user.isActive && !user.isSuspended), the model treats this as your intentional, preferred style. By the fifth endpoint, the model generates the exact same copy-pasted logic, multiplying technical debt.
Why System Prompts Fail
Many developers try to fix this by adding strict rules inside files like AGENTS.md or .cursorrules. However, enforcing subjective rules like "do not write redundant comments" or "write elegant code" is notoriously difficult for models to follow consistently. Standard linters cannot easily catch these conceptual duplication issues.
Maintain Code for Humans and Agents
The true solution is to "write code like a human will maintain it." If you extract a helper function, the LLM will discover and use that helper function in its next generation. By maintaining clean, modular, and DRY structures, you implicitly train the LLM to follow those exact high-quality patterns without bloating your prompts.
Try it in 2 minutes
if (user.isActive && user.hasPermission('read') && !user.isSuspended && account.status === 'open') { /* ... */ }javascript
✓ When to use
- When building projects with agentic coding environments like Cursor, Claude Code, or Copilot.
- When establishing team guidelines for code reviews of AI-generated pull requests.
✕ When NOT to use
- For disposable one-off scripts where longevity and future AI interaction are non-existent.
What to do today
- Refactor any duplicated logical blocks into clean, shared helper functions before prompting your agent for new endpoints.
- Review your .cursorrules or AGENTS.md to ensure they complement a clean repository structure rather than trying to fix messy code after the fact.
What the community says
“The problem of duplicated code described in the article, goes in a different way in reality: AI does not update 4 places in the same way, but implement them a slightly different.”
“I have tried prompting it out and providing strong guidelines in my AGENTS.md against it, but I still get _way_ too many useless 'explain the code' style comments...”
“...if LLMs reduce the cost of maintaining more explicit or verbose code, we should use that to raise the standard, not preserve compromises made for human convenience.”
Sources