This skill should be used when the user asks to "compress context", "summarize conversation history", "implement compaction", "reduce token usage", or mentions context compression, structured summarization, tokens-per-task optimization, or long-running agent sessions exceeding context limits.
Add this skill
npx mdskills install muratcankoylan/context-compressionComprehensive guide to context compression with structured strategies, evaluation frameworks, and practical examples
1---2name: context-compression3description: This skill should be used when the user asks to "compress context", "summarize conversation history", "implement compaction", "reduce token usage", or mentions context compression, structured summarization, tokens-per-task optimization, or long-running agent sessions exceeding context limits.4---56# Context Compression Strategies78When agent sessions generate millions of tokens of conversation history, compression becomes mandatory. The naive approach is aggressive compression to minimize tokens per request. The correct optimization target is tokens per task: total tokens consumed to complete a task, including re-fetching costs when compression loses critical information.910## When to Activate1112Activate this skill when:13- Agent sessions exceed context window limits14- Codebases exceed context windows (5M+ token systems)15- Designing conversation summarization strategies16- Debugging cases where agents "forget" what files they modified17- Building evaluation frameworks for compression quality1819## Core Concepts2021Context compression trades token savings against information loss. Three production-ready approaches exist:22231. **Anchored Iterative Summarization**: Maintain structured, persistent summaries with explicit sections for session intent, file modifications, decisions, and next steps. When compression triggers, summarize only the newly-truncated span and merge with the existing summary. Structure forces preservation by dedicating sections to specific information types.24252. **Opaque Compression**: Produce compressed representations optimized for reconstruction fidelity. Achieves highest compression ratios (99%+) but sacrifices interpretability. Cannot verify what was preserved.26273. **Regenerative Full Summary**: Generate detailed structured summaries on each compression. Produces readable output but may lose details across repeated compression cycles due to full regeneration rather than incremental merging.2829The critical insight: structure forces preservation. Dedicated sections act as checklists that the summarizer must populate, preventing silent information drift.3031## Detailed Topics3233### Why Tokens-Per-Task Matters3435Traditional compression metrics target tokens-per-request. This is the wrong optimization. When compression loses critical details like file paths or error messages, the agent must re-fetch information, re-explore approaches, and waste tokens recovering context.3637The right metric is tokens-per-task: total tokens consumed from task start to completion. A compression strategy saving 0.5% more tokens but causing 20% more re-fetching costs more overall.3839### The Artifact Trail Problem4041Artifact trail integrity is the weakest dimension across all compression methods, scoring 2.2-2.5 out of 5.0 in evaluations. Even structured summarization with explicit file sections struggles to maintain complete file tracking across long sessions.4243Coding agents need to know:44- Which files were created45- Which files were modified and what changed46- Which files were read but not changed47- Function names, variable names, error messages4849This problem likely requires specialized handling beyond general summarization: a separate artifact index or explicit file-state tracking in agent scaffolding.5051### Structured Summary Sections5253Effective structured summaries include explicit sections:5455```markdown56## Session Intent57[What the user is trying to accomplish]5859## Files Modified60- auth.controller.ts: Fixed JWT token generation61- config/redis.ts: Updated connection pooling62- tests/auth.test.ts: Added mock setup for new config6364## Decisions Made65- Using Redis connection pool instead of per-request connections66- Retry logic with exponential backoff for transient failures6768## Current State69- 14 tests passing, 2 failing70- Remaining: mock setup for session service tests7172## Next Steps731. Fix remaining test failures742. Run full test suite753. Update documentation76```7778This structure prevents silent loss of file paths or decisions because each section must be explicitly addressed.7980### Compression Trigger Strategies8182When to trigger compression matters as much as how to compress:8384| Strategy | Trigger Point | Trade-off |85|----------|---------------|-----------|86| Fixed threshold | 70-80% context utilization | Simple but may compress too early |87| Sliding window | Keep last N turns + summary | Predictable context size |88| Importance-based | Compress low-relevance sections first | Complex but preserves signal |89| Task-boundary | Compress at logical task completions | Clean summaries but unpredictable timing |9091The sliding window approach with structured summaries provides the best balance of predictability and quality for most coding agent use cases.9293### Probe-Based Evaluation9495Traditional metrics like ROUGE or embedding similarity fail to capture functional compression quality. A summary may score high on lexical overlap while missing the one file path the agent needs.9697Probe-based evaluation directly measures functional quality by asking questions after compression:9899| Probe Type | What It Tests | Example Question |100|------------|---------------|------------------|101| Recall | Factual retention | "What was the original error message?" |102| Artifact | File tracking | "Which files have we modified?" |103| Continuation | Task planning | "What should we do next?" |104| Decision | Reasoning chain | "What did we decide about the Redis issue?" |105106If compression preserved the right information, the agent answers correctly. If not, it guesses or hallucinates.107108### Evaluation Dimensions109110Six dimensions capture compression quality for coding agents:1111121. **Accuracy**: Are technical details correct? File paths, function names, error codes.1132. **Context Awareness**: Does the response reflect current conversation state?1143. **Artifact Trail**: Does the agent know which files were read or modified?1154. **Completeness**: Does the response address all parts of the question?1165. **Continuity**: Can work continue without re-fetching information?1176. **Instruction Following**: Does the response respect stated constraints?118119Accuracy shows the largest variation between compression methods (0.6 point gap). Artifact trail is universally weak (2.2-2.5 range).120121## Practical Guidance122123### Three-Phase Compression Workflow124125For large codebases or agent systems exceeding context windows, apply compression through three phases:1261271. **Research Phase**: Produce a research document from architecture diagrams, documentation, and key interfaces. Compress exploration into a structured analysis of components and dependencies. Output: single research document.1281292. **Planning Phase**: Convert research into implementation specification with function signatures, type definitions, and data flow. A 5M token codebase compresses to approximately 2,000 words of specification.1301313. **Implementation Phase**: Execute against the specification. Context remains focused on the spec rather than raw codebase exploration.132133### Using Example Artifacts as Seeds134135When provided with a manual migration example or reference PR, use it as a template to understand the target pattern. The example reveals constraints that static analysis cannot surface: which invariants must hold, which services break on changes, and what a clean migration looks like.136137This is particularly important when the agent cannot distinguish essential complexity (business requirements) from accidental complexity (legacy workarounds). The example artifact encodes that distinction.138139### Implementing Anchored Iterative Summarization1401411. Define explicit summary sections matching your agent's needs1422. On first compression trigger, summarize truncated history into sections1433. On subsequent compressions, summarize only new truncated content1444. Merge new summary into existing sections rather than regenerating1455. Track which information came from which compression cycle for debugging146147### When to Use Each Approach148149**Use anchored iterative summarization when:**150- Sessions are long-running (100+ messages)151- File tracking matters (coding, debugging)152- You need to verify what was preserved153154**Use opaque compression when:**155- Maximum token savings required156- Sessions are relatively short157- Re-fetching costs are low158159**Use regenerative summaries when:**160- Summary interpretability is critical161- Sessions have clear phase boundaries162- Full context review is acceptable on each compression163164### Compression Ratio Considerations165166| Method | Compression Ratio | Quality Score | Trade-off |167|--------|-------------------|---------------|-----------|168| Anchored Iterative | 98.6% | 3.70 | Best quality, slightly less compression |169| Regenerative | 98.7% | 3.44 | Good quality, moderate compression |170| Opaque | 99.3% | 3.35 | Best compression, quality loss |171172The 0.7% additional tokens retained by structured summarization buys 0.35 quality points. For any task where re-fetching costs matter, this trade-off favors structured approaches.173174## Examples175176**Example 1: Debugging Session Compression**177178Original context (89,000 tokens, 178 messages):179- 401 error on /api/auth/login endpoint180- Traced through auth controller, middleware, session store181- Found stale Redis connection182- Fixed connection pooling, added retry logic183- 14 tests passing, 2 failing184185Structured summary after compression:186187```markdown188## Session Intent189Debug 401 Unauthorized error on /api/auth/login despite valid credentials.190191## Root Cause192Stale Redis connection in session store. JWT generated correctly but session could not be persisted.193194## Files Modified195- auth.controller.ts: No changes (read only)196- middleware/cors.ts: No changes (examined)197- config/redis.ts: Fixed connection pooling configuration198- services/session.service.ts: Added retry logic for transient failures199- tests/auth.test.ts: Updated mock setup200201## Test Status20214 passing, 2 failing (mock setup issues)203204## Next Steps2051. Fix remaining test failures (mock session service)2062. Run full test suite2073. Deploy to staging208```209210**Example 2: Probe Response Quality**211212After compression, asking "What was the original error?":213214Good response (structured summarization):215> "The original error was a 401 Unauthorized response from the /api/auth/login endpoint. Users received this error with valid credentials. Root cause was stale Redis connection in session store."216217Poor response (aggressive compression):218> "We were debugging an authentication issue. The login was failing. We fixed some configuration problems."219220The structured response preserves endpoint, error code, and root cause. The aggressive response loses all technical detail.221222## Guidelines2232241. Optimize for tokens-per-task, not tokens-per-request2252. Use structured summaries with explicit sections for file tracking2263. Trigger compression at 70-80% context utilization2274. Implement incremental merging rather than full regeneration2285. Test compression quality with probe-based evaluation2296. Track artifact trail separately if file tracking is critical2307. Accept slightly lower compression ratios for better quality retention2318. Monitor re-fetching frequency as a compression quality signal232233## Integration234235This skill connects to several others in the collection:236237- context-degradation - Compression is a mitigation strategy for degradation238- context-optimization - Compression is one optimization technique among many239- evaluation - Probe-based evaluation applies to compression testing240- memory-systems - Compression relates to scratchpad and summary memory patterns241242## References243244Internal reference:245- [Evaluation Framework Reference](./references/evaluation-framework.md) - Detailed probe types and scoring rubrics246247Related skills in this collection:248- context-degradation - Understanding what compression prevents249- context-optimization - Broader optimization strategies250- evaluation - Building evaluation frameworks251252External resources:253- Factory Research: Evaluating Context Compression for AI Agents (December 2025)254- Research on LLM-as-judge evaluation methodology (Zheng et al., 2023)255- Netflix Engineering: "The Infinite Software Crisis" - Three-phase workflow and context compression at scale (AI Summit 2025)256257---258259## Skill Metadata260261**Created**: 2025-12-22262**Last Updated**: 2025-12-26263**Author**: Agent Skills for Context Engineering Contributors264**Version**: 1.1.0265266
Full transparency — inspect the skill content before installing.