context-optimization

Reduce LLM context token usage through masking, compaction, caching, and partitioning.

Updated Aug 7, 2026
One-click install
npx skills add https://github.com/Sambhav242005/Major-Project --skill context-optimization-sambhav242005
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: context-optimization
Source: https://github.com/Sambhav242005/Major-Project/tree/main/.agents/skills/context-optimization
Command: npx skills add https://github.com/Sambhav242005/Major-Project --skill context-optimization-sambhav242005

SYSTEM DOCUMENTATION & REQUIREMENTS

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

What problem does it solve? Long-running agent sessions overflow limited context windows, driving up token costs, latency, and quality degradation. This Skill provides concrete techniques and utilities to extend effective context capacity without lowering answer quality. ## Core Features & Use Cases - Observation Masking: Replace verbose tool outputs with compact retrievable references, achieving 60-80% reduction in masked observations. - Compaction & Budgeting: Trigger summarization at 70-80% utilization with category-aware strategies and explicit token budget allocation via the ContextBudget class. - KV-Cache Optimization: Structure prompts with stable prefixes to reach 70%+ cache hit rates and cut cost and latency. - Use Case: An agent loop where tool outputs consume over half the context window uses ObservationStore to mask stale outputs and ContextBudget to trigger compaction before quality degrades. ## Quick Start Ask the AI to analyze your agent's context usage and apply observation masking and budget-triggered compaction using the provided Python utilities.

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 references, then trigger compaction when utilization exceeds 70-80%. The ContextBudget class monitors usage per category and signals when optimization should fire.

How to improve KV-cache hit rate for LLM prompts?

Place stable content first in the prompt: system prompt, tool definitions, templates, then dynamic content last. Remove timestamps and session IDs from the prefix, since even a single whitespace change invalidates the cached block downstream.

When should I compact context versus mask observations?

Mask first when tool outputs dominate context, since masking removes low-value bulk with under 2% quality impact. Compact afterward when total utilization still exceeds 70%, summarizing remaining turns while preserving decisions and task state.

Why does my prompt caching stop working after deployment?

Cache misses spike when prompt prefixes change between deployments, such as reworded system prompts or reordered tools. The entire cached block downstream of any change is invalidated, so roll out prompt changes gradually and monitor hit rates.

What are the limitations of context partitioning across sub-agents?

Partitioning adds coordination overhead: each sub-agent needs its own system prompt, tools, and result aggregation. For tasks with fewer than 3 independent subtasks, the overhead often exceeds the token savings, so estimate total cost first.

How accurate is the token estimation in the scripts?

The estimate_token_count function uses a rough heuristic of about 4 characters per token for English. Production systems should use model-specific tokenizers like tiktoken for OpenAI models or HuggingFace tokenizers for local models.