跪拜 Guibai
← All articles
Claude · Agent · Interview

How to Stop AI-Generated Code From Becoming a Team's Maintenance Nightmare

By 洛卡卡了 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Teams that hand coding to AI without structured, version-controlled rules see output quantity rise while quality collapses, and the resulting maintenance debt lands squarely on the lead who still has to review every line. Splitting project facts from executable standards and scoping rules by file path keeps AI context lean and compliance high, turning a chaotic multi-developer AI workflow into something a team can actually sustain.

Summary

When a team adopts Claude Code, the same AI prompt produces wildly different code from each developer — camelCase vs. snake_case, console.log vs. structured loggers, bare queries vs. full error handling. A CLAUDE.md file alone fails because it either stays too vague for the AI to execute or grows so long it exhausts the context window and gets ignored.

The fix is a two-tier structure: CLAUDE.md holds project facts (tech stack, commands, directory layout) under 200 lines, while `.claude/rules/` holds detailed, executable coding standards split across files like `coding-style.md`, `api-design.md`, and `database.md`. Path-scoped YAML frontmatter makes rules load only when relevant — frontend rules don't clutter backend sessions and vice versa.

Rules evolve through practice: architectural conventions are written at project start, and recurring code-review failures get codified with positive/negative examples and priority levels. The system is not a set-and-forget solution; outdated and conflicting rules must be pruned every major version. Rules guide the AI toward correct patterns, while hooks provide the hard enforcement for non-negotiable violations like committed secrets.

Takeaways
CLAUDE.md should stay under 200 lines and contain only project facts: tech stack, common commands, directory structure, and basic constraints.
Detailed coding standards belong in `.claude/rules/`, split into separate files by domain (style, API design, database, testing).
Path-scoped rules use YAML frontmatter (`paths:`) to load only when relevant files are touched, preserving context window space.
Rules need positive and negative code examples plus priority levels (P0 must-follow, P1 strongly recommended) so the AI can execute them, not just read them.
User-level rules in `~/.claude/rules/` apply across all projects but are overridden by project-level rules when they conflict.
Symbolic links or git submodules let multiple repositories share a single maintained rule set.
Rules are guidance, not enforcement; hooks must handle hard blocks like committed API keys or skipped lint checks.
Regular rule reviews (every major version) remove outdated constraints and resolve conflicts between different rule files.
New team members should be explicitly onboarded to check `.claude/rules/` and verify AI compliance as their first task.
Rule effectiveness is measured by tracking recurring code-review problem types, CI static-check violation trends, and spot-checks of AI-generated PRs.
Conclusions

AI-assisted coding in teams inverts the traditional onboarding problem: newcomers no longer need to memorize standards, but they do need to know the rules directory exists and how to verify the AI is actually following it.

The CLAUDE.md 200-line limit is not a hard technical cap but a practical attention budget — beyond it, the model starts ignoring later rules, making a bloated file worse than no file at all.

Path-scoped rules are an underused lever; most teams load every rule into every session, wasting context and causing the AI to confuse frontend and backend conventions.

Rules that lack concrete examples are effectively invisible to an LLM. A rule saying 'use logger' is ignored; a rule showing the exact `logger.error()` call with context parameters gets followed.

Monorepo teams often mistakenly assume nested `.claude/rules/` directories will auto-scope by subdirectory, but Claude Code does not work that way — path frontmatter is the only reliable scoping mechanism.

The distinction between rules (guidance) and hooks (enforcement) mirrors the difference between teaching a developer and adding a pre-commit check; both are necessary, and confusing their roles leads to frustration on both sides.

Concepts & terms
Path-scoped rules
Claude Code rules files that include a YAML frontmatter `paths:` field specifying glob patterns. The rule only loads into context when the current session touches matching files, keeping unrelated rules from consuming the context window.
CLAUDE.md vs. rules/ separation
A design pattern where CLAUDE.md holds project-level facts (tech stack, commands, structure) under 200 lines, while `.claude/rules/` holds detailed, executable coding standards split by domain. This prevents context bloat and makes standards maintainable.
User-level rules
Personal coding preferences stored in `~/.claude/rules/` that apply across all projects. They load alongside project-level rules but are overridden by them when conflicts occur, useful for consultants or developers working across multiple codebases.
Hook vs. rule enforcement
Rules guide AI behavior by providing context and examples but do not guarantee compliance. Hooks programmatically intercept tool calls (like file writes) to block specific violations such as committed secrets, providing hard enforcement that rules alone cannot.
From the discussion
Featured comments
Crazy_MT

Does every conversation carry all the rules documents?

洛卡卡了

If there's no path, all rules are loaded at once. If a path is specified, they only load when you're under that path.

See top comments, translated →
Source: juejin.cn ↗ Google Translate ↗ Backup ↗