32

AI Rules Configuration

⏱️ 30 min

AI Rules Configuration

.cursorrules, CLAUDE.md, and Skills are three complementary, not interchangeable config formats. Sort out what each is good at — and what it isn't — before mixing them. Otherwise you end up writing the same rule three times and nobody on the team will keep them in sync.

If you've never written one, start with Cursor Rules Intro and Claude Code Skills. This page covers how to divide responsibility across the three formats and keep them aligned across a team.

What each format is actually for

FormatLoadingGranularityBest for
.cursorrulesLoaded fully on Cursor startupSingle project fileSmall projects (<500 lines of rules), Cursor-only teams
.cursor/rules/*.mdcAuto-injected by glob, or agent-triggeredMultiple files with frontmatter globLarge projects, load different rules per file pattern
CLAUDE.mdLoaded by Claude Code on startup, with nested loading from subdirsProject + user + per-directoryLong-lived architectural rules, domain knowledge
Skills (~/.claude/skills/)Triggered on demand, not in default contextNamed bundle, SKILL.md + assetsHigh-frequency but task-specific complex workflows (>200-line SOPs)

The key difference:

  • .cursorrules and CLAUDE.md are always-on context — write too much and you blow your token budget every session
  • Skills are lazy-loaded — only read when the AI decides it needs them, so you can park lots of SOPs without polluting context
  • .cursor/rules/*.mdc sits in between, matching by glob

What goes where

The most common mistake is dumping everything into CLAUDE.md until it's 2,000 lines and burns 5K tokens per session. Better split:

Rule typeGoes inExample
Architectural laws (always enforced)Top of CLAUDE.md"no service file over 600 lines", "prefer ObjectId over slug"
Code style.cursorrules or .cursor/rules/style.mdcnaming, import order, tab vs space
Domain knowledgeMiddle of CLAUDE.md"users have free/paid/enterprise tiers, perms via RBAC"
Complex workflow SOPs (>200 lines)Skilldeployment flow, data migration, cert generation
Stack-specific.cursor/rules/{stack}.mdc with globReact rules that only apply to *.tsx
Personal preferences~/.claude/CLAUDE.md (user-level)"use bun not npm", "commit messages in Chinese"

Team sync strategy

Rules only matter if they're committed to git. Three layers, three approaches:

# 1) Project-level — all in
git add .cursorrules .cursor/rules/ CLAUDE.md
git commit -m "chore: update AI rules for new auth pattern"

# 2) Project Skills — committed under .claude/skills/
git add .claude/skills/deploy-prod/
git commit -m "skill: add prod deploy SOP"

# 3) User-level — never committed
# ~/.claude/CLAUDE.md and ~/.claude/skills/ are personal; don't commit

Typical .gitignore additions:

# AI cache + personal settings stay local
.claude/settings.local.json
.cursor/settings.local.json

# But project-level rules MUST be committed:
# !.cursorrules
# !.cursor/rules/
# !CLAUDE.md
# !.claude/skills/

Mandatory review: changes to CLAUDE.md or .cursorrules must go through PR — no direct pushes. These files affect every engineer's AI behavior, equivalent to changing lint config; the team needs to agree.

Real example: how JR Academy layers it

~/.claude/CLAUDE.md                         # Personal (~50 lines)
└─ "use bun not npm"
└─ "auto-push after commit"
└─ Projects registry

jr-academy-ai/CLAUDE.md                     # Project laws (~600 lines)
└─ ABSOLUTE RULES (rm policy, git safety)
└─ Service architecture (600-line cap, single responsibility)
└─ ID-First Principle (post-incident learnings)
└─ Anti-Template Content Rule
└─ MongoDB / Auth / Testing constraints

.cursor/rules/frontend.mdc                  # glob: *.tsx
└─ styled-components rules
└─ z-index conventions

.claude/skills/bootcamp-sync/               # Complex SOP (~400 lines)
└─ SKILL.md: sync curriculum/ → prod
└─ scripts/diff-check.ts
└─ templates/

.claude/skills/lesson-design/
└─ SKILL.md

Result: a typical session loads ~8K of context (the CLAUDE.md); when a deploy or sync is needed, the AI auto-triggers the matching skill and pulls in another 5-10K. About 60% cheaper than putting everything in CLAUDE.md.

Common traps

  • Rules written too abstractly — "code should be clean and maintainable" is dead weight; AI can't act on it. Write "split any service file over 600 lines" instead.
  • Conflicting rules.cursorrules says "use tabs", CLAUDE.md says "4 spaces", AI behavior becomes unpredictable. Audit periodically.
  • Skill trigger conditions are vague — if SKILL.md description says "helps with code review", the AI will never auto-trigger it. Write "auto-triggers on PR review, takes a PR diff, outputs a 5-category issue list."
  • Rules go stale — team locked in "use React 17" six months ago, you've since upgraded to React 19, AI still writes legacy patterns. Audit quarterly.

Next

📚 相关资源

❓ 常见问题

关于本章主题最常被搜索的问题,点击展开答案

.cursorrules 和 CLAUDE.md 是干什么的?

它们是把团队的编码规范、架构约定、踩坑记录沉淀给 AI 的『工作手册』。.cursorrules 给 Cursor 用,CLAUDE.md 给 Claude Code 用 —— 写清 tech stack、命名规则、禁止行为、重复出现的 bug 模式。每次开新对话 AI 自动读取,不用每次粘贴 repo 上下文,团队多人协作输出风格也能稳定。

什么时候应该新增一条 AI rule?

5 个触发条件:同一个新技术 / 模式被 3+ 个 file 用到、某类 bug 反复出现且能被规则提前挡掉、code review 反复给出相同反馈、出现新的 security 或 performance 模式、复杂任务需要团队一致做法。规则不是越多越好,没满足这些条件就先别加。

什么时候应该更新已有 AI rule?

5 个信号:codebase 里出现了更好的实现示例、发现新的 edge case、相关规则刚更新需要联动、底层实现细节变了(比如换了框架版本)、团队反馈这条规则容易引起歧义。更新时打 version + 写 breaking changes,不要直接覆盖 —— 老规则可能还有人在引用。

一条好 rule 应该长什么样?

6 段式:Purpose(这条规则解决什么)、When to Apply(什么场景触发)、Implementation(最小可用示例 + 进阶模式)、Common Pitfalls(已知坑 + 怎么避)、References(关联规则 + 外部文档)。质量 checklist 6 项:actionable / specific / tested / complete / current / linked —— 缺一项规则就是『写了但没人按它做』。

怎么衡量一条 AI rule 真的有用?

4 个指标:usage count(这条规则被引用多少次)、error prevention(避免了多少 bug)、time saved(每周省多少小时)、user feedback 评分。月度 review 用量、季度做大更新、年度做 deprecation review。没有指标的规则,3 个月后通常没人记得它存在 —— rule library 会变成 dead weight。