mcpbeat

Common Context Optimization

hoangnguyen0403/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.

1k tokens
context cost
the whole folder, loaded on every use
4
files
instructions only
0
copies elsewhere
how many repositories repackaged it
536
stars on the repo
on the repository, not the skill itself

Install

one command, takes just this skill from the repository
npx skills add https://github.com/HoangNguyen0403/agent-skills-standard --skill common-context-optimization

What comes with it

1 987 bytes besides the instruction
references/compaction.md
references/implementation.md
references/masking.md

The instruction itself

6 sections, as written by the author

Priority: P1 (HIGH)

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.

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

See implementation examples for masking patterns.

2. Context Compaction (State Preservation)

Problem: Long conversations drift from original intent.

Solution: Recursive summarization that preserves _State_ over _Dialogue_.

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

See implementation examples 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
  • Compaction Algorithms

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.

How to use it

Copy the folder

Take hoangnguyen0403/common-context-optimization from the repository into ~/.claude/skills for personal use, or into .claude/skills inside a project.

Check the name does not clash

The agent identifies a skill by the name field in its header. Two skills with the same name cannot sit side by side — one of them will be ignored.