Skip to content
Back/Claude Code

Claude Code 上下文管理完整指南 — 2026-04-18 更新

View in Graph
Updated 2026-05-10
3 min read
560 words

Claude Code 上下文管理完整指南

核心来源:Anthropic 工程师 Thariq Shihipar(官方指南),Ben Tossell(实战反馈) 官方博客https://claude.com/blog/using-claude-code-session-management-and-1m-context

这是 claude-code/context-window-optimization 的补充与更新,聚焦于 session 管理层面的实战决策。


核心命题:窗口大小 ≠ 使用效率

1M Token 上下文窗口是真实的,但它不意味着可以无限堆积内容。真正的限制是 Context Rot(上下文腐化)

技术解释:随着 token 增多,注意力机制被摊薄到更多位置,旧的、不相关的内容开始干扰当前任务,导致性能下降——即使总 token 量远低于窗口上限。

Ben 的 60% 警戒线:上下文窗口达到约 60% 时就应开始主动管理,而不是等到满了再处理。


五种上下文管理工具

情景 工具 理由
同一任务,上下文仍有效 直接继续(Continue) 窗口内容仍负载有效信息
Claude 走错了路 /rewind(双击 Esc) 保留有用的文件读取,丢弃失败尝试
会话臃肿,充满调试噪音 /compact <hint> 低成本;Claude 决定哪些重要,可加 hint 引导
开始全新任务 /clear 零腐化;你控制携带哪些内容
下一步会产生大量中间输出 Subagent 中间噪音留在子 Agent 的上下文;只有结论回到主线

关键决策:/compact vs /clear

使用 /compact 的场景

  • 同一任务仍在进行,但 session 被调试内容撑大
  • <hint> 引导 Claude 保留关键上下文(如"保留数据库 schema 和核心函数签名")
  • 成本低,但质量取决于 Claude 对下一步方向的预判

使用 /clear 的场景

  • 开始全新任务
  • 上一个任务已完全结束
  • 你想精确控制哪些内容进入下一个会话

最反直觉的建议(Thariq 原文):当 Claude 走错路,不要直接说"那个不行,换 X 方法"。 正确做法:/rewind 到文件读取完成后的节点,然后用学到的信息重新 prompt,明确排除失败路径。这样保留了有价值的读取操作,丢弃了失败路径的上下文污染。


Subagent 心智测试

"我之后还需要这个 tool 输出本身,还是只需要结论?"

如果只需要结论 → 用 Subagent,让中间噪音留在子 Agent 的上下文。 如果需要引用具体输出 → 留在主会话。

Subagent 委托策略(Pawel Huryn)

Anything that is bulk-mechanical, scoped research, or parallelizable should run in a subagent. The parent context stays clean and you parallelize across cheaper models.

Model selection for subagents:

  • Haiku: bulk mechanical work, no judgment
  • Sonnet: scoped research, code exploration, in-scope synthesis
  • Opus: subtasks needing real planning or tradeoffs

If a subagent realizes it needs a higher tier than itself, return to the parent. Parent owns final output and cross-spawn synthesis.

CLAUDE.md task-delegation block template:

## Task Delegation

Spawn subagents to isolate context, parallelize independent work, or offload bulk mechanical tasks. Don't spawn when the parent needs the reasoning, when synthesis requires holding things together, or when spawn overhead dominates.

Pick the cheapest model that can do the subtask well:
- Haiku: bulk mechanical work, no judgment
- Sonnet: scoped research, code exploration, in-scope synthesis
- Opus: subtasks needing real planning or tradeoffs

If a subagent realizes it needs a higher tier than itself, return to the parent.

Parent owns final output and cross-spawn synthesis. User instructions override.

Skills as Subagents

Add agent: true and model: to a skill's frontmatter and the skill runs in its own subagent context with its own model. The parent gets back only the structured output; the full input never touches parent context.

Example skill frontmatter:

---
name: tldr-pdf
description: Extract a 200-word TL;DR from a PDF without loading the full text into the parent context
agent: true
model: sonnet
---

与 Opus 4.7 xhigh 模式的关系

Claude Opus 4.7 新增 xhigh 努力等级(Claude Code 默认),在更长的上下文下仍需遵守相同的管理原则。Claude Opus 4.7 的 Document Reasoning 提升(+23.5 pts)和 Graphwalks 提升(+19.9 pts)说明模型对长上下文的处理能力在提升,但 Context Rot 的物理限制仍然存在。


实战 Checklist(Ben's Cheatsheet 精华)

  1. 60% 警戒线:上下文达到约 60% 时主动触发 /compact
  2. 新任务 = 新会话:切换到全新任务时,评估是否需要 /clear
  3. 纠错用 /rewind:Claude 走错路时,不要继续追加,而是回退到干净节点
  4. 复杂搜索用 Subagent:需要大量搜索/验证时,派 Subagent 做,只要结论
  5. Side Chat 探索:探索性问题用 ⌘+; 开 Side Chat,不污染主线程(Desktop Redesign

Counterpoints & Gaps

  • 60% 并非精确科学:这是 Ben 的经验值,不同任务类型、不同模型版本的实际衰退点可能不同
  • /compact 的盲点:坏的自动 compact 通常发生在模型无法预测下一步方向时——此时恰好是上下文腐化最严重、模型智能最低的时刻
  • Subagent 有开销:每次派生 Subagent 都有额外成本(token + 延迟),不适用于所有任务
  • 1M 上下文的反直觉:大多数人发布后的本能反应是"塞更多内容"。实际上,大窗口的价值在于给你更多主动管理的时间,而非允许你塞更多内容

关联页面


2026-04-28 更新:Auto Mode 的 Risk Router 设计

来源:AI 简报 2026-04-28-full

Anthropic 工程文章介绍了 auto mode 的双层架构,核心设计理念是 risk router 而非简单的"信任/不信任"二元开关:

  • 输入层:扫描工具输出中的 prompt-injection 尝试,在进入 agent 上下文前拦截
  • 行动层:分类器评估拟执行命令是否与任务对齐、是否安全到无需询问
  • 设计哲学:低风险、可恢复、与任务一致的动作顺畅通过;高风险动作需要明确审批+审计记录+失败边界
  • 关键启示:权限系统不该只有"完全信任 agent"和"不断打断人"两个选项,而应该是分级风险路由

Sources

Synthesized from 3 sources
  • 2026-04-18 My cheatsheet for a clean contextSupporting source listed by this page.Whole pagemediumbody
  • 2026-04-16 My cheatsheet for a clean contextSupporting source listed by this page.Whole pagemediumbody
  • Claude Code's Limits Are Generous. The Problem Is Your Harness.Supporting source listed by this page.Whole pagemediumbody

Evolution

1 event
  1. absorbed

    Derived from source material

    This page is currently synthesized from 3 sources.

    From 2026-04-18 My cheatsheet for a clean context, 2026-04-16 My cheatsheet for a clean context, Claude Code's Limits Are Generous. The Problem Is Your Harness.To Claude Code 上下文管理完整指南 — 2026-04-18 更新
    Sources: raw/newsletters/Ben's Bites/2026-04-18 My cheatsheet for a clean context.md · raw/newsletters/Ben's Bites/2026-04-16 My cheatsheet for a clean context.md · raw/to-learn/Claude Code's Limits Are Generous. The Problem Is Your Harness..md

Linked from