context-optimization
What it does
This skill covers ways to fit more useful work into a limited context window, such as hiding verbose tool output, reusing cached prompt prefixes and splitting work across agents. It is for teams trying to lower AI costs or run bigger tasks.
When to use it
- βWhen a task runs out of context space or costs too many tokens.
- βWhen long tool outputs fill the context with details the agent rarely needs.
- βWhen you want to reuse cached parts of a prompt to cut repeated costs.
Skill link
https://github.com/muratcankoylan/Agent-Skills-for-Context-Engineering/blob/main/skills/context-optimization/SKILL.mdCopy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.
How to use
The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.
Ready-to-paste message
For Claude Code, Codex or Cursor: run it in your project folder and the skill gets installed.
Download the SKILL.md and drop it into your agentβs skills folder.
Example of what to ask your AI
βMy agent's tool outputs fill up the context too fast. Show me how to keep the conversation lean without losing quality.β
5 new skills a week. 1 minute to read.
How it works
The skill ranks techniques by impact and risk, such as masking outputs, caching the start of prompts and splitting work, and says to measure each change.
Technical content (original SKILL.md)
Original text by the author, straight from GitHub.
Context Optimization Techniques
Context optimization extends the effective capacity of limited context windows through strategic compression, masking, caching, and partitioning. Effective optimization increases useful capacity without requiring larger models or longer windows β but only when applied with measurement discipline. The techniques below are ordered by impact and risk.
When to Activate
Activate this skill when:
- Context budgets or token costs constrain task complexity
- Observation masking can replace verbose tool outputs with retrievable references
- Prefix or KV-cache hit rate needs improvement
- Retrieval scoping can reduce irrelevant loaded context
- Context partitioning can extend effective capacity across agents
- Budget triggers are needed for masking, compaction, or partitioning
Do not activate this skill for adjacent work owned by other skills:
- Explaining why attention or context windows behave this way:
context-fundamentals. - Diagnosing active lost-in-middle, poisoning, distraction, confusion, or clash:
context-degradation. - Designing a structured handoff summary for a long conversation:
context-compression. - Storing large outputs, plans, or logs as files:
filesystem-context. - Model-initiated rewrites of its own context and their re-prefill cost:
self-managed-context.
Core Concepts
Apply four primary strategies in this priority order:
KV-cache optimization β Reorder and stabilize prompt structure so the inference engine reuses cached Key/Value tensors. This is the cheapest optimization when the runtime supports prefix caching: low quality risk, immediate cost and latency savings. Apply it first when stable prefixes exist.
Observation masking β Replace verbose tool outputs with compact references once their purpose has been served. Tool outputs can dominate agent trajectories (claim-context-optimization-tool-output-dominance), so masking often yields the largest capacity gains. The original content remains retrievable if needed downstream.
Compaction β Summarize accumulated context when utilization exceeds 70%, then reinitialize with the summary. This distills the window's contents while preserving task-critical state. Compaction is lossy β apply it after masking has already removed the low-value bulk.
Context partitioning β Split work across sub-agents with isolated contexts when a single window cannot hold the full problem. Each sub-agent operates in a clean context focused on its subtask. Reserve this for tasks where estimated context exceeds 60% of the window limit, because coordination overhead is real.
The governing principle: context quality matters more than quantity. Every optimization preserves signal while reducing noise. Measure before optimizing, then measure the optimization's effect.
Detailed Topics
Compaction Strategies
Trigger compaction when context utilization exceeds 70%: summarize the current context, then reinitialize with the summary. This distills the window's contents in a high-fidelity manner, enabling continuation with minimal performance degradation. Prioritize compressing tool outputs first (they consume 80%+ of tokens), then old conversation turns, then retrieved documents. Never compress the system prompt β it anchors model behavior and its removal causes unpredictable degradation.
Preserve different elements by message type:
- Tool outputs: Extract key findings, metrics, error codes, and conclusions. Strip verbose raw output, stack traces (unless debugging is ongoing), and boilerplate headers.
- Conversational turns: Retain decisions, commitments, user preferences, and context shifts. Remove filler, pleasantries, and exploratory back-and-forth that led to a conclusion already captured.
- Retrieved documents: Keep claims, facts, and data points relevant to the active task. Remove supporting evidence and elaboration that served a one-time reasoning purpose.
Target 50-70% token reduction with less than 5% quality degradation. If compaction exceeds 70% reduction, audit the summary for critical information loss β over-aggressive compaction is the most common failure mode.
Observation Masking
Mask observations selectively based on recency and ongoing relevance β not uniformly. Apply these rules:
- Never mask: Observations critical to the current task, observations from the most recent turn, observations used in active reasoning chains, and error outputs when debugging is in
FAQ
What is observation masking?
It replaces long tool outputs with short references the agent can look up again when needed.
What is prefix caching?
It reuses the unchanging start of a prompt so the model does not reprocess it each time.
Does this reduce answer quality?
The skill says to measure each change, so a technique is kept only if the answers stay good.
Details
Original description by the author
This skill should be used for improving context efficiency: context budgeting, observation masking, prefix or KV-cache strategy, partitioning, token-cost reduction, retrieval scoping, and extending effective context capacity without lowering answer quality.
Popularity
- GitHub stars
- 18k
- Ranking position
- #10640
- License
- MIT
- Updated
- 10/11/2026
Comments
- β¦
More skills from this repository
context-engineering-collection
18kHelps your agent manage what goes into an AI model's context window, from memory and tools to multi-agent setups and evaluation.
advanced-evaluation
18kTeaches your agent how to judge AI answers using another AI, with scoring, side-by-side comparison and bias checks.
bdi-mental-states
18kTeaches your agent to model beliefs, goals and plans using the BDI approach, so its decisions can be traced and explained.
context-compression
18kHelps your agent shorten long AI sessions into summaries that keep decisions, files, risks and next steps.
Similar skills
add-model-provider
394kLets your agent connect a new AI model provider to OpenClaw using safe setup commands, keep API keys out of plain text, and run a live test.
acp-router
394kHelps your agent send a task to a coding assistant such as Claude Code or Cursor through OpenClaw, so the work runs in that tool.
browser-automation
394kTeaches your agent to control web pages step by step, including logins, tabs, and recovery when a page stops responding.