# Common Context Optimization

> Maximize context window efficiency, reduce latency, and prevent lost-in-middle issues through strategic masking and compaction. Use when token budgets are tight, tool outputs overflow the context, conversations drift from intent, or latency spikes from cache misses.

- **Type:** Skill
- **Install:** `agentstack add skill-hoangnguyen0403-agent-skills-standard-common-context-optimization`
- **Verified:** Yes — security-reviewed for prompt injection and unsafe behavior
- **Seller:** [HoangNguyen0403](https://agentstack.voostack.com/s/hoangnguyen0403)
- **Installs:** 0
- **Category:** [Agent Skills](https://agentstack.voostack.com/c/agent-skills)
- **Latest version:** 0.1.0
- **License:** Apache-2.0
- **Upstream author:** [HoangNguyen0403](https://github.com/HoangNguyen0403)
- **Source:** https://github.com/HoangNguyen0403/agent-skills-standard/tree/develop/.agents/skills/common/common-context-optimization
- **Website:** https://www.npmjs.com/package/agent-skills-standard

## Install

```sh
agentstack add skill-hoangnguyen0403-agent-skills-standard-common-context-optimization
```

Requires the [AgentStack CLI](https://agentstack.voostack.com/docs/cli). Works with Claude Code, Cursor, and any MCP-compatible agent.

## About

## **Priority: P1 (OPTIMIZATION)**

## 1. Observation Masking (Noise Reduction)

**Problem**: Large tool outputs (logs, JSON lists) overwhelm context and degrade reasoning.
**Solution**: Replace raw output with semantic summaries _after_ consumption.

1. **Identify** outputs exceeding 50 lines or 1 KB.
2. **Extract** critical data points immediately.
3. **Mask** by rewriting history to replace raw data with summary placeholder.
4. **See** `references/masking.md` for patterns.

See [implementation examples](references/implementation.md) for masking patterns.

## 2. Context Compaction (State Preservation)

**Problem**: Long conversations drift from original intent.
**Solution**: Recursive summarization that preserves _State_ over _Dialogue_.

1. **Trigger** compaction every 10 turns or 8k tokens.
2. **Compact**:
 - **Keep**: User Goal, Active Task, Current Errors, Key Decisions.
 - **Drop**: Chat chit-chat, intermediate tool calls, corrected assumptions.
3. **Format**: Update System Prompt or Memory File with compacted state.
4. **See** `references/compaction.md` for algorithms.

See [implementation examples](references/implementation.md) for compacted state format.

## 3. KV-Cache Awareness (Latency)

**Goal**: Maximize pre-fill cache hits.

- **Static Prefix**: Enforce strict ordering — System -> Tools -> RAG -> User.
- **Append-Only**: Never insert into middle of history; append new turns only.

## References

- [Observation Masking Patterns](references/masking.md)
- [Compaction Algorithms](references/compaction.md)

## Anti-Patterns

- **No raw tool dumps**: Mask large outputs immediately after extracting data.
- **No unbounded growth**: Compact every 10 turns to preserve intent over dialogue.
- **No middle insertions**: Append-only history maximizes KV cache hits.

## Source & license

This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.

- **Author:** [HoangNguyen0403](https://github.com/HoangNguyen0403)
- **Source:** [HoangNguyen0403/agent-skills-standard](https://github.com/HoangNguyen0403/agent-skills-standard)
- **License:** Apache-2.0
- **Homepage:** https://www.npmjs.com/package/agent-skills-standard

Install and usage instructions live in the source repository linked above.

## Pricing

- **Free** — Free

## Security capabilities

Automated source analysis of v0.1.0 — what this tool can access:

- **Network access:** no
- **Filesystem access:** no
- **Shell / process execution:** no
- **Environment & secrets:** no
- **Dynamic code execution:** no

*"Yes" means the capability is present in the source — more access means more to trust, not that it is unsafe.*


## Versions

- **0.1.0** — security scan: passed — Imported from the upstream source.

## Links

- Listing page: https://agentstack.voostack.com/l/skill-hoangnguyen0403-agent-skills-standard-common-context-optimization
- Seller: https://agentstack.voostack.com/s/hoangnguyen0403
- Browse the marketplace: https://agentstack.voostack.com/browse

---
Listed on AgentStack — the marketplace for AI agent skills and MCP servers. Every listing is security-reviewed. Creators keep 70%.
