context-optimization

Optimize LLM context windows through compaction, observation masking, KV-cache strategy, and partitioning.

2|Updated May 30, 2026
One-click install
npx skills add https://github.com/virahitvin8/crafty-gis --skill context-optimization-virahitvin8
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: context-optimization
Source: https://github.com/virahitvin8/crafty-gis/tree/main/GIT_STAR/assignment-generator/antigravity-skills-main/antigravity-skills-main/skills/context-optimization
Command: npx skills add https://github.com/virahitvin8/crafty-gis --skill context-optimization-virahitvin8

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes scripts (resource) and references (resource) components.

What problem does it solve? Long-running AI agent sessions overflow limited context windows, driving up token costs, latency, and output quality degradation. This Skill provides concrete techniques and Python utilities to extend effective context capacity without switching to larger models. ## Core Features & Use Cases - Compaction & Summarization: Trigger category-aware summarization of tool outputs, conversation turns, and retrieved documents when utilization exceeds 70-80%. - Observation Masking: Replace verbose tool outputs with compact retrievable references using the ObservationStore class, achieving 60-80% reduction in masked content. - KV-Cache Optimization: Design stable prompt prefixes and measure cache hit rates to cut cost and latency on repeated requests. - Budget Management & Partitioning: Allocate token budgets per context category and split oversized tasks across sub-agents with isolated contexts. - Use Case: An agent loop processing hundreds of tool calls per session uses ContextBudget to detect 80% utilization, masks resolved observations, and compacts history before quality degrades. ## Quick Start Ask the AI to apply context optimization techniques to reduce token usage in an agent conversation that is approaching its context window limit.

Frequently Asked Questions about context-optimization

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I reduce token usage in a long LLM agent conversation?

Apply observation masking first to replace verbose tool outputs with compact references, then trigger compaction when utilization exceeds 70-80%. The ContextBudget class detects these thresholds and returns optimization reasons each agent loop iteration.

What is observation masking in context engineering?

Observation masking replaces verbose tool outputs with short references like "[Obs:id elided. Key: summary]" once their purpose is served. The ObservationStore class keeps full content retrievable by reference ID, achieving 60-80% reduction with minimal quality impact.

How do I improve KV-cache hit rates for LLM prompts?

Place stable content (system prompt, tool definitions, templates) at the prefix and dynamic content at the end. Remove timestamps and session IDs from system prompts, since even a single whitespace change invalidates the cached prefix downstream.

When should I compact context versus partition across sub-agents?

Compact when utilization crosses 70-80% and the task still fits one window. Partition only when estimated context exceeds 60% of the limit and at least 3 independent subtasks exist, since coordinator overhead can exceed savings on smaller tasks.

Why does compaction sometimes lose critical task information?

Compaction quality degrades when the summarizing model is itself under context pressure above 85% utilization. Trigger compaction at 70-80%, never compress the system prompt, and re-validate summaries against the current task goal afterward.

Does the token estimation in this skill work for production systems?

The built-in estimate uses a rough 4-characters-per-token heuristic for quick budget checks. Production systems should replace it with model-specific tokenizers such as tiktoken for OpenAI models or HuggingFace tokenizers for local models.