Skip to content
Back to skills

Context Compression

ASecurity

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 stars
  • 0 votes
  • 0 copies
  • 4 views
  • Added June 4, 2026
code-qualitygodebugging

Security analysis

A100/100

Pro scans all 3 files and shows the line behind each finding

Scanned June 4, 2026

npx -y skills add lidge-jun/cli-jaw-skills --skill context-compression --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Context Compression?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Context Compression
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/lidge-jun-context-compression/badge)](https://www.skillsdirectory.com/skills/lidge-jun-context-compression)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: context-compression
description: 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.
---

# Context Compression Strategies

When agent sessions generate millions of tokens of conversation history, compression becomes mandatory. Optimize for tokens per task (total tokens to complete a task, including re-fetching costs), not tokens per request.

## When to Activate

- Agent sessions exceeding context window limits
- Codebases exceeding context windows (5M+ token systems)
- Designing conversation summarization strategies
- Debugging cases where agents "forget" modified files

## Three Approaches

1. **Anchored Iterative Summarization**: Maintain structured, persistent summaries with explicit sections for session intent, file modifications, decisions, and next steps. On compression, summarize only the newly-truncated span and merge with existing summary. Structure forces preservation by dedicating sections to specific information types.

2. **Opaque Compression**: Produce compressed representations optimized for reconstruction fidelity. Achieves highest compression ratios (99%+) but sacrifices interpretability.

3. **Regenerative Full Summary**: Generate detailed structured summaries on each compression. Readable output but may lose details across repeated cycles due to full regeneration rather than incremental merging.

**Key insight**: structure forces preservation. Dedicated sections act as checklists the summarizer must populate.

## The Artifact Trail Problem

Artifact trail integrity is the weakest dimension across all compression methods (2.2–2.5/5.0). Coding agents need to track:
- Which files were created or modified and what changed
- Which files were read but not changed
- Function names, variable names, error messages

This likely requires specialized handling beyond general summarization: a separate artifact index or explicit file-state tracking.

## Structured Summary Template

```markdown
## Session Intent
[What the user is trying to accomplish]

## Files Modified
- auth.controller.ts: Fixed JWT token generation
- config/redis.ts: Updated connection pooling

## Decisions Made
- Using Redis connection pool instead of per-request connections

## Current State
- 14 tests passing, 2 failing

## Next Steps
1. Fix remaining test failures
2. Run full test suite
```

## Compression Trigger Strategies

| Strategy | Trigger Point | Trade-off |
|----------|---------------|-----------|
| Fixed threshold | 70–80% context utilization | Simple but may compress too early |
| Sliding window | Keep last N turns + summary | Predictable context size |
| Importance-based | Compress low-relevance sections first | Complex but preserves signal |
| Task-boundary | Compress at logical task completions | Clean summaries but unpredictable timing |

Sliding window with structured summaries provides the best balance for most coding agent use cases.

## Probe-Based Evaluation

Traditional metrics (ROUGE, embedding similarity) fail to capture functional quality. Use probe questions after compression:

| Probe Type | What It Tests | Example |
|------------|---------------|---------|
| Recall | Factual retention | "What was the original error message?" |
| Artifact | File tracking | "Which files have we modified?" |
| Continuation | Task planning | "What should we do next?" |
| Decision | Reasoning chain | "What did we decide about the Redis issue?" |

## Six Evaluation Dimensions

1. **Accuracy**: Technical details correct? File paths, function names, error codes
2. **Context Awareness**: Response reflects current conversation state?
3. **Artifact Trail**: Agent knows which files were read or modified?
4. **Completeness**: Response addresses all parts of the question?
5. **Continuity**: Work can continue without re-fetching information?
6. **Instruction Following**: Response respects stated constraints?

## Three-Phase Compression Workflow

For large codebases or agent systems exceeding context windows:

1. **Research Phase**: Produce a research document from architecture diagrams, docs, and key interfaces. Output: single structured analysis.
2. **Planning Phase**: Convert research into implementation spec with function signatures, type definitions, and data flow. A 5M token codebase compresses to ~2,000 words.
3. **Implementation Phase**: Execute against the spec rather than raw codebase exploration.

## Implementing Anchored Iterative Summarization

1. Define explicit summary sections matching your agent's needs
2. On first compression, summarize truncated history into sections
3. On subsequent compressions, summarize only new truncated content
4. Merge into existing sections rather than regenerating
5. Track which information came from which compression cycle

## When to Use Each Approach

| Approach | Best when |
|----------|-----------|
| Anchored iterative | Long sessions (100+ messages), file tracking matters, need verifiability |
| Opaque | Maximum token savings needed, short sessions, low re-fetching costs |
| Regenerative | Summary interpretability critical, clear phase boundaries |

## Compression Ratios

| Method | Compression | Quality (5.0) | Notes |
|--------|-------------|---------------|-------|
| Anchored Iterative | 98.6% | 3.70 | Best quality |
| Regenerative | 98.7% | 3.44 | Good quality |
| Opaque | 99.3% | 3.35 | Best compression, quality loss |

The 0.7% additional tokens retained by structured summarization buys 0.35 quality points — worthwhile when re-fetching costs matter.

## Guidelines

1. Optimize for tokens-per-task, not tokens-per-request
2. Use structured summaries with explicit file tracking sections
3. Trigger compression at 70–80% context utilization
4. Use incremental merging rather than full regeneration
5. Test compression quality with probe-based evaluation
6. Track artifact trail separately when file tracking is critical

## References

- [Evaluation Framework Reference](./references/evaluation-framework.md) — probe types and scoring rubrics
- Related skills: context-degradation, context-optimization, evaluation, memory-systems
- Factory Research: Evaluating Context Compression for AI Agents (December 2025)
- Netflix Engineering: "The Infinite Software Crisis" — three-phase workflow (AI Summit 2025)

Files in this skill

  • SKILL.md6.3 KB
  • references/evaluation-framework.md8.3 KB
  • scripts/compression_evaluator.py21.2 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…