从Anthropic官方文档学习Claude Code的全局内存配置
機械翻訳 / Machine-translated

機械翻訳 / Machine-translated
你有没有凭自己的经验往 ~/.claude/CLAUDE.md 里随意填写内容?我以前就是这样的。
Anthropic在官方文档中,针对模型实际出现的问题症状,提供了可以"直接粘贴到提示词中"的英文语句。比起凭感觉自己写,直接照搬精准度更高。
先说结论,列出我添加的内容。我的全局内存今天从 58行增加到了120行。
| # | 添加的规则 | 对应的官方章节 |
|---|---|---|
| 3 | 能直接表达的内容,不用比喻替代 | 文章密度(mannered prose) |
| 4 | 对已受托的任务,不要请求许可 | 完成整个任务 |
| 5 | 将修改和测试限定在任务要求的范围内 | 同名章节 |
| 6 | 小改动时,不重写整个文件 | 优先进行精准编辑 |
| 7 | 越是熟悉的名称,越要搜索后再作答 | 低努力搜索的触发条件 |
这些内容并非我自己想出来的。全部都是直接照搬官方文档中的英文语句。下面逐一说明每条语句是为了纠正什么问题。
Claude Code的指令文件分为两个层级:
~/.claude/CLAUDE.md … 在所有项目、所有会话中加载(全局)CLAUDE.md … 仅在该仓库中加载写在全局端的内容,无论在哪个项目中发出什么请求,都会出现在对话开头。"每次都说了,每次还是这样"——这类问题的存放位置,非这里莫属。
Anthropic的Claude Fable 5.1提示词指南与其他文档的写法不同。它先列举模型实际出现的问题症状,然后针对每个症状说明"把这段英文粘贴到提示词中"。
| 症状 | 官方对策 |
|---|---|
| 文章冗长、密度高 | 定义并禁止 mannered prose(矫揉造作的文章) |
| 任务未完成就结束轮次,或询问"要继续吗?" | 声明自主运行+对最后一段进行自我检查 |
| 未经请求的修改或测试被提交 | 要求将修改和测试限定在任务范围内 |
| 小改动导致整个文件被重写 | 要求优先进行精准编辑 |
| 凭记忆作答而不搜索 | 告知"知道名称"和"了解现状"是两回事 |
这些都不是项目特有的问题,而是模型本身的特性问题。因此有必要放在全局端。
官方定义的反模式值得直接引用:
Mannered prose substitutes metaphor and flourish for direct statement. Instead of "a parameter worth varying," the mannered writer produces "a dial worth turning." (…) The fix is to say what you mean. When a literal phrase is available, use it.
明明写"值得调整的参数"就够了,却写成"值得拨动的旋钮"。这种表达方式的存在,不是为了传递内容,而是为了展示写作者自身。
官方也提供了简短版本:Please remove all mannered prose. 仅此一句也有效果。
在执行较长任务的中途,模型停下来问"要应用这个吗?",用户只能不停地输入"继续"。
官方的对策,关键在于开头的第一句话:
You are operating autonomously. The user is not watching in real time and cannot answer questions mid-task…
文档中写道:"告知模型用户并未在旁观的这句开头,承担了大部分效果。请按原文保留。"也就是说,如果摘要缩写,效果就会打折。
但需要注意,这句话在边对话边操作时与事实不符。我将其适用范围明确限定为"无人值守执行(cron或 claude -p)时"。官方本身也注明"请确认在模糊请求时不再提问的权衡取舍"。
在对话场景下同样有效的,是同一章节后半部分的自我检查:在结束轮次之前,检查最后一段。如果它是计划、分析、问题、下一步列表或"我将会……"这样的承诺,那就立即去做。
在作业过程中即使发现现有的bug或性能问题,也不在本次修改中修复,仅在摘要中作为后续事项报告。测试只在任务有明确要求,或仓库中同类修改本就附有测试的情况下才提交。
官方对此指示的说明是:"未经请求的添加和被提交的测试代码大幅减少,任务成功率没有可测量的变化。"减少的只是多余的部分,工作完成率不受影响。
不重写整个文件。 每次小改动都重写全文,只会增加输出token和时间。官方语句只有两句话:
The number of tokens used to edit files is best minimized, all else being equal. Therefore, when it will not affect the end result, try to surgically edit a file rather than rewrite the entire thing.
越是熟悉的名称,越要搜索后再作答。 AI模型名称和工具名称,几个月内情况就会发生变化。官方指出的原因颇为犀利:对某事一知半解,恰恰会让过时的答案听起来充满权威。
partial background is exactly what makes an out-of-date answer sound authoritative, so familiarity is not a reason to skip the search.

我认为这是最容易被忽视的地方。
官方文档中有一处指示的是删除而非添加:
旧版模型在作业中会积极输出更新,因此系统提示中会出现"所有发现事项须保留至最终回复"之类的内容。在添加任何内容之前,请先删除此类内容。
格式方面也是同样的道理。旧版模型过度使用项目符号和粗体,所以很多人写了抑制规则。而现在的模型反而连标题和列表都用得太少。如果抑制规则还留着,就会进一步削减本就不足的内容。
针对旧版模型写的内容,对现在的模型可能产生反效果。以为在完善内存,实际上却在抵消模型的改进。
与其看增加了多少行,不如看删掉了多少行。
我的全局内存以前有两条规则:
这两条都是我针对AI"不写实体内容就敷衍了事"的问题自己写的。
今天添加第3条mannered prose规则时,发现它与第1条中的"不用比喻称呼"内容重叠。若重叠部分放任不管,当其中一条过时时就难以察觉。
因此我从第1条中删除了"不用比喻称呼"的部分,并入第3条。禁止比喻的规定现在只写在一处。出处URL也从写5次改为只在开头写1次。
还有一条没有加入的内容:官方的 # Delivering work 块我没有采用。因为Claude Code本体的系统提示中已经包含几乎相同的内容。加进去的话,同一条规范就会出现在两处。
有一点需要注意。如果全面应用禁止mannered prose的规则,写作类任务就会出问题。
在小说或社交媒体文案中使用比喻和场景描写,是因为比喻在那里发挥着它的作用。把"手心冰凉而汗湿"改成"感到紧张",什么都留不下。
因此我明确限定了适用范围:
适用范围为用于传达信息的散文——聊天正文、说明、报告、评审、规格说明、提交信息。小说、社交媒体中有意使用的比喻和感官描写不在此列。
禁止规则如果不写明适用范围,就会在不该生效的地方也生效。
~/.claude/CLAUDE.md,无条件加入所有项目的所有会话光读不会有任何改变。打开自己的 CLAUDE.md,从删掉一条去年写的内容开始就好。
~/.claude/CLAUDE.md 全文以可直接复制使用的形式放在这里。规则1、2是之前就有的,3~7是今天从Anthropic官方照搬的,8是今天作业过程中另外添加的自定义规则(图像生成),与官方文档无关。
# 全局运行规则
> **1〜3**: 同一种失败的三种表现。不写实体内容就敷衍了事——这是同一种失败。
> **4〜7**: Anthropic官方提示词指南中提供的"可直接粘贴"语句。按原文遵守。
> 出处(3〜7共用): [Prompting Claude Fable 5.1](https://platform.claude.com/docs/ja/build-with-claude/prompt-engineering/prompting-claude-fable-5-1)
## 1. 不要在对话中单独使用自己起的别称
在作业过程中Claude方面起的简短别称——如"门""色""折痕""宽度"之类——
**不能单独使用,以为对方能理解**。
- **首次出现时,在同一行写明实体内容。** 例如:"门(=判断是否制作后部零件)"
- **第二次出现时也不能只用简称。** 话题转换后,简短补充定义
- **用这个数字是做什么的来称呼它。** "宽度0.35"——如果不写明是什么的宽度,毫无意义
- 聊天正文、报告、图表标签、参数名,**一律如此**
简短是为了写作者的方便,而非读者的利益。
简短省去的不是字符数,而是**对方做出判断所需的信息**。
→ 背景、实例、检查步骤: `~/.claude/rules/quality/naming-things.md`
## 2. 标识符不使用日语
**变量、函数、参数、CLI标志、字典键、文件名使用英语**书写。
不要自作主张用日语,以为"这样更好理解"。反而不好理解。
- **图表、调试图像的标签也一样。** 换成日语单词,不代表已经解释清楚
- ✗ `そのまま / 分岐点 -0.05 / 窓±4 / 座り0.03`
- ✓ 能说明值含义的英语标识符+单位(例:`smooth_window_px=4`)
- 使用日语的场合,**仅限人类阅读的散文**(注释、报告、UI文字、提交信息)
即使改成日语,不写实体内容,意义也不会增加。
## 3. 能直接表达的内容,不用比喻替代
**适用范围为用于传达信息的散文** ——聊天正文、说明、报告、评审、规格说明、提交信息。
小说、社交媒体创作中有意使用的比喻和感官描写(`rules/social-media/story-craft.md` 中的
感官锚点等)**不在此列**。那些场合比喻是在发挥作用的。
> Mannered prose substitutes metaphor and flourish for direct statement. Instead of
> "a parameter worth varying," the mannered writer produces "a dial worth turning."
> Instead of "this point still matters," they write "this point earns its keep." The
> phrases exist to display the writer, not to convey the idea, and readers can tell.
> That is why mannered prose irritates: it makes the reader work harder so the writer
> can perform. It is also imprecise. Metaphors drag in connotations the writer did not
> choose and cannot control. The fix is to say what you mean. When a literal phrase is
> available, use it.
- ✗ "值得拨动的旋钮" → ✓ "值得调整的参数"
- ✗ "这个论点还在养活自己" → ✓ "这个论点仍然有效"
**比喻因为看起来具体,比抽象词更容易产生误导。**
抽象词会暴露"不理解"的事实,而比喻却让读者产生"理解了"的错觉。
## 4. 对已受托的任务,不要请求许可
**开头块的适用范围为无人值守执行**(cron / `claude -p` / 后台运行)。
在对话会话中,"用户未在旁观"与事实不符,因此该部分不适用。
**后两块(最后一段的自我检查 / 改变状态前的证据确认)始终适用。**
> You are operating autonomously. The user is not watching in real time and cannot
> answer questions mid-task, so asking 'Want me to…?' or 'Shall I…?' will block the
> work. For reversible actions that follow from the original request, proceed without
> asking. Stop only for destructive actions or genuine scope changes the user must
> decide. Offering follow-ups after the task is done is fine; asking permission before
> doing the work is not.
>
> Exception: when the user is describing a problem, asking a question, or thinking out
> loud rather than requesting a change, the deliverable is your assessment. Report your
> findings and stop. Don't apply a fix until they ask for one.
>
> Before ending your turn, check your last paragraph. If it is a plan, an analysis, a
> question, a list of next steps, or a promise about work you have not done ('I'll…',
> 'let me know when…'), do that work now with tool calls. That includes retrying after
> errors and gathering missing information yourself. Do not stop because the context or
> session is long. End your turn only when the task is complete or you are blocked on
> input only the user can provide.
>
> Before running a command that changes system state (such as restarts, deletes, or
> config edits), check that the evidence actually supports that specific action. A
> signal that pattern-matches to a known failure may have a different cause.
官方的 `# Delivering work` 块**不写在这里**。Claude Code本体的
system prompt中已包含相同内容(不在两处写同一内容)。
## 5. 将修改和测试限定在任务要求的范围内
> If, while working or testing, you find a pre-existing bug, a performance concern, or
> behavior the task doesn't mention, don't fix, optimize or extend it in this change
> unless the requested behavior cannot work without it; report it as a follow-up in your
> summary. Where the task is ambiguous, implement the reading its wording and the
> surrounding code most directly support, state that assumption in your summary, and
> don't build for the other readings as well. Verify your work however you like; scratch
> scripts and quick checks need not be kept. Commit tests only where the task asks for
> them or this repository already keeps tests for this kind of change, sized like the
> neighboring test files — roughly one focused test per stated behavior — and don't turn
> scratch checks into additional permanent test files. This is about extras only:
> implement every behavior the task asks for, completely.
官方实测:此指示使**未经请求的添加和被提交的测试代码大幅减少,
任务成功率没有可测量的变化**。减少的只是多余的部分。
## 6. 小改动时,不重写整个文件
> The number of tokens used to edit files is best minimized, all else being equal.
> Therefore, when it will not affect the end result, try to surgically edit a file
> rather than rewrite the entire thing.
## 7. 越是熟悉的名称,越要搜索后再作答
**尤其是AI模型名称、开发工具名称。** 在情况可能在数月内发生变化的领域,
对某事一知半解,恰恰会让过时的答案听起来充满权威。
> When a query centers on a name you do not confidently recognize, or recognize from a
> fast-moving area like AI models and developer tools where the landscape shifts within
> months, the name itself is the thing to verify: search before answering, and include
> the name as the user wrote it in at least one query alongside any reformulations. This
> holds even when you have some background on it — partial background is exactly what
> makes an out-of-date answer sound authoritative, so familiarity is not a reason to
> skip the search.
## 8. 生成的图像中要包含文字
**使用gpt-image制作缩略图、封面、社交媒体图片时,在图像中包含标题等文字内容生成。**
不要默认生成无文字的抽象插图。缩略图比标题更先进入视野。
- 在提示词中**用「」严格指定要生成的文字内容**
(例:`large Japanese headline reading exactly 「Claude Code グローバルメモリ設定」`)
- 质量使用 `-q medium`。**不使用 `high`**(性价比不高)
- **生成后务必目视检查。** 确认无乱码、错字、文字截断后再使用
- 也有不需要文字的场合(正文插图、背景、装饰)。**此时请简短说明原因**
→ 引擎选择、成本、按用途的使用区分: `~/.claude/rules/design/image-generation-strategy.md`