$npx -y skills add muratcankoylan/Agent-Skills-for-Context-Engineering --skill context-optimizationThis skill should be used for improving context efficiency: context budgeting, observation masking, prefix or KV-cache strategy, partitioning, token-cost reduction, retrieval scoping, and extending effective context capacity without lowering answer quality.
| 1 | # Context Optimization Techniques |
| 2 | |
| 3 | Context optimization extends the effective capacity of limited context windows through strategic compression, masking, caching, and partitioning. Effective optimization increases useful capacity without requiring larger models or longer windows — but only when applied with measurement discipline. The techniques below are ordered by impact and risk. |
| 4 | |
| 5 | ## When to Activate |
| 6 | |
| 7 | Activate this skill when: |
| 8 | - Context budgets or token costs constrain task complexity |
| 9 | - Observation masking can replace verbose tool outputs with retrievable references |
| 10 | - Prefix or KV-cache hit rate needs improvement |
| 11 | - Retrieval scoping can reduce irrelevant loaded context |
| 12 | - Context partitioning can extend effective capacity across agents |
| 13 | - Budget triggers are needed for masking, compaction, or partitioning |
| 14 | |
| 15 | Do not activate this skill for adjacent work owned by other skills: |
| 16 | - Explaining why attention or context windows behave this way: `context-fundamentals`. |
| 17 | - Diagnosing active lost-in-middle, poisoning, distraction, confusion, or clash: `context-degradation`. |
| 18 | - Designing a structured handoff summary for a long conversation: `context-compression`. |
| 19 | - Storing large outputs, plans, or logs as files: `filesystem-context`. |
| 20 | |
| 21 | ## Core Concepts |
| 22 | |
| 23 | Apply four primary strategies in this priority order: |
| 24 | |
| 25 | 1. **KV-cache optimization** — Reorder and stabilize prompt structure so the inference engine reuses cached Key/Value tensors. This is the cheapest optimization when the runtime supports prefix caching: low quality risk, immediate cost and latency savings. Apply it first when stable prefixes exist. |
| 26 | |
| 27 | 2. **Observation masking** — Replace verbose tool outputs with compact references once their purpose has been served. Tool outputs can dominate agent trajectories (claim-context-optimization-tool-output-dominance), so masking often yields the largest capacity gains. The original content remains retrievable if needed downstream. |
| 28 | |
| 29 | 3. **Compaction** — Summarize accumulated context when utilization exceeds 70%, then reinitialize with the summary. This distills the window's contents while preserving task-critical state. Compaction is lossy — apply it after masking has already removed the low-value bulk. |
| 30 | |
| 31 | 4. **Context partitioning** — Split work across sub-agents with isolated contexts when a single window cannot hold the full problem. Each sub-agent operates in a clean context focused on its subtask. Reserve this for tasks where estimated context exceeds 60% of the window limit, because coordination overhead is real. |
| 32 | |
| 33 | The governing principle: context quality matters more than quantity. Every optimization preserves signal while reducing noise. Measure before optimizing, then measure the optimization's effect. |
| 34 | |
| 35 | ## Detailed Topics |
| 36 | |
| 37 | ### Compaction Strategies |
| 38 | |
| 39 | Trigger compaction when context utilization exceeds 70%: summarize the current context, then reinitialize with the summary. This distills the window's contents in a high-fidelity manner, enabling continuation with minimal performance degradation. Prioritize compressing tool outputs first (they consume 80%+ of tokens), then old conversation turns, then retrieved documents. Never compress the system prompt — it anchors model behavior and its removal causes unpredictable degradation. |
| 40 | |
| 41 | Preserve different elements by message type: |
| 42 | |
| 43 | - **Tool outputs**: Extract key findings, metrics, error codes, and conclusions. Strip verbose raw output, stack traces (unless debugging is ongoing), and boilerplate headers. |
| 44 | - **Conversational turns**: Retain decisions, commitments, user preferences, and context shifts. Remove filler, pleasantries, and exploratory back-and-forth that led to a conclusion already captured. |
| 45 | - **Retrieved documents**: Keep claims, facts, and data points relevant to the active task. Remove supporting evidence and elaboration that served a one-time reasoning purpose. |
| 46 | |
| 47 | Target 50-70% token reduction with less than 5% quality degradation. If compaction exceeds 70% reduction, audit the summary for critical information loss — over-aggressive compaction is the most common failure mode. |
| 48 | |
| 49 | ### Observation Masking |
| 50 | |
| 51 | Mask observations selectively based on recency and ongoing relevance — not uniformly. Apply these rules: |
| 52 | |
| 53 | - **Never mask**: Observations critical to the current task, observations from the most recent turn, observations used in active reasoning chains, and error outputs when debugging is in progress. |
| 54 | - **Mask after 3+ turns**: Verbose outputs whose key points have already been extracted into the conversation flow. Replace with a compact reference: `[Obs:{ref_id} elided. Key: {summary}. Full content retrievable.]` |
| 55 | - **Always mask immediately**: Repeated/duplicate outputs, b |