MR
Mayur Rathi
@muratcankoylan
⭐ 5 GitHub stars

Context Optimization

Apply compaction, masking, and caching strategies

mkdir -p ./skills/context-optimization && curl -sfL https://raw.githubusercontent.com/mayurrathi/awesome-agent-skills/main/skills/context-optimization/SKILL.md -o ./skills/context-optimization/SKILL.md

Run in terminal / PowerShell. Requires curl (Unix) or PowerShell 5+ (Windows).

Skill Content

When to Use This Skill


Apply compaction, masking, and caching strategies


Use this skill when working with apply compaction, masking, and caching strategies.

# Context Optimization Techniques


Context optimization extends the effective capacity of limited context windows through strategic compression, masking, caching, and partitioning. The goal is not to magically increase context windows but to make better use of available capacity. Effective optimization can double or triple effective context capacity without requiring larger models or longer contexts.


When to Activate


Activate this skill when:

- Context limits constrain task complexity

- Optimizing for cost reduction (fewer tokens = lower costs)

- Reducing latency for long conversations

- Implementing long-running agent systems

- Needing to handle larger documents or conversations

- Building production systems at scale


Core Concepts


Context optimization extends effective capacity through four primary strategies: compaction (summarizing context near limits), observation masking (replacing verbose outputs with references), KV-cache optimization (reusing cached computations), and context partitioning (splitting work across isolated contexts).


The key insight is that context quality matters more than quantity. Optimization preserves signal while reducing noise. The art lies in selecting what to keep versus what to discard, and when to apply each technique.


Detailed Topics


Compaction Strategies


**What is Compaction**

Compaction is the practice of summarizing context contents when approaching limits, then reinitializing a new context window with the summary. This distills the contents of a context window in a high-fidelity manner, enabling the agent to continue with minimal performance degradation.


Compaction typically serves as the first lever in context optimization. The art lies in selecting what to keep versus what to discard.


**Compaction Implementation**

Compaction works by identifying sections that can be compressed, generating summaries that capture essential points, and replacing full content with summaries. Priority for compression goes to tool outputs (replace with summaries), old turns (summarize early conversation), retrieved docs (summarize if recent versions exist), and never compress system prompt.


**Summary Generation**

Effective summaries preserve different elements depending on message type:


Tool outputs: Preserve key findings, metrics, and conclusions. Remove verbose raw output.


Conversational turns: Preserve key decisions, commitments, and context shifts. Remove filler and back-and-forth.


Retrieved documents: Preserve key facts and claims. Remove supporting evidence and elaboration.


Observation Masking


**The Observation Problem**

Tool outputs can comprise 80%+ of token usage in agent trajectories. Much of this is verbose output that has already served its purpose. Once an agent has used a tool output to make a decision, keeping the full output provides diminishing value while consuming significant context.


Observation masking replaces verbose tool outputs with compact references. The information remains accessible if needed but does not consume context continuously.


**Masking Strategy Selection**

Not all observations should be masked equally:


Never mask: Observations critical to current task, observations from the most recent turn, observations used in active reasoning.


Consider masking: Observations from 3+ turns ago, verbose outputs with key points extractable, observations whose purpose has been served.


Always mask: Repeated outputs, boilerplate headers/footers, outputs already summarized in conversation.


KV-Cache Optimization


**Understanding KV-Cache**

The KV-cache stores Key and Value tensors computed during inference, growing linearly with sequence length. Caching the KV-cache across requests sharing identical prefixes avoids recomputation.


Prefix caching reuses KV blocks across requests with identical p

🎯 Best For

  • Claude users
  • Software engineers
  • Development teams
  • Tech leads

💡 Use Cases

  • Code quality improvement
  • Best practice enforcement

📖 How to Use This Skill

  1. 1

    Install the Skill

    Copy the install command from the Terminal tab and run it. The SKILL.md file downloads to your local skills directory.

  2. 2

    Load into Your AI Assistant

    Open Claude and reference the skill. Paste the SKILL.md content or use the system prompt tab.

  3. 3

    Apply Context Optimization to Your Work

    Open your project in the AI assistant and ask it to apply the skill. Start with a small module to verify the output quality.

  4. 4

    Review and Refine

    Review AI suggestions before committing. Run tests, check for regressions, and iterate on the skill output.

❓ Frequently Asked Questions

Is Context Optimization compatible with Cursor and VS Code?

Yes — this skill works with any AI coding assistant including Cursor, VS Code with Copilot, and JetBrains IDEs.

Do I need specific dependencies for Context Optimization?

Check the install command and Works With section. Most code skills only require the AI assistant and your codebase.

How do I install Context Optimization?

Copy the install command from the Terminal tab and run it. The skill downloads to ./skills/context-optimization/SKILL.md, ready to use.

Can I customize this skill for my team?

Absolutely. Edit the SKILL.md file to add team-specific instructions, examples, or workflows.

⚠️ Common Mistakes to Avoid

Skipping validation

Always test AI-generated code changes, even for simple refactors.

Missing dependency updates

Check if the skill requires updated dependencies or new packages.

🔗 Related Skills