最新n1n v2.0.1 正式上线!企业级大模型接口聚合平台 (LLM API Gateway),为您接入 500+ AI Models,价格低至 1 折, 立即尝试

使用单个AGENTS.md统一Claude Code、Cursor与Codex配置

作者
  • avatar
    姓名
    Nino
    职业
    Senior Tech Editor

如果你同时使用多个AI编码代理,你可能最终会拥有三个不同的指令文件:用于Claude Code的CLAUDE.md、一堆用于Cursor的.cursor/rules以及用于Codex的AGENTS.md。它们一开始可能完全相同,但一个月后,你会发现一个文件还在使用npm,另一个改成了pnpm,第三个则忘记了测试文件应该紧挨着业务代码存放。

这就是指令文件产生漂移的方式。解决办法很简单:将AGENTS.md作为唯一的真实数据源,并让所有工具特定的文件保持精简。本文将展示目录结构、每个工具实际加载AGENTS.md的方式以及你可以直接复制的模板。

your-repo/
├── AGENTS.md            # 规范源:命令、目录结构、规范、完成定义
├── CLAUDE.md            # "@AGENTS.md" + Claude专属注释
├── .claude/
│   ├── settings.json    # 权限与钩子
│   ├── agents/          # 子代理
│   └── skills/          # 按需加载的过程
├── .cursor/
│   ├── rules/           # .mdc规则,核心规则指向AGENTS.md
│   └── cli.json         # `agent` CLI的权限
├── .cursorignore
└── .codex/
    ├── config.toml      # 沙箱与审批默认值
    └── agents/          # 自定义代理

AGENTS.md包含了每个代理所需的一切内容。其他文件仅保留特定于某个工具的内容:其行为、权限和特性。当你在开发工作流中使用高性能API或通过像 n1n.ai 这样强大的基础设施扩展开发管道时,维护干净、统一的文档文件对于自动化代理获取准确上下文至关重要。

每个工具加载指令的不同方式

这是人们经常搞错的部分,因为这三个工具加载指令的方式各不相同。

  • Codex在每次运行时构建一次指令链。它从全局文件(~/.codex/AGENTS.override.md或AGENTS.md)开始,然后遍历从项目根目录到你当前工作目录的每一个目录。在每个目录中,它会检查AGENTS.override.md、AGENTS.md以及任何project_doc_fallback_filenames,每个目录最多获取一个文件。离你的工作目录更近的文件在后面,因此当指令冲突时它们会胜出。组合后的链具有默认32 KiB的大小上限,达到该限制后Codex将停止添加文件。

  • Cursor支持在根目录和子目录中放置AGENTS.md。嵌套文件与父文件合并,更具体的指令具有更高的优先级。

  • Claude Code则有一个陷阱。它仅在工作目录或上层目录中没有CLAUDE.md或CLAUDE.local.md时才直接读取AGENTS.md。一旦你添加了CLAUDE.md,必须在顶部放置一个导入声明以确保两者都加载:

# CLAUDE.md
@AGENTS.md

一个个人的、未提交的CLAUDE.local.md也算作“存在CLAUDE.md”。因此,即使你从不创建项目级的CLAUDE.md,添加一个本地文件也会静默阻止Claude读取AGENTS.md。保持显式的@AGENTS.md导入可以彻底避免这种情况。

通用AGENTS.md模板

以下是我使用的精简版模板。请将占位符替换为你自己的技术栈。

# AGENTS.md

## Project snapshot
- What it is: <one sentence>
- Main language / runtime: <TypeScript 5, Node 22>
- Package manager: <pnpm>. Do not use any other package manager.
- Layout:
  - `apps/web/`: frontend
  - `apps/api/`: HTTP API (handlers in `apps/api/src/handlers/`)
  - `packages/db/`: schema + migrations

## Commands
- Install: `<pnpm install>`
- Unit tests (fast, run often): `<pnpm test>`
- Single test file: `<pnpm test -- path/to/file.test.ts>`
- Lint + types: `<pnpm lint && pnpm typecheck>`
- Build: `<pnpm build>`

## Conventions
- Named exports, no default exports.
- Errors: return `{ code, message }` objects from services; never throw raw strings.
- New code needs tests next to it (`*.test.ts`).

## Working agreement
1. Restate the task in one or two lines and list the files you expect to touch before editing.
2. Make the smallest change that solves the task. No drive-by refactors.
3. If requirements are ambiguous, stop and ask. Do not guess on behaviour.
4. Never commit secrets, tokens, or `.env*` files.
5. Do not run `git push`, `git reset --hard`, `git clean -fd`, force-pushes,
   or destructive DB commands unless explicitly asked.

## Definition of done
- [ ] Lint + types pass
- [ ] Tests pass (and a new test covers the change)
- [ ] Diff is limited to files related to the task
- [ ] Short summary: what changed, why, how it was verified, open risks

## Code Review Rules
- Flag any handler that reads request input without validation.
- Flag SQL built with string concatenation.

命令放在最前面,因为错误的命令会浪费最多代理轮次:使用npm而不是pnpm,或者使用了错误测试运行器。单文件测试命令也能节省token,因为代理不会在每次迭代时都运行整个测试套件。

工作协议将常见的失败模式(如静默范围膨胀和盲目猜测)转化为显式规则。完成定义为每个代理提供相同的退出标准。如果没有它,你可能会听到代理喊出“完成了!”,而测试实际上依然失败。

## Code Review Rules是Codex的GitHub代码审查会寻找的标题,因此审查指导可以免费附带。在集成企业级LLM工作流或通过 n1n.ai 部署多代理系统时,保持指令标准化可确保每个模型实例都严格遵守安全边界。

保持简短。将格式检查留给CI,而不是规则文件。编写代理可以验证的指令:“在结束前运行pnpm test”比“测试你的更改”效果更好。

工具特定配置

CLAUDE.md:Claude Code项目指令

<!-- Keep this file under ~200 lines. HTML comments are stripped before Claude sees them. -->

@AGENTS.md

## Claude Code specifics
- Use plan mode before touching more than 3 files.
- For searches that would flood the context, delegate to a subagent
  and ask for a summary with file paths.
- Before calling a task done, run the checks in "Definition of done"
  and paste the command output summary.

## Things that went wrong before
- Do not "fix" failing tests by loosening assertions or adding `.skip`.

两个细节:导入是相对于CLAUDE.md的,最多可以嵌套四层。块级HTML注释在注入前会被剥离,因此给人类看的注释不会消耗上下文成本。运行/context以确认实际加载了哪些记忆文件。

Cursor项目规则

Cursor项目规则必须是.cursor/rules/目录下的.mdc文件。那里的纯.md文件会被忽略。常驻核心规则只需指向AGENTS.md并增加一些硬性限制:

---
description: "Core working agreement for every Agent chat in this repo"
alwaysApply: true
---

- Follow `AGENTS.md` at the repo root for commands, layout and definition of done.
- Before editing, list the files you plan to change. Keep the diff limited to the task.
- Never read, print, or edit `.env*`, `*.pem`, `*.key` or credential files.
- Finish every task with: what changed, how you verified it (exact commands), open risks.

特定技术栈的规则放在通过globs附加的独立.mdc文件中,因此只有在上下文中存在匹配文件时它们才会消耗token。

Codex配置

Codex的指令来自AGENTS.md。行为(沙箱、审批、子代理)存放在config.toml中:

approval_policy = "on-request"
sandbox_mode = "workspace-write"
web_search = "cached"

[agents]
max_concurrent_threads_per_session = 4

Codex仅在你信任该项目时才会加载项目的.codex/层。

故障排查与优化

如果代理忽略了你的规范,原因几乎总是规则文件太长、含糊不清、相互矛盾,或者根本没有加载。在重写任何内容之前请先检查:

  • Claude Code: /context与/memory
  • Codex: /status
  • Cursor: Customize → Rules

然后缩短文件,使指令可验证,并消除冲突。单一的规范文件使这变得容易得多,因为只有一个地方需要修改。

总结清单

  1. AGENTS.md是规范源。CLAUDE.md和.cursor/rules保持精简并指向它。
  2. 在CLAUDE.md中,务必添加显式的@AGENTS.md导入。
  3. 将命令放在首位,使用可验证的指令,并为每个代理提供相同的完成定义。
  4. 将工具行为(权限、沙箱、钩子)保存在工具自身的配置中,而不是指令中。

无论你是在编排本地IDE扩展,还是通过 n1n.ai 路由复杂的推理任务,统一核心指令文件都能防止昂贵的token浪费和行为退化。

Get a free API key at n1n.ai