context_mastery

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Context Mastery & Token Optimization Protocol

上下文精通与Token优化协议

1. The "Grep-First" Rule (Token Saver)

1. “Grep优先”规则(Token节省方案)

  • Problem:
    read_file
    on a 2000-line file consumes ~500-1000 tokens instantly.
  • Solution: Always use
    grep
    or
    read_file_range
    first to inspect specific functions or imports.
  • Banned:
    cat
    or
    read_file
    on entire directories or massive files (e.g.,
    package-lock.json
    , giant logs) unless absolutely necessary.
  • 问题:对2000行文件执行
    read_file
    会立即消耗约500-1000个Token。
  • 解决方案:始终优先使用
    grep
    read_file_range
    来检查特定函数或导入内容。
  • 禁止操作:除非绝对必要,否则不要对整个目录或超大文件(如
    package-lock.json
    、大型日志文件)执行
    cat
    read_file

2. Incremental Summarization

2. 增量摘要

  • Trigger: When the conversation exceeds ~20 turns or you feel the context window filling.
  • Action:
    1. Summarize what has been achieved in the last 10 turns.
    2. Write it to
      scratchpad.md
      or
      active_task.md
      .
    3. Explicitly state "I am clearing internal context of validated steps" (if tool allows) or just rely on the scratchpad for future lookup.
  • 触发条件:当对话超过约20轮,或你感觉到上下文窗口即将被占满时。
  • 操作步骤
    1. 总结最近10轮对话中已完成的工作。
    2. 将总结内容写入
      scratchpad.md
      active_task.md
    3. 明确声明“我正在清除已验证步骤的内部上下文”(如果工具支持的话),或者后续直接依赖草稿文件进行查阅。

3. Focused Context Loading

3. 聚焦式上下文加载

  • Don't: "Read all files in
    src/
    to understand the project." (Too expensive).
  • Do:
    1. Read
      CLAUDE.md
      /
      README.md
      .
    2. Read directory listing
      ls -R
      .
    3. Selectively read only the interfaces (
      types.ts
      ) or entry points (
      index.ts
      ) related to the current task.
  • 避免操作:“读取
    src/
    目录下的所有文件以了解项目”(成本过高)。
  • 推荐操作
    1. 读取
      CLAUDE.md
      /
      README.md
    2. 读取目录列表
      ls -R
    3. 仅选择性地读取与当前任务相关的接口文件(如
      types.ts
      )或入口文件(如
      index.ts
      )。

4. Context Efficiency Checklist

4. 上下文效率检查表

  • Did I use
    grep
    instead of reading the whole file?
  • Did I check if I already have this info in
    MEMORY.md
    ?
  • Is my next prompt efficient? (Avoid repeating massive code blocks back to the user).
  • 我是否使用
    grep
    替代了读取整个文件?
  • 我是否检查过
    MEMORY.md
    中已有相关信息?
  • 我的下一个提示词是否高效?(避免向用户重复大段代码块)