AI 编程工具

Claude.md 最佳实践与示例

Claude.md 最佳实践与示例

Claude.md 示例与最佳实践

如果使用 Claude Code 时没有 Claude.md 文件,AI 每次开始会话都得从头了解项目。它不知道你的项目结构、编码规范,也不知道哪些文件绝对不能动。Claude.md 文件可以解决这个问题。如果还没有配置 Claude Code,可以先看安装指南。

你可以把它理解为一张长期贴在代码仓库里的便签。与其反复告诉 Claude 项目如何组织、使用什么技术、哪些文件不能修改,不如写下来,让 Claude 自己读取。

本文介绍 Claude.md 是什么,提供适用于不同项目的实际示例,并说明需要避免的常见错误。

什么是 Claude.md?

Claude.md 是 Claude Code 开始处理仓库时会读取的文件。它相当于为项目长期保留的一份系统提示词。每次 Claude 开始新会话,都能看到这些指令,你不必再重复说明。

为什么要写这个文件?

没有 Claude.md 时:

  • 每次会话都只能从有限的上下文开始
  • AI 生成的代码可能前后不一致
  • 不同会话中的架构决策容易发生偏移
  • 重构的效果时好时坏

有了 Claude.md,开发会更快,输出会更一致,也能减少重复沟通。对于经常维护的项目,花时间写好这个文件,几次会话就能收回投入。

文件应该放在哪里?

放在项目根目录:

my-project/
  ├── src/
  ├── public/
  ├── package.json
  ├── astro.config.mjs
  └── Claude.md

根目录是人和 Claude 都最容易找到它的位置。

示例一:精简版 Claude.md

小项目可以写得简短一些:

# Project Instructions

## Tech Stack
- Astro
- TypeScript
- Tailwind CSS

## Coding Standards
- Use TypeScript whenever possible
- Prefer functional components
- Avoid unnecessary dependencies

## SEO Requirements
- Include descriptive meta titles
- Include meta descriptions
- Use semantic HTML

## Restrictions
- Do not modify package.json without permission

即使只有这些内容,也能发挥作用。Claude 从第一条提示词开始,就知道项目使用的技术和需要遵守的规则。

示例二:用于 Astro 博客的 Claude.md

如果网站重视 SEO,可以在文件中补充内容规范:

# Project Overview
Personal blog built with Astro.

## Content Guidelines
- All articles require frontmatter
- Include title and description
- Add tags
- Add canonical URLs

## SEO Requirements
- Optimize heading hierarchy
- Use only one H1
- Internal linking is encouraged
- Add image alt text

## Performance Rules
- Minimize JavaScript
- Prefer static rendering
- Avoid unnecessary client hydration

## Before Modifying Code
Explain:
1. What will be changed
2. Why it is needed
3. Potential side effects

这样可以提醒 Claude,在生成内容或代码之前,先考虑 SEO 和性能。

示例三:进阶用户的 Claude.md

进阶用户还会加入工作流规则:

# Workflow Rules
Before making any change:
1. Analyze the existing architecture
2. Identify affected files
3. Explain the implementation plan

After making changes:
1. Verify imports
2. Check build compatibility
3. Check for duplicate logic
4. Summarize modifications

## Code Quality
- Follow DRY principles
- Prefer reusable components
- Avoid code duplication

## Git Workflow
Never perform git push, git force-push, or branch deletion without explicit permission.

这些规则能降低 Claude 意外修改仓库的可能性。

示例四:用于 SEO 内容创作的 Claude.md

内容营销工作流可以采用下面的写法:

# Content Creation Rules
Every article must include:
- SEO title
- Meta description
- Canonical URL
- Target keyword
- Internal link opportunities

## Writing Style
- Clear and practical
- Use short paragraphs
- Use descriptive subheadings

## Formatting
- One H1 only
- H2 for major sections
- H3 for supporting topics

## SEO Goals
- Search intent alignment
- Topical authority
- Helpful content

这样可以让 AI 生成的内容始终符合你的 SEO 策略。

最佳实践

指令要具体。 “写出好代码”没有实际指导作用。“使用 TypeScript 严格模式,避免使用 any 类型,优先采用可复用的工具函数”,才是 Claude 可以执行的要求。

重点记录长期规则。 Claude.md 应当记录稳定的项目要求,例如编码规范、架构决策、SEO 规则和工作流要求。“修复导航栏错误”之类的临时任务应该放在提示词中。

保护关键文件。 如果某些文件绝对不能让 Claude 修改,就明确写出来。部署配置、package.json 和云平台设置都是常见的保护对象。

定义工作流。 告诉 Claude 应当如何沟通改动。很多用户会要求它在编辑前说明计划、列出受影响的文件,完成后再提供修改摘要。

记录项目结构。 简单介绍各个目录存放什么内容,可以帮助 Claude 更快地理解代码仓库。

常见错误

把文件写得过长。 一份长达 5,000 词的指令文件反而会适得其反。内容应聚焦于架构、标准和约束。

反复重复同一条规则。 一条清楚的指令,胜过重复三遍。

把临时任务存进文件。 Claude.md 不是待办清单。修复错误、一次性改动等任务应当放在提示词中。Claude.md 保存的是跨会话有效的项目级规则。

可用于生产项目的模板

# Project Overview
Project Type: [describe project]
Tech Stack:
- Astro
- TypeScript
- Tailwind CSS

# Coding Standards
- Use TypeScript
- Avoid duplicated logic
- Prefer reusable components

# SEO Rules
- One H1 per page
- Descriptive meta titles
- Meta descriptions required
- Internal linking encouraged

# Performance
- Minimize JavaScript
- Prefer static generation
- Optimize images

# Restrictions
Do not modify package.json or deployment configuration without permission.

# Communication
Before making changes:
1. Explain the plan
2. List affected files

After making changes:
1. Summarize modifications
2. Highlight risks

最后的想法

Claude.md 写得好不好,没用过时可能觉得无所谓,用过之后才知道差别。当 Claude 无需提醒就能遵循你的约定,写这个文件投入的时间很快就能省回来。

如果你经常使用 Claude Code,就花 15 分钟写一份 Claude.md。它是项目基础设施的一部分,不应该被当作可有可无的配置。

相关文章:

Newman

BePhil 作者与网站开发者,关注设计系统、前端开发与清晰的思考。