context-optimization

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

Updated Apr 7, 2026
One-click install
npx skills add https://github.com/NT-boop-star/BRMV-tract --skill context-optimization-nt-boop-star
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: context-optimization
Source: https://github.com/NT-boop-star/BRMV-tract/tree/main/antigravity/skills/context-optimization
Command: npx skills add https://github.com/NT-boop-star/BRMV-tract --skill context-optimization-nt-boop-star

SYSTEM DOCUMENTATION & REQUIREMENTS

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

What problem does it solve? Long-running agents and conversations hit context window limits, driving up token costs, latency, and quality degradation. This Skill provides concrete techniques and utilities to extend effective context capacity without larger models. ## Core Features & Use Cases - Four Optimization Strategies: Apply KV-cache optimization, observation masking, compaction, and context partitioning in priority order with measurable targets. - Ready-to-Use Utilities: Python helpers for token estimation, category-aware summarization, observation masking with retrievable references, budget tracking, and cache metrics. - Use Case: An agent processing dozens of tool calls sees context utilization cross 80%. Use the ContextBudget class to detect the threshold, mask stale tool outputs via ObservationStore, and compact remaining history to recover 50-70% of the window. ## Quick Start Ask the AI to optimize your agent's context usage by masking old tool outputs and compacting history when utilization exceeds 80%.

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 long LLM agent conversations?

Apply observation masking first to replace verbose tool outputs with compact retrievable references, then compact remaining history when utilization exceeds 70-80%. Stabilize prompt prefixes to maximize KV-cache hits for additional cost and latency savings.

What is KV-cache optimization for LLM prompts?

KV-cache optimization orders prompts so stable content (system prompt, tool definitions) forms a reusable prefix while dynamic content goes last. Identical prefixes reuse cached tensors, targeting 70%+ hit rates and 50%+ cost reduction on cached tokens.

When should I compact context versus mask observations?

Mask observations first when tool outputs dominate context, since masking removes low-value bulk with under 2% quality impact. Compact afterward when utilization still exceeds 70%, targeting 50-70% token reduction with under 5% quality degradation.

Why does my KV-cache hit rate drop after prompt changes?

Any change in the prompt prefix, even a single whitespace or an interpolated timestamp, invalidates the entire cached block downstream. Pin system prompts as immutable strings and move dynamic metadata into user messages or tool results.

When should I partition work across sub-agents instead of compacting?

Partition when estimated task context exceeds 60% of the window and the task decomposes into at least 3 independent subtasks. Below that threshold, coordinator overhead typically exceeds the token savings from isolation.

What are the limitations of heuristic token counting in this Skill?

The estimate_token_count function uses a rough 4-characters-per-token heuristic that varies by model, content type, and language. Production systems should use model-specific tokenizers such as tiktoken for OpenAI models or HuggingFace tokenizers for local models.