Glean 拾遗
Daily /2026-08-26 / A Complete Guide to AGENTS.md

A Complete Guide to AGENTS.md

Source www.aihero.dev Glean’d 2026-08-26 06:00 Read 8 min
AI summary

AGENTS.md is a Markdown file checked into Git that customizes how AI coding agents behave in a repo. Because the entire file is injected into context on every request, it consumes a finite instruction budget: frontier LLMs reliably follow roughly 150–200 instructions, while smaller models handle fewer. Oversized files also go stale quickly; outdated file paths and architecture notes actively poison agent context, unlike a human who can ignore bad docs. The guide argues for a minimal root AGENTS.md containing only a one-sentence project description, the package manager when it isn't npm, and any non-standard build/typecheck commands. Everything else should be progressively disclosed through linked markdown files, nested AGENTS.md files for monorepo packages, or agent skills. It also covers symlinking AGENTS.md to CLAUDE.md for Claude Code, and includes a copy-paste prompt to refactor an existing bloated file. Practical for teams running agentic coding tools who want to reduce token waste and document drift.

Original · 8 min
www.aihero.dev ↗
§ 1

Have you ever felt concerned about the size of your AGENTS.md file?

Maybe you should be. A bad AGENTS.md file can confuse your agent, become a maintenance nightmare, and cost you tokens on every request.

So you'd better know how to fix it.

你是否曾担心过你的 AGENTS.md 文件太大?

也许你确实该担心。一份糟糕的 AGENTS.md 会混淆你的 agent,变成维护噩梦,并且每次请求都消耗 token。

所以你最好知道怎么修复它。

§ 2

An AGENTS.md file is a markdown file you check into Git that customizes how AI coding agents behave in your repository. It sits at the top of the conversation history, right below the system prompt.

Think of it as a configuration layer between the agent's base instructions and your actual codebase. The file can contain two types of guidance:

Personal scope: Your commit style preferences, coding patterns you prefer

Project scope: What the project does, which package manager you use, your architecture decisions

AGENTS.md 是一个提交到 Git 的 markdown 文件,用来定制 AI 编程 agent 在仓库中的行为。它位于对话历史的顶部,就在系统提示词正下方。

可以把它理解为 agent 基础指令与实际代码库之间的配置层。文件可以包含两类指南:

个人范围:你偏好的提交风格、编码模式

项目范围:项目做什么、使用哪个包管理器、架构决策

§ 3

The AGENTS.md file is an open standard supported by many - though not all - tools.

CLAUDE.md

Notably, Claude Code doesn't use AGENTS.md - it uses CLAUDE.md instead. You can symlink between them to keep all your tools working the same way:

# Create a symlink from AGENTS.md to CLAUDE.md
ln -s AGENTS.md CLAUDE.md

AGENTS.md 是一个开放标准,许多工具(虽然不是全部)都支持它。

CLAUDE.md

值得注意的是,Claude Code 不使用 AGENTS.md,而是使用 CLAUDE.md。你可以在两者之间创建符号链接,让所有工具以同样的方式工作:

# Create a symlink from AGENTS.md to CLAUDE.md
ln -s AGENTS.md CLAUDE.md
§ 4

There's a natural feedback loop that causes AGENTS.md files to grow dangerously large:

The agent does something you don't like

You add a rule to prevent it

Repeat hundreds of times over months

File becomes a "ball of mud"

Different developers add conflicting opinions. Nobody does a full style pass. The result? An unmaintainable mess that actually hurts agent performance.

Another culprit: auto-generated AGENTS.md files. Never use initialization scripts to auto-generate your AGENTS.md. They flood the file with things that are "useful for most scenarios" but would be better progressively disclosed. Generated files prioritize comprehensiveness over restraint.

AGENTS.md 的膨胀存在一个自然的反馈循环,会让文件危险地变大:

agent 做了你不喜欢的事

你添加一条规则来阻止

几个月内重复数百次

文件变成一坨「泥球」

不同开发者添加互相矛盾的意见,没有人做一次完整的风格梳理。结果是什么?一个难以维护的烂摊子,反而会损害 agent 的表现。

另一个罪魁祸首:自动生成的 AGENTS.md。绝不要用初始化脚本自动生成 AGENTS.md。它们会把「在大多数场景下有用」的内容塞满文件,而这些内容更适合渐进式披露。自动生成的文件把全面性置于克制之上。

§ 5

Kyle from Humanlayer's article mentions the concept of an "instruction budget":

Frontier thinking LLMs can follow ~ 150-200 instructions with reasonable consistency. Smaller models can attend to fewer instructions than larger models, and non-thinking models can attend to fewer instructions than thinking models.

Every token in your AGENTS.md file gets loaded on every single request, regardless of whether it's relevant. This creates a hard budget problem:

Taken together, this means that the ideal AGENTS.md file should be as small as possible.

Humanlayer 的 Kyle 在文章中提到了「指令预算」这一概念:

前沿思考型 LLM 可以稳定地遵循大约 150–200 条指令。较小的模型能关注的指令数量少于较大的模型,非思考模型能关注的指令数量少于思考模型。

AGENTS.md 中的每个 token 在每次请求时都会被加载,无论它是否相关。这就构成了一个硬性的预算问题:

综合来看,理想的 AGENTS.md 应该尽量小。

§ 6

Another issue for large AGENTS.md files is staleness.

Documentation goes out of date quickly. For human developers, stale docs are annoying, but the human usually has enough built-in memory to be skeptical about bad docs. For AI agents that read documentation on every request, stale information actively poisons the context.

This is especially dangerous when you document file system structure. File paths change constantly. If your AGENTS.md says "authentication logic lives in src/auth/handlers.ts" and that file gets renamed or moved, the agent will confidently look in the wrong place.

Instead of documenting structure, describe capabilities. Give hints about where things might be and the overall shape of the project. Let the agent generate its own just-in-time documentation during planning.

Domain concepts (like "organization" vs "group" vs "workspace") are more stable than file paths, so they're safer to document. But even these can drift in fast-moving AI-assisted codebases. Keep a light touch.

AGENTS.md 的另一个问题是陈旧。

文档很快就会过时。对人类开发者来说,过时的文档很烦人,但人类通常有足够的内建记忆,会对烂文档持怀疑态度。而每次请求都会读文档的 AI agent,过时信息会实实在在地污染上下文。

当你记录文件系统结构时,这尤其危险。文件路径不断变化。如果你的 AGENTS.md 写着「认证逻辑位于 src/auth/handlers.ts」,而该文件被重命名或移动,agent 会自信地找错地方。

与其记录结构,不如描述能力。给出关于内容可能在哪里的提示,以及项目的整体形态,让 agent 在规划时自己生成即时文档。

领域概念(如 organization、group、workspace 的区别)比文件路径稳定得多,因此记录它们更安全。但即使在快速演进的 AI 辅助代码库中,这些概念也可能漂移。保持轻量。

§ 7

Be ruthless about what goes here. Consider this the absolute minimum:

One-sentence project description (acts like a role-based prompt)

Package manager (if not npm; or use corepack for warnings)

Build/typecheck commands (if non-standard)

That's honestly it. Everything else should go elsewhere.

The One-Liner Project Description

This single sentence gives the agent context about why they're working in this repository. It anchors every decision they make.

Example:

This is a React component library for accessible data visualization.

That's the foundation. The agent now understands its scope.

Package Manager Specification

If you're In a JavaScript project and using anything other than npm, tell the agent explicitly:

This project uses pnpm workspaces.

Without this, the agent might default to npm and generate incorrect commands.

Corepack is also great You could also use corepack to let the system handle warnings automatically, saving you precious instruction budget.

对于该放什么,要毫不留情。把这当作绝对底线:

一句话项目描述(作用类似基于角色的 prompt)

包管理器(如果不是 npm;或用 corepack 处理警告)

构建/typecheck 命令(若非标准)

真的就这些。其他一切都要放到别处。

一句话项目描述

这一句话给 agent 提供了「为什么在这个仓库里工作」的上下文,并锚定它所做的每个决定。

示例:

这是一个用于无障碍数据可视化的 React 组件库。

这就是基础。agent 现在明白了自己的工作范围。

包管理器说明

如果你在 JavaScript 项目中使用的是 npm 以外的包管理器,明确告诉 agent:

本项目使用 pnpm workspaces。

否则,agent 可能会默认使用 npm 并生成错误的命令。

Corepack 也很好 你也可以用 corepack 让系统自动处理警告,从而省下宝贵的指令预算。

§ 8

Use Progressive Disclosure

Instead of cramming everything into AGENTS.md, use progressive disclosure: give the agent only what it needs right now, and point it to other resources when needed.

Agents are fast at navigating documentation hierarchies. They understand context well enough to find what they need.

Move Language-Specific Rules to Separate Files

If your AGENTS.md currently says:

Always use const instead of let.

Use interface instead of type when possible.

Move that to a separate file instead. In your root AGENTS.md:

For TypeScript conventions, see docs/TYPESCRIPT.md

Notice the light touch, no "always," no all-caps forcing. Just a conversational reference.

The benefits:

TypeScript rules only load when the agent writes TypeScript

Other tasks (CSS debugging, dependency management) don't waste tokens

File stays focused and portable across model changes

渐进式披露

不要把一切都塞进 AGENTS.md,而是采用渐进式披露:只给 agent 当下需要的东西,需要时再指向其他资源。

agent 在文档层级中导航很快,它们对上下文的理解足以找到需要的内容。

把语言专属规则移到单独文件

如果你的 AGENTS.md 现在写着:

始终使用 const 而不是 let。

尽可能使用 interface 而不是 type。

把它们移到单独文件。在根 AGENTS.md 中写:

TypeScript 约定请见 docs/TYPESCRIPT.md

注意这种轻触:没有「always」,没有全大写强调,只是一句对话式的参考。

好处:

TypeScript 规则只在 agent 写 TypeScript 时加载

其他任务(CSS 调试、依赖管理)不会浪费 tokens

文件保持专注,并且跨模型更换时依然可移植

§ 9

Nest Progressive Disclosure

You can go even deeper. Your docs/TYPESCRIPT.md can reference docs/TESTING.md. Create a discoverable resource tree:

│ └── references TESTING.md

│ └── references specific test runners

└── references esbuild configuration

You can even link to external resources, Prisma docs, Next.js docs, etc. The agent will navigate these hierarchies efficiently.

Use Agent Skills

Many tools support "agent skills" - commands or workflows the agent can invoke to learn how to do something specific. These are another form of progressive disclosure: the agent pulls in knowledge only when needed.

We'll cover agent skills in-depth in a separate article.

AI Hero · Skill System

A great AGENTS.md is step one

See the skills I run on top of mine to turn that file into shipped work.

See the skill set

嵌套渐进式披露

你还可以更进一步。docs/TYPESCRIPT.md 可以引用 docs/TESTING.md。构建一棵可发现的资源树:

│ └── references TESTING.md

│ └── references specific test runners

└── references esbuild configuration

你甚至可以链接到外部资源,如 Prisma 文档、Next.js 文档等。agent 会高效地浏览这些层级。

使用 Agent Skills

许多工具支持「agent skills」——agent 可以调用的命令或工作流,用来学习如何做某件特定的事。这是渐进式披露的另一种形式:agent 只在需要时拉取知识。

我们会在另一篇文章中深入介绍 agent skills。

AI Hero · Skill System

一份好的 AGENTS.md 是第一步

看看我在其之上运行的技能,如何把这个文件变成已交付的工作。

查看技能集

§ 10

You're not limited to a single AGENTS.md at the root. You can place AGENTS.md files in subdirectories, and they merge with the root level.

This is powerful for monorepos:

What Goes Where

Root AGENTS.md:

This is a monorepo containing web services and CLI tools.

Use pnpm workspaces to manage dependencies.

See each package's AGENTS.md for specific guidelines.

Package-level AGENTS.md (in packages/api/AGENTS.md):

This package is a Node.js GraphQL API using Prisma.

Follow docs/API_CONVENTIONS.md for API design patterns.

Don't overload any level. The agent sees all merged AGENTS.md files in its context. Keep each level focused on what's relevant at that scope.

你并不局限于根目录下单一的一个 AGENTS.md。你可以在子目录里也放 AGENTS.md,它们会与根级别的文件合并。

这对 monorepo 非常有用:

什么放哪里

AGENTS.md

这是一个包含 Web 服务和 CLI 工具的 monorepo。

使用 pnpm workspaces 管理依赖。

各 package 的具体指南参见各自的 AGENTS.md

包级 AGENTS.md(位于 packages/api/AGENTS.md):

这个 package 是一个使用 Prisma 的 Node.js GraphQL API。

API 设计模式请遵循 docs/API_CONVENTIONS.md。

不要在任何一层过度堆砌。agent 会在上下文中看到所有合并后的 AGENTS.md 文件。让每一层专注于该范围内相关的内容。

§ 11

If you're starting to get nervous about the AGENTS.md file in your repo, and you want to refactor it to use progressive disclosure, try copy-pasting this prompt into your coding agent:

I want you to refactor my AGENTS.md file to follow progressive disclosure principles.

  1. Find contradictions: Identify any instructions that conflict with each other. For each contradiction, ask me which version I want to keep.

  2. Identify the essentials: Extract only what belongs in the root AGENTS.md:

  • One-sentence project description

  • Package manager (if not npm)

  • Non-standard build/typecheck commands

  • Anything truly relevant to every single task

  1. Group the rest: Organize remaining instructions into logical categories (e.g., TypeScript conventions, testing patterns, API design, Git workflow). For each group, create a separate markdown file.

  2. Create the file structure: Output:

  • A minimal root AGENTS.md with markdown links to the separate files

  • Each separate file with its relevant instructions

  • A suggested docs/ folder structure

  1. Flag for deletion: Identify any instructions that are:
  • Redundant (the agent already knows this)

  • Too vague to be actionable

  • Overly obvious (like "write clean code")

When you're about to add something to your AGENTS.md, ask yourself where it belongs:

The ideal AGENTS.md is small, focused, and points elsewhere. It gives the agent just enough context to start working, with breadcrumbs to more detailed guidance.

Everything else lives in progressive disclosure: separate files, nested AGENTS.md files, or skills.

This keeps your instruction budget efficient, your agent focused, and your setup future-proof as tools and best practices evolve.

如果你开始对仓库里的 AGENTS.md 感到不安,想用渐进式披露重构它,可以试试把这个提示词复制粘贴到你的 coding agent 中:

我要你重构我的 AGENTS.md,让它遵循渐进式披露原则。

  1. 找出矛盾:识别所有互相冲突的指令。对每一条矛盾,问我保留哪个版本。

  2. 找出要点:只提取属于根 AGENTS.md 的内容:

  • 一句话项目描述

  • 包管理器(如果不是 npm)

  • 非标准的构建/typecheck 命令

  • 与每个任务都真正相关的内容

  1. 归拢其余部分:把剩余指令按逻辑分类(如 TypeScript 约定、测试模式、API 设计、Git 工作流)。为每个分类创建单独的 markdown 文件。

  2. 创建文件结构:输出:

  • 一个极简的根 AGENTS.md,用 markdown 链接指向各个独立文件

  • 每个独立文件及其相关指令

  • 一个建议的 docs/ 目录结构

  1. 标记删除项:识别这些指令:
  • 冗余的(agent 已经知道)

  • 太模糊而无法执行

  • 过于显而易见的(比如「写干净代码」)

当你准备往 AGENTS.md 里加东西时,问问自己它该放在哪里:

理想的 AGENTS.md 小而聚焦,并指向其他地方。它给 agent 足够的上下文开始工作,同时留下通向更详细指南的面包屑。

其余一切都属于渐进式披露:独立文件、嵌套的 AGENTS.md 或 skills。

这样能让你的指令预算高效、agent 保持专注,也让你的配置在工具和最佳实践演进时依然面向未来。

Open source ↗