context-optimizer

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Context Optimizer

上下文优化器

Manage your context window and token budget effectively.
有效管理你的上下文窗口和 Token 预算。

Quick Diagnosis

快速诊断

  1. Run
    /context
    to check current usage
  2. If > 70% → compact now before it degrades
  3. If > 90% → you're in the "dumb zone", compact immediately
  1. 运行
    /context
    检查当前使用量
  2. 如果 > 70% → 在情况恶化前立即压缩
  3. 如果 > 90% → 你已进入“低效区”,立即压缩

Optimization Strategies

优化策略

Immediate

立即执行

ActionSavesWhen
/compact
30-50% contextAt task boundaries
Disable unused MCPs~5% per MCPWhen switching domains
Use subagents for explorationKeeps main context cleanHeavy search/read tasks
Fresh session via
/resume
100% resetWhen starting unrelated work
操作可节省适用场景
/compact
30-50% 上下文任务边界处
禁用未使用的 MCP每个 MCP 约 5%切换领域时
使用 subagent 进行探索保持主上下文整洁搜索或读取任务量较大时
通过
/resume
开启全新会话
100% 重置开始无关工作时

Configuration

配置

Set proactive auto-compaction:
json
{
  "env": {
    "CLAUDE_AUTOCOMPACT_PCT_OVERRIDE": "50"
  }
}
设置主动自动压缩:
json
{
  "env": {
    "CLAUDE_AUTOCOMPACT_PCT_OVERRIDE": "50"
  }
}

MCP Audit

MCP 审计

Keep <10 MCPs enabled, <80 tools total. Each MCP adds overhead to every request.
bash
/mcp          # List active servers
启用的 MCP 应少于 10 个,工具总数应少于 80 个。每个 MCP 都会为每次请求增加开销。
bash
/mcp          # List active servers

Disable what you're not using

Disable what you're not using

undefined
undefined

Prompt Engineering for Token Efficiency

提升 Token 效率的 Prompt 工程

  • Scope your prompts: "In src/auth/, fix the login bug"
  • Provide constraints: "Don't modify the middleware"
  • Give acceptance criteria: "Should return 429 after 5 attempts"
  • Avoid vague prompts: "Fix the code" (forces Claude to read everything)
  • 限定 Prompt 范围:“在 src/auth/ 中,修复登录缺陷”
  • 提供约束:“不要修改 middleware”
  • 给出验收标准:“尝试 5 次后应返回 429”
  • 避免模糊 Prompt:“修复代码”(这会迫使 Claude 读取全部内容)

Subagent Delegation

Subagent 委派

Heavy operations that generate lots of output should go to subagents:
  • Test suite output → subagent
  • Large file exploration → subagent
  • Documentation generation → subagent
  • Log analysis → subagent
The main session stays clean while subagents handle the volume.
应将产生大量输出的高开销操作交给 subagent:
  • 测试套件输出 → subagent
  • 大型文件探索 → subagent
  • 文档生成 → subagent
  • 日志分析 → subagent
主会话保持整洁,由 subagent 处理大量输出。

Context Budget Planning

上下文预算规划

PhaseTarget UsageAction If Over
Planning< 20%Keep plans concise
Implementation< 60%Compact between files
Testing< 80%Delegate to subagent
Review< 90%Start fresh session
阶段目标使用率超出时的操作
规划< 20%保持方案简洁
实施< 60%在文件之间压缩上下文
测试< 80%委派给 subagent
审查< 90%开启全新会话

Token Efficiency

Token 效率

Output Reduction (40-60% savings)

输出精简(节省 40-60%)

  • No sycophantic openers ("Sure!", "Great question!")
  • No closing fluff ("Let me know if you need anything!")
  • No prompt restatement before answering
  • Code first, explanation only if non-obvious
  • Structured output (tables, bullets) over prose
  • ASCII only: -- not em dashes, straight quotes not smart quotes
  • 不使用谄媚式开场白(“Sure!”、“Great question!”)
  • 不使用收尾废话(“Let me know if you need anything!”)
  • 回答前不要复述 Prompt
  • 代码优先,仅在含义不直观时进行解释
  • 使用结构化输出(表格、项目符号)而非长篇文本
  • 仅使用 ASCII:使用 --,不要使用长破折号;使用直引号,不要使用智能引号

Behavioral Efficiency

行为效率

  • One-pass coding: complete solution, test once, stop if green
  • Read before write: never modify unread files
  • No re-reads: don't re-read unchanged files
  • Tool-call budgets: 20 (quick fix) to 80 (large feature)
  • Never iterate more than twice on the same failure
  • 一次完成编码:提供完整解决方案,测试一次,通过后停止
  • 先读后写:绝不修改未读取的文件
  • 不要重复读取:不要重新读取未变更的文件
  • 工具调用预算:快速修复为 20,大型功能为 80
  • 对同一失败绝不迭代超过两次

Task Profiles

任务类型

Switch response style based on context:
  • Coding: code first, minimal explanation, simplest solution
  • Agent/Pipeline: structured output only, no prose, parseable
  • Analysis: finding first, tables over paragraphs, sourced numbers
根据上下文切换响应风格:
  • 编码:代码优先,解释最少,采用最简单的解决方案
  • Agent/Pipeline:仅输出结构化内容,不要长篇说明,确保可解析
  • 分析:先给结论,表格优于段落,数字需有来源

CLAUDE.md Optimization

CLAUDE.md 优化

  • Root CLAUDE.md: < 60 lines ideal, < 150 max
  • Move package-specific info to package-level CLAUDE.md
  • Move personal preferences to CLAUDE.local.md
  • Remove obvious or rapidly-changing information
  • 根目录 CLAUDE.md:理想情况下少于 60 行,最多少于 150 行
  • 将包特定信息移到包级 CLAUDE.md
  • 将个人偏好移到 CLAUDE.local.md
  • 移除显而易见或快速变化的信息

When Context Is Degraded

上下文退化时

Signs:
  • Claude repeats itself or forgets earlier context
  • Responses become generic or lose project-specific knowledge
  • Tool calls start failing for reasons that worked earlier
Fix:
  1. Manual
    /compact
  2. If still bad: new session with
    /resume
  3. For recurring issues: reduce CLAUDE.md size, disable MCPs
迹象:
  • Claude 重复自身内容或忘记之前的上下文
  • 响应变得泛化或丢失项目特定知识
  • 此前正常的工具调用开始失败
修复方法:
  1. 手动执行
    /compact
  2. 如果仍然不佳:通过
    /resume
    开启新会话
  3. 如果问题反复出现:缩减 CLAUDE.md 大小,禁用 MCP