跳转到内容

writing-for-agents

供代理使用的文档写作参考:适用于编写 skill、AGENTS.md / CLAUDE.md,以及修改这些文件或它们指向的文档。打包方式不同,写法相同:每次运行时,代理执行的是同一套流程,而不是产出同一种结果,因此同样的写作手段能让流程更可预测。

如果你要写的是 skill,请阅读 SKILL-MECHANICS.md,了解 frontmatter、调用方式和路由 skill。

上下文指针是代理上下文中的一条引用。它会指出上下文之外的材料,并说明什么情况下要去读取。skill 的 description 是一种上下文指针;AGENTS.md 中指向某份文档的一行也是。

决定代理何时读取材料、读取得有多可靠的,是指针的措辞,而不是目标文件。必需材料藏在措辞含糊的指针后面,会造成运行差异。先把指针写明确;只有明确措辞仍不够时,才把材料放进正文。

指针有两个作用:说明材料是什么,并列出哪些分支需要读取它。分支指文档处理的不同情况,不同运行会走不同路径。

  • 把最关键的触发词放在前面,让指针一开始就能触发读取。
  • 每个分支只写一个触发条件。用同义词重复描述同一个分支,相当于把一个分支写了两遍;删掉重复部分,只留下真正不同的分支。
  • 删掉正文已经说明的身份信息。

常驻加载的指针每一轮都会消耗上下文,所以更要精简。

你添加的每份文档和每个指针,都会消耗两种负载之一:

  • 上下文负载:代理每一轮都要加载的材料,占用上下文窗口和注意力,无论材料是否会触发。
  • 认知负载:人要记住有哪些文档,以及何时该去读哪份。人是索引。这不该被当作需要尽量压低的成本:这是人自主判断所付出的代价,只应在人类判断有价值的地方承担。

只有通过指针才能读到的材料,不占常驻上下文负载,但指针本身会占负载。没有指针的材料则完全依赖人的认知负载。

文档包含两类内容:步骤,也就是代理按顺序执行的操作;以及参考资料,也就是按需查阅的定义、规则和事实。这两类内容可以自由组合:文档可以是一套流程、一个评审规则集,也可以两者都有。

关键是决定每部分在信息层级中处于哪一层。层级从最需要立即看到的内容开始:

  1. 文件内步骤是首要层级:代理按顺序执行的操作。
  2. 文件内参考资料供按需查阅。它们通常是同一层级的一组规则,这种平铺结构很合理,不是问题。
  3. 显式指向的参考资料被放在单独文件中,通过上下文指针读取。它可以是同目录下的配套文件,也可以是外部任何位置的参考资料,供其他文档指向。

把太多内容留在上层,正文就会臃肿;把太多内容移到下层,代理又会找不到真正需要的信息。这就是整个设计要权衡的地方。

渐进式披露是沿层级向下移动内容,把材料放到主文件之外,通过指针读取,让正文保持清晰。它主要不是为了省 token,而是为了维护信息层级。

判断是否该披露内容,最清楚的方法是看分支:每个分支都需要的内容放在正文里;只有部分分支会用到的内容,放在指针后面。文档包含步骤时,把本应披露的参考资料留在正文里,会掩盖步骤,让代理是否留意这些内容变成碰运气。这会增加运行差异,也不只是降低可读性。

就近组织决定同一层级里哪些内容该放在一起;信息层级则决定内容应该放在哪一层。把概念定义、规则和注意事项放在同一个标题下,不要拆散。这样,读一部分时也能看到相关内容。文档应该像是写给代理使用的文档:分组清晰就有这种效果,内容散落各处则没有。

这和重复不同:重复是在多处表达同一个意思;散落则是把一个意思拆成多块,分布在不同位置。

内容蔓延是这里的一种失败模式:文档变得太长,即使每一行都有用,也会让注意力越来越分散。每多一行,代理就多一项需要保持相关的内容。解决方法是利用层级:通过指针披露参考资料,并按分支或顺序拆分内容,让每条路径只携带它需要的部分。

每个步骤都要有一个完成标准,告诉代理何时算做完。两个特征能让标准真正起作用:

  • 清晰度:代理能否分辨完成与否?「达到理解」这类模糊标准会导致过早完成:步骤还没真正做完,代理就结束了,注意力开始转向「赶快做完」。标准越清楚,抵抗这种倾向的力量越强。

    按这个顺序处理:先把完成界限说清楚,这是局部且成本低的调整。只有当界限确实无法说清、而且你观察到代理在赶进度时,才通过拆分流程来隐藏后续步骤。隐藏内容只有跨越真实的上下文边界才有效,例如交接或派发给子代理;在同一上下文里的调用仍会让后续步骤留在视野中,无法清除干扰。

  • 要求力度:标准要求代理做到什么程度。「每个修改过的模型都已处理」要求代理彻底完成工作;「列出变更」则没有这样的要求。要求力度会驱动调查工作:代理在工作中需要深挖的内容,这些要求往往隐含在措辞里,不会单独列成步骤。

    要求力度不限于步骤:「应用了每条规则」可以约束一组平铺的参考资料,就像「完成了每个步骤」约束一套流程。因此,即使全篇都是参考资料,也能要求代理逐项落实。

最有力的标准既能检查,也能覆盖所有要求。

把一份文档拆成两份,会消耗两种负载之一,所以只有拆分确实有价值时才做:

  • 按顺序拆分:拆开一串步骤,避免代理被当前步骤之后的内容催着赶进度。把后续步骤移出视野,有助于代理更深入地完成当前任务。也要留意相反的风险:把不同流程合并,会让每一步都暴露在后续步骤面前,诱发过早完成。
  • 按调用方式拆分:这是 skill 特有的情况,见 SKILL-MECHANICS.md。

开头词是模型预训练中已有的简洁概念,代理执行文档时会用它来思考,例如 lesson、fog of war、tracer bullets。把它当作词语反复使用,而不是句子,可以逐步形成一套分散在文档中的定义,也能用更少的 token 锚定一整片行为模式,因为模型已有相关认知。

它有两种锚定作用。在正文里,它影响执行:代理每次看到这个词,都会想到同一种行为;在一组平铺的参考资料里,它能聚焦代理要寻找的内容。在指针里,它影响调用:当同一个词出现在提示词、文档和代码库中,代理会把这些共同用语与相关材料联系起来,更可靠地读取它。

要寻找使用开头词重构内容的机会。比如同一组三项内容在三个地方都被逐条写出,或者指针花一整句话来表达一个意思。这些地方都可以收束成一个词:

  • 「快速、确定、开销低」→ 紧凑,例如「紧凑的循环」。
  • 「一个你信得过的循环」→ 红色,把模糊的关卡变成可以观察的二元状态:循环会因 bug 变红,也可能不会。

这样能同时减少 token,并给代理一个更清晰的思考抓手。假设每份文档都包含一些反复表达、可以用开头词替代的内容,然后逐一找出来。

否定表达是这个写作手段旁边的一种失败模式:用禁止来引导行为,会把被禁止的行为带进上下文,让它更容易被想起。「不要想大象」,脑中就只剩大象。否定词只是很弱的修饰语,很容易被强烈激活的概念盖过,所以禁令可能被读成是在要求执行那件事。

要正面描述目标行为,例如「写一行注释」,让被禁止的行为根本不会被提起。只有当某条硬性限制无法用正面措辞表达时,禁止才值得保留;即使如此,也要同时写出正面的目标,让注意力落在该做什么上。

  • 每个意思都只保留一个唯一事实来源:指定一个权威位置,让行为变更只需修改一处。重复会增加维护成本和 token,也会让某个意思在信息层级中的显著程度超过它应有的程度。(开头词是有意重复词语,而不重复意思,恰好相反。)
  • 环境也是事实来源,例如 package.json 脚本、配置文件、目录布局和 --help 输出。重复描述环境内容的文档就是一份缓存:只有查找成本高时,缓存才值得保留。只缓存代理无法通过查找得知的内容,例如未写明的惯例、选择背后的原因、配置文件没有记录的陷阱。只需查看一个文件或运行一个命令就能查到的信息,应留在环境里,避免文档过时。
  • 逐行检查内容是否相关:它是否仍然服务于文档的用途?如果内容从未与任务相关,例如只是背景说明或本该披露的分支内容;或者它描述的行为或情况已经变化,这一行就失去了相关性。文档越短,就越容易保持相关。如果没有精简的习惯,默认结果就是沉积:过时内容层层堆积,因为添加让人安心,删除却让人担心,最后只能一层层挖开,才能找到仍然有效的内容。
  • 逐句寻找无效指令:模型本来就会照做的指令,却额外占了负载。判断方法是看它是否改变模型的默认行为。若没有,就直接删掉整句,不要只删几个词。这个判断取决于模型的默认行为,不取决于读者的默认行为:两个人若对某句是不是无效指令看法不同,分歧就在于他们对模型默认行为的判断不同,应通过实际运行文档来解决。

开头词也要按这个标准检查:如果一个词不足以改变默认行为,例如模型本来就会做到一些程度的「认真」,那它就是无效指令。解决办法是换成力度更强的词,例如「全力以赴」,而不是改用另一种写法。

读者可以点名用它,模型也可以自己调用它。它在 Claude Code 插件里。

这份参考文档讲解如何写出让代理每次都能按同一流程执行的文档,涵盖上下文指针、信息层级、步骤标准、开头词和精简方法。

Writing documents for agents. Use when creating or editing skills, or modifying AGENTS.md or CLAUDE.md.
英文 SKILL.md
---
name: writing-for-agents
description: Writing documents for agents. Use when creating or editing skills, or modifying AGENTS.md or CLAUDE.md.
---
Reference for writing any document an agent consumes: a skill, an `AGENTS.md` / `CLAUDE.md`, a doc reached by a pointer. The packaging differs; the writing does not: the same levers make each one predictable, since the agent takes the same _process_ every run rather than producing the same output.
When the document you're writing is a skill, read [`SKILL-MECHANICS.md`](SKILL-MECHANICS.md) for frontmatter, invocation choice, and router skills.
## Context pointers
A **context pointer** is a reference held in the agent's context that names some out-of-context material and encodes the condition for reaching it. A skill's description is one; a line in `AGENTS.md` naming a doc is the same object. The pointer's _wording_, not its target, decides when the agent reaches the material, and how reliably. A must-have target behind a weakly worded pointer is a variance bug: sharpen the wording first, and inline the material only if sharpening fails.
A pointer does two jobs: state what the material is, and list the **branches** that should trigger reaching it (a branch is a distinct case the document handles, so different runs take different paths through it). Every word of an always-loaded pointer costs on every turn, so it earns even harder pruning than the body:
- **Front-load the leading word**: the pointer is where it does its triggering work.
- **One trigger per branch.** Synonyms that rename a single branch are one branch written twice; collapse them and keep only genuinely distinct branches.
- **Cut identity the body already carries.**
## The two loads
Every document and pointer you add spends one of two budgets:
- **Context load** is the cost of always-loaded material on the agent's window: an `AGENTS.md` line, a skill description, anything sitting in context every turn, spending tokens and attention whether or not it fires.
- **Cognitive load** is the cost on the human: which documents exist and when to reach for each. The human is the index. Not a cost to minimise: it is the price of human agency; spend it where human judgement matters, remove it where it does not.
Material reached only through a pointer escapes context load at the price of the pointer's own line; material with no pointer at all rides entirely on cognitive load.
## Information hierarchy
A document is built from two content types: **steps** (the ordered actions the agent performs) and **reference** (definitions, rules, facts consulted on demand). The two mix freely: all steps (a recipe), all reference (a review's rules, this skill), or both. The core decision is where each piece sits on the **information hierarchy**, a ladder ranked by how immediately the agent needs the material:
1. **In-file step** is the primary tier: what the agent does, in order.
2. **In-file reference** is consulted on demand. Often a legitimately flat peer-set (every rule of a review on one rung), which is a fine arrangement, not a smell.
3. **Disclosed reference** is pushed out into a separate file, reached by a context pointer, loaded only when the pointer fires. Spans a sibling file in the same folder through fully external reference that lives anywhere and any document can point at.
Push too little down and the top bloats; push too much and you hide material the agent actually needs. That tension is the whole decision.
**Progressive disclosure** is the move down the ladder (out of the main file and behind a pointer) so the top stays legible. Not primarily a token optimisation: it is how the hierarchy is protected. Branching is the cleanest disclosure test: inline what every branch needs, and push behind a pointer what only some branches reach. When a document has steps, in-file reference that should be disclosed buries them and turns attending to them into a coin-flip: a variance lever, not just a legibility one.
**Co-location** is the within-file companion: where the ladder decides _how far down_ a piece sits, co-location decides _what sits beside it_ once there. Keep a concept's definition, rules, and caveats under one heading rather than scattered, so reading one part brings its neighbours with it. The test: the document should read like documentation written for the agent. Grouped material reads that way; scattered material does not. (Distinct from duplication: that repeats one meaning in two places; scattering fragments one meaning across many.)
**Sprawl** is the failure mode here: a document simply too long, even when every line is live and unique. Attention thins across the excess, and every extra line is one more to keep relevant. The cure is the ladder: disclose reference behind pointers, and split by branch or sequence so each path carries only what it needs.
## Steps and completion criteria
Every step ends on a **completion criterion**, the condition that tells the agent the work is done. Two properties make it a lever:
- **Clarity**: can the agent tell done from not-done? A vague bound ("understanding reached") invites **premature completion**: ending the step before it is genuinely done, attention slipping to _being done_. The visible steps still ahead (the **post-completion steps**) supply the pull; the criterion's clarity is the resistance. Defend in order: **sharpen the bound first** (local and cheap); only if it is irreducibly fuzzy _and_ you observe the rush, hide the later steps by splitting the sequence. Hiding only works across a real context boundary (a hand-off or a subagent dispatch; an inline call leaves the later steps in context and clears nothing).
- **Demand**: how much it requires. "Every modified model accounted for" forces thorough work where "produce a change list" does not. Demand drives **legwork** (the digging the agent does within the work, latent in the wording rather than written as its own step), and it is not step-bound: "every rule applied" binds a body of flat reference just as "every step done" binds a sequence, which is how an all-reference document still carries an exhaustiveness bar.
The strongest criteria are both checkable and exhaustive.
## When to split
Splitting one document into two spends one of the two loads, so split only when the cut earns it:
- **By sequence**: split a run of steps where the post-completion steps tempt the agent to rush the one in front of it. Keeping them out of view drives more legwork on the current task. Beware the reverse: merging sequences exposes each step's later steps to what follows, inviting premature completion.
- **By invocation**, skill-specific: see [`SKILL-MECHANICS.md`](SKILL-MECHANICS.md).
## Leading words
A **leading word** is a compact concept already living in the model's pretraining that the agent thinks with while running the document (_lesson_, _fog of war_, _tracer bullets_). Repeated as a token, never as a sentence, it accumulates a distributed definition and anchors a whole region of behaviour in the fewest tokens, by recruiting priors the model already holds. Coining your own works if you define it clearly, but a made-up word recruits no priors: you pay in definition tokens what a pretrained word gives free; reach for an existing word first.
It anchors twice. In the body, _execution_: the agent reaches for the same behaviour every time the word appears, and inside flat reference it focuses attention on a class of thing to look for. In a pointer, _invocation_: when the same word lives in your prompts, your docs, and your codebase, the agent links that shared language to the material and reaches it more reliably.
Hunt for opportunities to refactor with leading words. A triad spelled out at three sites, a pointer spending a sentence to gesture at one idea. Each is a passage begging to collapse into a single token:
- "fast, deterministic, low-overhead" → _tight_ (a _tight_ loop).
- "a loop you believe in" → _red_, turning a fuzzy gate into a binary observable state (the loop goes _red_ on the bug, or it doesn't).
You win twice: fewer tokens, and a sharper hook for the agent to hang its thinking on. Assume every document is carrying restatements that leading words retire. Go find them.
**Negation** is the failure mode beside this lever: steering by prohibition drags the forbidden behaviour into context and makes it _more_ available, not less. _Don't think of an elephant_, and the elephant is all there is; the negation is a weak modifier the strongly-activated concept overruns, so the ban half-reads as an instruction to do the thing. Prompt the **positive**: state the target behaviour ("write one-line comments") so the banned one is never spoken. A prohibition earns its place only as a hard guardrail you cannot phrase positively; even then, pair it with the positive target so attention lands on what to do.
## Pruning
- Keep each meaning in a **single source of truth**: one authoritative place, so changing the behaviour is a one-place edit. **Duplication** (the same meaning in more than one place) costs maintenance and tokens, and inflates a meaning's prominence on the ladder past its real rank. (The accidental inverse of a leading word, which repeats a token on purpose, never the meaning.)
- The **environment** is a source of truth too (`package.json` scripts, config files, the directory layout, `--help` output), and a document that restates it is a **cache**: a copy of a lookup, earning its load only when the lookup is expensive. Cache what the agent cannot find by looking: the unwritten convention, the reason behind a choice, the gotcha no config confesses. Leave the one-file, one-command lookups to the environment, where they cannot go stale.
- Check every line for **relevance**: does it still bear on what the document does? A line loses relevance by never bearing on the task (mere exposition, or a branch that should be disclosed) or by going stale as the behaviour or world it describes changes. Shorter documents are easier to keep relevant. Without a pruning discipline the default fate is **sediment**: stale layers that settle because adding feels safe and removing feels risky, until you must core down through them to find what is still live.
- Hunt **no-ops** sentence by sentence: an instruction the model already obeys by default pays load to say nothing. The test (does it change behaviour versus the default?) is model-relative, not reader-relative: two people disagreeing about a no-op disagree about the default, and settle it by running the document, not by debate. When a sentence fails, delete the whole sentence rather than trim words from it. The test also grades leading words: a word too weak to beat the default (_be thorough_ when the agent is already thorough-ish) is a no-op, and the fix is a stronger word (_relentless_), not a different technique.

非官方学习站,与 Matt Pocock 和 AI Hero 没有隶属关系。本站不提供可安装的中文技能包。

技能原文采用 MIT 许可。Copyright (c) 2026 Matt Pocock

鲁ICP备2025189036号