context-optimization

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

Updated Mar 26, 2026
One-click install
npx skills add https://github.com/LGJ-Jonathan/Jonathan_Global_Claude_Skills --skill context-optimization-lgj-jonathan
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: context-optimization
Source: https://github.com/LGJ-Jonathan/Jonathan_Global_Claude_Skills/tree/main/context-engineering/skills/context-optimization
Command: npx skills add https://github.com/LGJ-Jonathan/Jonathan_Global_Claude_Skills --skill context-optimization-lgj-jonathan

SYSTEM DOCUMENTATION & REQUIREMENTS

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

What problem does it solve? Long-running agents and LLM applications hit context window limits, driving up token costs, latency, and quality degradation. This Skill provides concrete techniques and Python utilities to extend effective context capacity without larger models. ## Core Features & Use Cases - Compaction & Summarization: Category-aware summarization of tool outputs, conversation turns, and retrieved documents, triggered at 70-80% utilization thresholds. - Observation Masking: Replace verbose tool outputs with compact retrievable references via the ObservationStore class, targeting 60-80% reduction in masked content. - KV-Cache Optimization: Stabilize prompt prefixes by removing timestamps and session IDs to maximize cache hit rates and cut cost and latency. - Context Budgeting & Partitioning: Allocate token budgets per category with ContextBudget, detect optimization triggers, and partition work across sub-agents when a single window cannot hold the task. - Use Case: An agent processing dozens of tool calls per session exceeds 80% context utilization; apply masking to old tool outputs, compact history, and stabilize the system prompt to continue without quality loss. ## Quick Start Ask the AI to optimize the context of a long agent conversation 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 references, then compact remaining history when utilization exceeds 70-80%. Stabilize prompt prefixes for KV-cache reuse to cut cost and latency without quality loss.

What is observation masking in context engineering?

Observation masking replaces verbose tool outputs with a compact reference like '[Obs:id elided. Key: summary. Full content retrievable.]' once their purpose is served. The full content is stored externally and remains retrievable, achieving 60-80% reduction in masked observations.

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

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

When should I trigger context compaction?

Trigger compaction at 70-80% context utilization, not at 90% or above. Compacting under extreme pressure degrades summary quality, causing loss of task goals and user constraints. Target 50-70% token reduction with under 5% quality degradation.

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

Partition when estimated task context exceeds 60% of the window limit and the task decomposes into at least 3 independent subtasks. For fewer subtasks, coordination overhead from coordinator prompts and result aggregation typically exceeds the token savings.

Why does masking break my agent's debugging loop?

Masking error outputs hides stack traces and failure details the agent needs in later turns to diagnose issues. During active debugging, suspend masking for all error-related observations until the issue is resolved.