Context Optimization
Preserve task quality first. Optimize what is loaded and repeated; do not hide diagnostic evidence or install invisible interceptors by default.
1. Diagnose the source
Classify the dominant pressure:
- repeated instructions or duplicate skill metadata;
- long command, test, build, or search output;
- large files read in full instead of targeted excerpts;
- stale exploration that no longer affects the plan;
- repeated summaries with no new information;
- missing checkpoint, causing the same facts to be rediscovered.
Use context-budget for a corpus-wide inventory. Continue here for active-task reduction.
2. Reduce input before output
- Search first, then read only relevant ranges.
- Use file lists, counts, hashes, and structured summaries before opening full bodies.
- Batch independent lookups when the host supports it.
- Prefer machine-readable filters and selectors over dumping complete datasets.
- Load detailed references only when their branch of the task is active.
3. Control command output safely
- Prefer concise or quiet flags only when they preserve errors and exit status.
- During diagnosis, keep the first failing trace; shorten repeated successful output afterward.
- Capture large raw output in a file when authorized, then report counts, failures, and representative excerpts.
- Do not redirect stderr or suppress warnings that may explain a failure.
- Paginate or cap listings and state that output was truncated.
4. Create a loss-aware checkpoint
Before compaction, handoff, or a long phase transition, record:
- objective and acceptance criteria;
- decisions and why they were made;
- verified facts with source paths or links;
- changes already made and tests run;
- unresolved risks, failed attempts, and blockers;
- exact next actions;
- user preferences and prohibitions that still apply.
Keep raw evidence reachable by path instead of copying it into every summary.
5. Compress by relevance
Retain:
- safety and authorization boundaries;
- current plan and state;
- exact errors still under investigation;
- interfaces, schemas, invariants, and acceptance tests;
- provenance and uncertainty.
Summarize or drop:
- superseded hypotheses;
- repeated successful logs;
- boilerplate already available in a referenced file;
- duplicated source excerpts;
- exploration that cannot affect remaining decisions.
6. Verify after optimization
Check that the checkpoint can answer:
- What is being done and why?
- What has already changed?
- What evidence supports the current direction?
- What must not be changed or forgotten?
- What remains and how will completion be verified?
If any answer is missing, restore it before continuing.
Guardrails
- Do not claim a hidden global hook exists.
- Do not install command proxies, modify runtime hooks, or intercept all commands without explicit authorization and verified host support.
- Do not hardcode a context-window size, provider, or model family.
- Do not trade debuggability for fewer tokens.
- Do not rewrite user-owned files merely to save session context.
1---2name: context-optimization3description: Reduce active-session context bloat without losing decisions, errors, evidence, or verification state. Use when tool output is verbose, a long task is.4---56# Context Optimization78Preserve task quality first. Optimize what is loaded and repeated; do not hide diagnostic evidence or install invisible interceptors by default.910## 1. Diagnose the source1112Classify the dominant pressure:1314- repeated instructions or duplicate skill metadata;15- long command, test, build, or search output;16- large files read in full instead of targeted excerpts;17- stale exploration that no longer affects the plan;18- repeated summaries with no new information;19- missing checkpoint, causing the same facts to be rediscovered.2021Use context-budget for a corpus-wide inventory. Continue here for active-task reduction.2223## 2. Reduce input before output2425- Search first, then read only relevant ranges.26- Use file lists, counts, hashes, and structured summaries before opening full bodies.27- Batch independent lookups when the host supports it.28- Prefer machine-readable filters and selectors over dumping complete datasets.29- Load detailed references only when their branch of the task is active.3031## 3. Control command output safely3233- Prefer concise or quiet flags only when they preserve errors and exit status.34- During diagnosis, keep the first failing trace; shorten repeated successful output afterward.35- Capture large raw output in a file when authorized, then report counts, failures, and representative excerpts.36- Do not redirect stderr or suppress warnings that may explain a failure.37- Paginate or cap listings and state that output was truncated.3839## 4. Create a loss-aware checkpoint4041Before compaction, handoff, or a long phase transition, record:42431. objective and acceptance criteria;442. decisions and why they were made;453. verified facts with source paths or links;464. changes already made and tests run;475. unresolved risks, failed attempts, and blockers;486. exact next actions;497. user preferences and prohibitions that still apply.5051Keep raw evidence reachable by path instead of copying it into every summary.5253## 5. Compress by relevance5455Retain:5657- safety and authorization boundaries;58- current plan and state;59- exact errors still under investigation;60- interfaces, schemas, invariants, and acceptance tests;61- provenance and uncertainty.6263Summarize or drop:6465- superseded hypotheses;66- repeated successful logs;67- boilerplate already available in a referenced file;68- duplicated source excerpts;69- exploration that cannot affect remaining decisions.7071## 6. Verify after optimization7273Check that the checkpoint can answer:7475- What is being done and why?76- What has already changed?77- What evidence supports the current direction?78- What must not be changed or forgotten?79- What remains and how will completion be verified?8081If any answer is missing, restore it before continuing.8283## Guardrails8485- Do not claim a hidden global hook exists.86- Do not install command proxies, modify runtime hooks, or intercept all commands without explicit authorization and verified host support.87- Do not hardcode a context-window size, provider, or model family.88- Do not trade debuggability for fewer tokens.89- Do not rewrite user-owned files merely to save session context.