正在加载陈远的个人网站

Claude 5 上下文工程的减法法则

Thariq总结Claude 5如何精简系统提示词、按需加载技能并用测试驱动规格提升编码效果。

原始内容

Thariq at Anthropic just published the new rules of context engineering for Claude 5 models. the same rules let them cut ~𝟴𝟬% of Claude Code's own system prompt with no measurable loss on their coding evals. tldr: • CLAUDE.md is for gotchas only. delete anything the model learns by looking at the repo, and spend the tokens on what it would trip over • swap hard bans for style descriptions. their old rule was never write comments. the new one is write code that matches the comment density and naming around it • stop writing the same rule in both the system prompt and the tool description. keep only the tool description • split a long skill into several files and load only what the task needs • a spec does not have to be markdown. a test suite, an HTML mockup, or a function in another codebase all work worth more than the next ten skills. read it today, then run /doctor to trim your own CLAUDE.md and skills.

The new rules of context engineering for Claude 5 models I’ve written previously about how to best [prompt the newest generation of Claude 5 models](https://x.com/trq212/status/2073100352921215386) and work with them iteratively to discover what you want to build. But when you send a message to Claude, the prompt is only a small part of the context it gets. Much of your context is assembled from your system prompt, Skills, CLAUDE.md files, memory, and other sources. We call this [context engineering](https://www.anthropic.com/engineering/effective-context-engineering-for-ai-agents), and it makes a big impact on the results you generate when using Claude Code or in building your own agents. Unlike a prompt, context is used generally across many requests, so it cannot be as specific. How do you build these general prompts and guidance for Claude, especially when you don’t know what a user’s prompt might be? This can be surprisingly difficult as Claude’s own capabilities evolve. Most recently, we noticed a large jump in the way we prompt the newest generation of Claude models. We removed over 80% of Claude Code’s system prompt for models like Claude Opus 5 and Claude Fable 5 with no measurable loss on our coding evaluations. Here’s what we’ve learned about prompting this new class of models, and how you can utilize it to update your context engineering. We’ve put these best practices in `claude doctor`, use the command /doctor in Claude Code to rightsize your skills, and CLAUDE.md files. ## Unhobbling Claude Overall, we found that we were over-constraining Claude Code, both through our system prompt and in our CLAUDE.md files and skills. For example, when we read transcripts of our own internal usage of Claude Code, we see several conflicting messages in a single request like “leave documentation as appropriate,” or “DO NOT add comments” as our system prompt, skills, and user requests clash with each other. Generally, Claude can interpret the user’s intent to get to the right answer, but Claude must think more carefully about these overlapping and conflicting messages before deciding what to do. And while these constraints were once needed to avoid worst case scenarios, we have since found we can delete many of them and let the model use surrounding context and judgement instead. Additionally, Claude Code now has many more tools. Claude used to rely on CLAUDE.md as a source of memory, information, and guidance. Now we have memory, artifacts, and skills, which Claude can use to create new ways of loading and sharing context across sessions. ## Then and now There were a number of previous context engineering best practices that had become myths. Including: Then: Give Claude rules Now: Let Claude use judgement When we first rolled out Claude Code, we needed to be sure that Claude avoided worst case scenarios, such as deleting files. This meant we would give particularly strong guidance that might not always be true, For example, in the system prompt we used to say: In code: default to writing no comments. Never write multi-paragraph docstrings or multi-line comment blocks — one short line max. Don't create planning, decision, or analysis documents unless the user asks for them — work from conversation context, not intermediate files. But for a certain subset of prompts, this guidance would be wrong. In the case of documentation, the user may have their own preferences, or specific parts of very complex code might need multi-line comment blocks. Still, without these guardrails for older models, the comments Claude wrote would be incorrect in many cases and we had to accept this tradeoff. But newer models have better judgement and can handle these decisions well without explicit rules. In the new system prompt we say: Write code that reads like the surrounding code: match its comment density, naming, and idiom. Then: Give Claude examples Now: Design interfaces The number one rule for tool usage was to give Claude examples on how to use them. With our newest models, we’ve found that giving examples actually constrains them to a certain exploration space. Instead of using examples, think more about the design of your tools, scripts and files- what parameters does Claude have and how can they be more expressive? For example, in the Todo tool example, just listing status as an enumeration between pending, in_progress, and completed, hints to Claude about how to use it. The instruction on keeping one item in_progress helps define our requested behavior. Then: Put it all upfront Now: Use progressive disclosure Because Claude Code was focused on coding, our system prompt included detailed information on how to do code review and verification. These were not always needed, but when they were, it was crucial information. Since then, Claude Code has gotten very competent at using progressive disclosure- loading the right context at the right times. For example, we moved verification and code review into their own skills that Claude Code could selectively call. But progressive disclosure is not just for skills, we also use it for tools. Some of our tools are ‘deferred loading,’ which means the agent must search for their full definitions using ToolSearch before using them. This allows us to have more tools (such as our Task tools) that don’t take up context until they’re needed. The same can be applied to your own CLAUDE.md and Skill.md files. A common myth is that you want to make these a central repository for every known practice that you might run into, because Claude would not find it otherwise. Instead, [consider having a tree of files that can be loaded at the right time](https://claude.com/blog/a-harness-for-every-task-dynamic-workflows-in-claude-code). Then: Repeat yourself Now: Simple tool descriptions Earlier Claude models could sometimes need repeated instructions or be more likely to listen to instructions at the end of their context window than at the start. This meant our system prompt would sometimes have references to tools in the main system prompt as well as instructions in the tool description. We found we could delete these repeat examples and put instructions on how to use tools in the tool descriptions rather than the system prompt. Then: Memory in CLAUDE.md files Now: Auto-memory We used to encourage users to save things to Claude’s memory, by using the # hotkey to write to their [CLAUDE.md](http://claude.md/) automatically. Instead, Claude now automatically saves memories that are relevant to the work and to you. Then: Simple specs Now: Rich references In plan mode, Claude Code has heavily relied on markdown files with plans. Storing these files as plans helped Claude refer to them when needed. Another similar best practice was to store specs in the codebase for Claude to refer to while working across longer projects. But we’ve found that Claude can handle increasingly more complicated references. Instead of simple markdown files, Claude can reference HTML artifacts created by our new artifacts feature. You may also give Claude references in the form of code. A spec may also be a detailed test suite, or a function in a different codebase that Claude might port. Rubrics are another form of references. Rubrics allow Claude to try and verify your taste in a particular field (e.g. what does a good API design look like) by using [dynamic workflows](https://claude.com/blog/a-harness-for-every-task-dynamic-workflows-in-claude-code) and spinning up verifier agents with those rubrics. ## Applying this to your context Pulling this all together, what does this look like when you assemble your context? System Prompt A system prompt is heavily tied to the product context. It tells Claude what product it’s operating in and what it’s doing. For Claude Code, you will likely never modify this, but if you are building your own agent harness, this is where you should spend a lot of time. CLAUDE.md Keep your CLAUDE.md lightweight and briefly describe what your repo is for, but spend most of the tokens on gotchas inside of the codebase. For example, you may organize your code to keep types in one monolithic file and nowhere else. Avoid stating ‘the obvious’ things Claude should know by looking at your file system or your repo. Use progressive disclosure for more details, for example if you have several unique instructions on how to verify your work, create a verification skill and reference it from your CLAUDE.md. Skills Think of skills as lightweight guides to let Claude find information when needed. Avoid making them overconstrained, except in highly important areas. For long skills, try and use progressive disclosure as much as possible- divide it into many files and split them out. It’s best when skills encode particular opinions, knowledge, or best practices that are particular to you, your team, or product. References You can @ mention files to include them as references. References allow Claude to refer to in-depth information about the current plan. This might be in specs files, mockups, or even entire codebases. Generally you should prefer files that are in code as it provides clear, high-fidelity instructions to Claude in a language it knows very well. For example, a HTML mockup of a design will generally produce better results than a description of the design or a screenshot. ## Try simplifying Across your system prompt, skills, and CLAUDE.md files, you may need to simplify just like we did. We rolled out a new command called `claude doctor,` which will help you do this automatically as well. For more details on prompting more advanced models specifically, check out our [Fable field guide](https://claude.com/blog/a-field-guide-to-claude-fable-finding-your-unknowns).

— @trq212(Thariq)的引用原文

中文译文

机器翻译,保留原文供核对。

Anthropic 的 Thariq 刚刚发布了 Claude 5 模型上下文工程的新规则。同一套规则让他们把 Claude Code 自身系统提示词的约 𝟴𝟬% 砍掉,而在编码评测上没有任何可测量的损失。tldr: • CLAUDE.md 只用来写坑点(gotchas)。凡是模型看一眼仓库就能学会的内容都删掉,把 token 花在它会栽跟头的地方 • 用风格描述替换硬性禁令。他们旧规则是「从不写注释」。新规则是「写出与周围代码注释密度和命名方式相匹配的代码」 • 不要同一条规则既写在系统提示词里又写在工具描述里。只保留工具描述 • 把一个长技能拆成多个文件,只加载任务需要的那部分 • 规格说明不一定要写成 markdown。测试套件、HTML 原型稿,或另一个代码库里的某个函数都可以 比接下来十个技能都更值钱。 今天就读,然后运行 /doctor 来精简你自己的 CLAUDE.md 和技能。

Claude 5 模型上下文工程的新规则 我之前写过如何最好地[为最新一代 Claude 5 模型写提示词](https://x.com/trq212/status/2073100352921215386),并与它们迭代协作,去发现你想构建的东西。 但当你给 Claude 发一条消息时,提示词只占它收到的上下文的一小部分。你的大部分上下文是由系统提示词、Skills、CLAUDE.md 文件、记忆和其他来源拼装而成的。我们把这称为[上下文工程](https://www.anthropic.com/engineering/effective-context-engineering-for-ai-agents),它对你在使用 Claude Code 或构建自己的 agent 时产出什么结果有很大影响。 与提示词不同,上下文是跨许多请求通用使用的,所以它没法那么具体。你要如何为 Claude 构建这些通用的提示词和指引,尤其是在你不知道用户的提示词会是什么的情况下? 这会出奇地困难,因为 Claude 自身的能力在不断演进。最近,我们注意到在给最新一代 Claude 模型写提示词的方式上出现了一次大幅跃升。我们为 Claude Opus 5 和 Claude Fable 5 这类模型删掉了 Claude Code 系统提示词的 80% 以上,而我们的编码评测没有任何可测量的损失。 以下是我们学到的、关于给这类新模型写提示词的经验,以及你可以如何用它来更新你的上下文工程。我们已把这些最佳实践放进 `claude doctor`,在 Claude Code 里使用 /doctor 命令来为你的技能和 CLAUDE.md 文件做尺寸校正。 ## 给 Claude 松绑 总的来说,我们发现我们通过系统提示词、以及在 CLAUDE.md 文件和技能里,对 Claude Code 过度约束了。 例如,当我们翻阅自己内部使用 Claude Code 的对话记录时,会在单个请求里看到几条互相冲突的信息,比如「按需保留文档」,或「不要添加注释」,因为我们的系统提示词、技能和用户请求彼此打架。 一般来说,Claude 能推断用户意图以抵达正确答案,但在决定怎么做之前,Claude 必须更仔细地思考这些重叠且矛盾的信息。 虽然这些约束曾经是为了避免最坏情况所必需的,但我们后来发现可以删掉其中许多,让模型转而使用周围的上下文和判断力。 此外,Claude Code 现在有更多工具。Claude 过去依赖 CLAUDE.md 作为记忆、信息和指引的来源。现在我们有了记忆、artifacts 和技能,Claude 可以用它们创造出跨会话加载和共享上下文的新方式。 ## 过去与现在 有许多过去的上下文工程最佳实践已经变成了迷思。包括: 过去:给 Claude 规则 现在:让 Claude 运用判断力 当我们最初推出 Claude Code 时,我们需要确保 Claude 避开最坏情况,比如删除文件。这意味着我们会给出特别强硬的指引,而它们并不总是成立。例如,在系统提示词里我们曾写道: In code: default to writing no comments. Never write multi-paragraph docstrings or multi-line comment blocks — one short line max. Don't create planning, decision, or analysis documents unless the user asks for them — work from conversation context, not intermediate files. 但对某一类提示词来说,这条指引会是错的。以文档为例,用户可能有自己的偏好,或者非常复杂的代码里某些部分确实需要多行注释块。 不过,如果对更老的模型没有这些护栏,Claude 写出的注释在很多情况下就会不正确,我们不得不接受这个权衡。但更新的模型判断力更好,无需明确规则就能很好地处理这些决定。 在新的系统提示词里我们写的是:Write code that reads like the surrounding code: match its comment density, naming, and idiom. 过去:给 Claude 示例 现在:设计接口 过去工具使用的头号规则是给 Claude 示例,告诉它怎么用。对最新的模型,我们发现给示例实际上会把它们限制在某个探索空间里。 与其用示例,不如多思考你的工具、脚本和文件的设计——Claude 有哪些参数,它们怎样才能更有表达力? 例如在 Todo 工具的例子中,仅把 status 列成 pending、in_progress 和 completed 这样的枚举,就在向 Claude 暗示它该怎么用。而关于保持一项处于 in_progress 的说明,则帮助定义了我们要的行为。 过去:全部前置 现在:渐进式披露 因为 Claude Code 专注于编码,我们的系统提示词里包含了如何做代码审查和验证的详细信息。这些并不总是需要,但一旦需要,就是关键信息。 此后,Claude Code 在渐进式披露上变得非常擅长——在正确的时机加载正确的上下文。例如,我们把验证和代码审查移到了各自的技能里,让 Claude Code 可以有选择地调用。 但渐进式披露不只用于技能,我们也用它来处理工具。我们有些工具是「延迟加载」的,也就是说 agent 必须先通过 ToolSearch 搜索到它们的完整定义才能使用。这让我们可以拥有更多工具(比如我们的 Task 工具),它们在需要之前不占用上下文。 同样的做法可以用在你自己的 CLAUDE.md 和 Skill.md 文件上。一个常见的迷思是:你想把它们做成一个中央仓库,收录你将来可能遇到的每一条已知实践,因为否则 Claude 就找不到它。相反,[可以考虑做一个能在正确时机加载的文件树](https://claude.com/blog/a-harness-for-every-task-dynamic-workflows-in-claude-code)。 过去:重复自己 现在:简洁的工具描述 更早的 Claude 模型有时需要重复指令,或者更倾向于听从上下文窗口末尾的指令而不是开头的。这意味着我们的系统提示词有时会在主系统提示词里提到某些工具,同时在工具描述里也写上指令。 我们发现可以删掉这些重复的示例,把如何使用工具的说明放在工具描述里,而不是系统提示词里。 过去:把记忆放在 CLAUDE.md 文件里 现在:自动记忆 我们过去鼓励用户把内容存进 Claude 的记忆,用 # 快捷键自动写入他们的 [CLAUDE.md](http://claude.md/)。现在,Claude 会自动保存与工作和你本人相关的记忆。 过去:简单的规格说明 现在:丰富的引用 在计划模式下,Claude Code 曾大量依赖带计划的 markdown 文件。把这些文件存成计划,有助于 Claude 在需要时回看它们。另一个类似的最佳实践是把规格说明存进代码库,供 Claude 在跨较长项目工作时参考。 但我们发现 Claude 能处理越来越复杂的引用。不再是简单的 markdown 文件,Claude 可以引用我们新的 artifacts 功能创建的 HTML artifacts。 你也可以用代码的形式给 Claude 提供引用。一份规格说明也可以是详细的测试套件,或是另一个代码库里某个 Claude 可能移植过来的函数。 评分标准(Rubrics)是另一种引用形式。评分标准让 Claude 通过[动态工作流](https://claude.com/blog/a-harness-for-every-task-dynamic-workflows-in-claude-code)并启用带这些评分标准的验证器 agent,来尝试校验你在某个特定领域的品味(例如,好的 API 设计长什么样)。 ## 把这套方法用到你的上下文上 把这些都汇总起来,在你组装上下文时它是什么样子? 系统提示词 系统提示词与产品上下文紧密绑定。它告诉 Claude 它运行在什么产品里、在做什么。对 Claude Code 来说,你大概永远不会改这个;但如果你在构建自己的 agent harness,这就是你应该花大量时间的地方。 CLAUDE.md 让 CLAUDE.md 保持轻量,简要描述你的仓库是做什么的,但把大部分 token 花在代码库内的坑点上。例如,你可能会把代码组织成把类型集中放在一个单一大文件里,其他地方不放。避免陈述 Claude 看一眼文件系统或仓库就该知道的「显而易见」的东西。 用渐进式披露承载更多细节,比如你有若干关于如何验证工作的独特说明,就创建一个验证技能,并从你的 CLAUDE.md 里引用它。 技能 把技能看作让 Claude 在需要时找到信息的轻量指南。避免把它们写得过度约束,除非是极其重要的领域。 对长技能,尽量多用渐进式披露——把它拆成多个文件并分离出去。 技能最好能编码那些你、你的团队或你的产品特有的观点、知识或最佳实践。 引用 你可以用 @ 提及文件,把它们作为引用纳入。引用让 Claude 能参考关于当前计划的深入信息。 这可能是规格文件、原型稿,甚至整个代码库。一般来说你应优先用代码形式的文件,因为它以 Claude 非常熟悉的语言提供了清晰、高保真的说明。例如,一个设计的 HTML 原型稿通常会比一段设计描述或一张截图产出更好的结果。 ## 试试简化 在你的系统提示词、技能和 CLAUDE.md 文件里,你可能也需要像我们一样做简化。我们推出了一个新命令 `claude doctor,`,它也能帮你自动完成这件事。关于更具体地给更先进模型写提示词的更多细节,请看我们的 [Fable 实战指南](https://claude.com/blog/a-field-guide-to-claude-fable-finding-your-unknowns)。

引用内容译文

深度解析

是什么 这是一条对 Anthropic 经验总结的二次转述,核心主题不是增加更多提示词,而是通过上下文工程做减法。据转述,Claude 5 相关模型在减少约 80% Claude Code 系统提示词后,编码评测没有出现可测量损失。

核心设计 其方法可归纳为四步:CLAUDE.md 只保留模型无法从仓库自行推断的陷阱;用周边代码的注释密度和命名习惯替代“绝对禁止”式硬规则;避免系统提示词与工具描述重复表达;把长技能拆成多个文件,按任务需要加载。更进一步,规格不必局限于 Markdown,测试套件、HTML 模拟稿或另一代码库中的函数同样可以承担规范作用。

关键洞察 高质量 Agent 指令的价值在于补充不确定性、减少决策歧义,而不是复述环境中已经存在的信息。把可执行资产作为规格,还能让约束更接近验证环节,降低提示词维护成本。

边界与风险 这是针对 Claude Code 与特定新模型的经验转述,不能直接证明所有模型都适用。精简规则前仍需保留安全、隐私、发布流程等仓库无法可靠推断的约束,并通过回归评测验证删除后的行为变化。

精选追评与案例

  1. 提供已经实践拆技能+subagent 按需调用的真实工作方式,可作为落地参照。

    @Voxyz_ai A lot of times these posts just validate what a lot of us (I hope) are already doing. Splitting skills and having defined subagents for everything - calling only the tools and skills needed for the task - is how I’ve been running my loops for a while now

    中文译文 · 机器翻译

    @Voxyz_ai 很多时候这类帖子只是在印证我们许多人(我希望如此)已经在做的事。把技能拆开、为每件事定义好 subagent——只调用任务所需的工具和技能——这就是我一段时间以来跑循环的方式

  2. 给出可直接运行的替代工具与安装命令,是 /doctor 之外的可选实操路径。

    @Voxyz_ai Exactly this! That is why I built claude-tuneup (npx skills add paulovitin/claude-tuneup) — an open-source tool that audits your CLAUDE.md and session rules to cut unnecessary tokens following Anthropic guidelines! 🚀 https://t.co/5nBjDZ7tj0

    中文译文 · 机器翻译

    @Voxyz_ai 正是如此!这就是我构建 claude-tuneup(npx skills add paulovitin/claude-tuneup)的原因——一个开源工具,按 Anthropic 的指南审计你的 CLAUDE.md 和会话规则,砍掉不必要的 token!🚀 https://t.co/5nBjDZ7tj0

  3. 补充该工具的具体能力(查重、去僵化、可回退),便于判断是否值得尝试。

    @Voxyz_ai Ele analisa regras obsoletas ou duplicadas, transforma instruções engessadas em julgamento de contexto e limpa caches — tudo interativo e reversível!

    中文译文 · 机器翻译

    @Voxyz_ai 它会分析过时或重复的规则,把僵化的指令转成基于上下文的判断,并清理缓存——全程交互式且可回退!

  4. 提出把规则内化为提示词预处理层的思路,是一个可继续讨论的应用方向。

    @Voxyz_ai Think it could just give to my Claude agent and tell it to follow these new rules when coding, ie translate my prompts so that it adheres to these new rules before being coded?

    中文译文 · 机器翻译

    @Voxyz_ai 我觉得可以直接把它交给我的 Claude agent,让它在编码时遵循这些新规则,也就是在写代码之前先把我的提示词翻译成符合这些新规则的形式?

  5. 一句话点明把 CLAUDE.md 当踩坑备忘录用最省 token,与原文核心一致。

    @Voxyz_ai 把提示词当踩坑备忘录用,挺省的