适用工具:Claude Code · Codex · Cursor · Copilot(实现位置见对照表)· 验证于 2026-08
上下文窗口装着整段对话:每条消息、每个读过的文件、每次命令输出。窗口越满,模型表现越差——Anthropic 称之为需要管理的”最重要资源”,OpenAI 称之为 context pollution / context rot。本章的每条实践都指向同一件事:只让当前任务需要的信息占据窗口。
场景:上一个任务结束,要开始一个不相关的新任务;或者同一个问题你已经纠正 agent 两次还没对。
做法:
/clear
/clear——然后把这两轮学到的东西写进一条更具体的初始 prompt 重新开始。干净会话 + 好 prompt 几乎总是胜过带着失败尝试的长会话。/compact <instructions> 定向压缩(如 /compact Focus on the API changes);顺手的小问题用 /btw 问,答案不进入对话历史。依据:Anthropic 把”kitchen sink session”和”correcting over and over”列为头两个常见失败模式,修法都是 /clear(最佳实践指南)。
边界:深入单个复杂问题时历史本身有价值,不要中途清空——Anthropic 明确说”sometimes you should let context accumulate”。
反模式:靠自动压缩兜底。压缩是有损摘要,被压掉的决策和文件状态不会回来(对策见 6.3)。
场景:需要摸清一个子系统、跑一轮测试、翻大量日志——这些工作会产生几万 token 的中间输出,而你之后只需要结论。
做法:
Use subagents to investigate how our authentication system handles token
refresh, and whether we have any existing OAuth utilities I should reuse.
Report back file paths and a summary; don't paste file contents.
.claude/agents/*.md(sub-agents)。/agent 查看和切换 agent 线程(subagents)。.cursor/agents/*.md,用 /name 显式调用(subagents)。依据:OpenAI 官方把这归为对抗 context pollution 的核心手段:”Return summaries from subagents instead of raw intermediate output”(Codex subagents);Anthropic 的表述是”探索在独立窗口进行,主对话留给实现”(sub-agents)。
边界:写多改重的任务不要并行丢给多个 subagent——OpenAI 明确警告并发写代码会制造冲突、抬高协调成本;subagent 每个都独立跑模型,token 消耗高于单 agent。
反模式:让 agent 无边界地”investigate”——它会读几百个文件填满主上下文。要么收窄范围,要么丢给 subagent。
场景:跨多次会话的大任务;或者你预计会话会长到触发自动压缩。
做法:
I want to build [brief description]. Interview me in detail using the
AskUserQuestion tool. Ask about technical implementation, edge cases,
and tradeoffs. Keep interviewing until we've covered everything,
then write a complete spec to SPEC.md.
Use a subagent to review the rate limiter diff against PLAN.md. Check that
every requirement is implemented and nothing outside the task's scope changed.
依据:这是 Anthropic 官方的 “Let Claude interview you” 工作流原文——”Once the spec is complete, start a fresh session to execute it”(最佳实践指南)。对话在接近上限时会被自动压缩成摘要,文件不会。
边界:一句话能描述清 diff 的小改动不值得写计划文件——Anthropic 原话是 “If you could describe the diff in one sentence, skip the plan”。
反模式:让计划只存在于 plan mode 的对话输出里,然后在同一个塞满探索记录的会话里直接开写。
场景:任务跨越多次坐下来的时间;或者你要让 agent 尝试一个可能失败的激进方案。
做法:
commit with a descriptive message and open a PR
Esc Esc 或 /rewind 可以只回滚对话、只回滚代码、或两者一起。用它做低成本试错——让 agent 试激进方案,不行就 rewind 换路。claude --continue 接最近会话,claude --resume 从列表选;用 /rename 给会话起 oauth-migration 这类可检索的名字。Cursor 的 subagent 返回 agent ID,可以 resume 继续之前的线程。依据:Anthropic 官方四阶段工作流以 commit 收尾,并明确警告 checkpoint “isn’t a replacement for git”(最佳实践指南);subagent resume 语义见 Cursor subagents。
边界:checkpoint 是会话内的撤销机制,不是团队协作产物——需要别人(或 CI)接手的进度必须以 commit / PR 形式存在。
场景:AGENTS.md / CLAUDE.md 越写越长,agent 开始无视其中的规则。
做法:
@ import 引用(Claude Code 语法,相对路径以所在文件为基准,最多递归 4 层):See @README for project overview and @package.json for available npm commands.
# Additional Instructions
- git workflow @docs/git-instructions.md
- 个人偏好(跨 worktree 共享):@~/.claude/my-project-instructions.md
注意 import 的文件仍在启动时全量进入上下文——@ 解决的是组织问题,不是预算问题。想提路径而不触发 import,用反引号包住:`@README`。
.claude/rules/ 的 paths frontmatter,规则只在 agent 读到匹配文件时载入:---
paths:
- "src/api/**/*.ts"
---
# API Development Rules
- All API endpoints must include input validation
- Use the standard error response format
Cursor 的等价物是 .cursor/rules/*.mdc 的 globs 字段(globs: src/components/**/*.tsx + alwaysApply: false)。
<!-- ... --> 块级注释,不花 token。依据:@ import 语法、200 行目标、paths 规则、HTML 注释剥离均来自 Claude memory 官方页;Cursor globs 语法见 rules 官方页;”Bloated CLAUDE.md files cause Claude to ignore your actual instructions” 见 最佳实践指南。
边界:@ import 是 Claude Code 专属语法,Codex / Cursor / Copilot 不解析;跨工具通用的按需引用写法是正文一句”改 X 前先读 docs/Y.md”。
反模式:把风格指南整本粘进 memory 文件——Cursor 官方点名这是错法:”Use a linter instead. Agent already knows common style conventions.”