Optimize LLM Context Compression for Codebases
A context-compression skill comparing anchored iterative summarization, opaque compression, and regenerative summaries for AI agents.
Why it matters
Reduce token consumption and improve agent efficiency in large codebases by implementing advanced context compression strategies. Ensure critical information is preserved across long agent sessions.
Outcomes
What it gets done
Implement anchored iterative summarization for structured context preservation.
Evaluate compression strategies using probe-based methods for functional quality.
Manage context window limits for coding agents and large codebases.
Optimize token usage by focusing on tokens-per-task rather than tokens-per-request.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/ag-context-compression | bash Overview
Context Compression Strategies
A context-compression skill comparing anchored iterative summarization, opaque compression, and regenerative summaries, with structured-summary templates and probe-based evaluation. Use it when agent sessions or codebases exceed context limits and you need a deliberate compression and evaluation strategy, not an ad hoc summary.
What it does
Context Compression Strategies is a skill for compressing agent conversation history and codebase exploration once sessions generate millions of tokens. It names three production-ready approaches: Anchored Iterative Summarization (a persistent, structured summary with dedicated sections for session intent, file modifications, decisions, and next steps, where only the newly-truncated span is summarized and merged into the existing sections on each trigger), Opaque Compression (compressed representations optimized purely for reconstruction fidelity, reaching 99%+ compression but sacrificing interpretability - you cannot verify what was preserved), and Regenerative Full Summary (a fresh detailed structured summary regenerated on each compression, readable but prone to losing details across repeated cycles). Its core insight is that structure forces preservation: dedicating explicit sections to specific information types prevents the silent information drift that unstructured summarization allows. The optimization target it argues for is tokens-per-task rather than tokens-per-request - a strategy saving 0.5% more tokens per request but causing 20% more re-fetching costs more overall. The Artifact Trail dimension (which files were created, modified, read, or contain which function/variable names) is flagged as the weakest dimension across all three methods, scoring only 2.2-2.5 out of 5.0 in evaluation.
A structured summary follows this section template:
### Session Intent
[What the user is trying to accomplish]
### Files Modified
- auth.controller.ts: Fixed JWT token generation
- config/redis.ts: Updated connection pooling
- tests/auth.test.ts: Added mock setup for new config
### Decisions Made
- Using Redis connection pool instead of per-request connections
- Retry logic with exponential backoff for transient failures
### Current State
- 14 tests passing, 2 failing
- Remaining: mock setup for session service tests
### Next Steps
1. Fix remaining test failures
2. Run full test suite
3. Update documentation
When to use - and when NOT to
Activate this skill when agent sessions exceed context window limits, when working across codebases exceeding 5M+ tokens, when designing conversation summarization strategies, when debugging cases where an agent "forgets" what files it modified, or when building evaluation frameworks for compression quality. Choose anchored iterative summarization for long-running sessions (100+ messages) where file tracking matters and you need to verify what was preserved. Choose opaque compression only when maximum token savings is required, sessions are relatively short, and re-fetching costs are low. Choose regenerative summaries when interpretability is critical, sessions have clear phase boundaries, and a full context review on each compression is acceptable.
Inputs and outputs
Input is a long-running agent conversation or a codebase exploration exceeding the context budget. Output is a structured summary populated across explicit sections, chosen and triggered by a defined strategy: fixed-threshold triggers compress at 70-80% context utilization, sliding-window keeps the last N turns plus a summary, importance-based compresses low-relevance sections first, and task-boundary triggers compress at logical task completions - the skill recommends sliding window with structured summaries as the best balance for most coding-agent use cases. Quality is measured with probe-based evaluation rather than lexical-overlap metrics like ROUGE: recall probes test factual retention, artifact probes test file tracking, continuation probes test task planning, and decision probes test the reasoning chain, scored against six dimensions - accuracy, context awareness, artifact trail, completeness, continuity, and instruction following. For codebases exceeding context windows, output can instead follow a three-phase workflow: a research phase compressing exploration into a structured analysis document, a planning phase converting that research into an implementation specification (a 5M-token codebase compresses to roughly 2,000 words of spec), and an implementation phase that executes against the spec rather than the raw codebase.
Integrations
This skill connects to three others in the same collection: context-degradation (compression is one mitigation strategy for degradation), context-optimization (compression is one optimization technique among several), and memory-systems (compression relates to scratchpad and summary memory patterns); evaluation supplies the probe-based methodology used to test compression quality.
Who it's for
Agent and context engineers designing conversation summarization, memory, or scratchpad strategies for long-running coding agents or large-codebase agent systems, who need a named comparison of compression trade-offs rather than picking a compression ratio blind.
FAQ
Common questions
Discussion
Questions & comments ยท 0
Sign In Sign in to leave a comment.