context-optimization

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

1|Updated May 16, 2026
One-click install
npx skills add https://github.com/ludotype/game-adventurertown --skill context-optimization-ludotype
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: context-optimization
Source: https://github.com/ludotype/game-adventurertown/tree/main/.agent/skills/context-optimization
Command: npx skills add https://github.com/ludotype/game-adventurertown --skill context-optimization-ludotype

SYSTEM DOCUMENTATION & REQUIREMENTS

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

What problem does it solve? Long-running agents and conversations quickly exhaust limited context windows, 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 a prioritized order with measurable performance targets. - Ready-to-Use Python Utilities: Estimate token counts, mask verbose tool outputs with retrievable references, manage token budgets, and design cache-stable prompts via the included scripts. - Use Case: Imagine an agent whose tool outputs consume over 50% of its context. Use this Skill to mask stale observations with compact references, trigger compaction at 70% utilization, and restructure prompts for 70%+ KV-cache hit rates. ## Quick Start Use the context-optimization skill to reduce token usage in my agent loop by masking old tool outputs and setting up a context budget.

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-running AI agent?▼

Apply observation masking first to replace verbose tool outputs with compact retrievable references, then trigger compaction when utilization exceeds 70%. Structure prompts with stable prefixes to maximize KV-cache reuse, and partition work across sub-agents only when a single window cannot hold the task.

What is observation masking in agent context management?▼

Observation masking replaces verbose tool outputs with compact references like '[Obs:{ref_id} elided. Key: {summary}]' once their purpose is served. The full content is stored externally and remains retrievable, achieving 60-80% reduction in masked observations with under 2% quality impact.

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

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

When should I trigger context compaction?▼

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

When should I not partition a task across sub-agents?▼

Avoid partitioning when the task has fewer than 3 independent subtasks, because each sub-agent requires its own system prompt, tool definitions, and coordination messages. For small tasks, this coordination overhead often exceeds the context 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.