# context-compactor

> Token-saving middleware that compresses long contexts using deterministic, reversible single-token symbol mapping.

context-compactor is a free AI agent skill pack (several skills in one listing) on Agensi by limp, in Prompt & Skill Engineering. It loads into your AI agent through the Agensi MCP server, always in its latest approved version.

- Price: Free
- Creator: [limp](https://www.agensi.io/creators/creator-1340)
- Category: [Prompt & Skill Engineering](https://www.agensi.io/skills/prompt-skill-engineering)
- Version: 1.0
- Last updated: 2026-11-21
- Security: scan passed (2026-11-20); every new listing is also reviewed by a person
- Listing: https://www.agensi.io/skills/context-compactor
- Tags: prompt-engineering, llm-ops, productivity, python, token-optimization, middleware

## About this skill

Long-context windows are expensive and slow. When your conversation history grows or you feed massive documents into an LLM, token costs balloon and prompt caching often fails. This package provides a deterministic, reversible compression layer that maps high-frequency phrases to single-token symbols, drastically reducing overhead without losing semantic data.

### What it does
- **Deterministic Compression** utilizes a dictionary-based approach to replace multi-token phrases with single-token symbols.
- **Transparent Middleware** intercepts chat completions to automatically compress history while keeping recent messages in raw text for reasoning accuracy.
- **Bidirectional Mapping** ensures every compressed string can be expanded back to its original byte-for-byte state.
- **Hybrid Support** handles mixed English n-grams and CJK (Chinese, Japanese, Korean) character strings with specialized overlap filtering.
- **Usage Analytics** reports original vs. compressed token counts, saved percentages, and the break-even point for dictionary overhead.

### How it works
1. **Analyze** the input text or conversation history to identify recurring phrases that provide the highest token savings.
2. **Build** a mapping of these phrases to a verified pool of symbols that represent exactly one token in the `cl100k_base` encoding.
3. **Compress** the history by replacing phrases, prefixing a small "legend" or dictionary header so the model understands the mapping.
4. **Invoke** the LLM with the compacted context, significantly reducing the input payload.

### Frameworks & tools
This tool is built for Python environments and requires `tiktoken` for accurate token validation. It includes a session wrapper compatible with the OpenAI Python client and any OpenAI-compatible API.

### Why this beats prompting it yourself
Prompting an LLM to "compress this text" is lossy, non-deterministic, and consumes output tokens. This skill uses a programmatic, zero-shot algorithm that guarantees 100% data retention and precise token reduction before the LLM even sees the request.

### Use cases
- **Long-running Chatbots** where conversation history grows beyond efficient window limits.
- **Technical Document Q&A** containing repetitive terminology, function names, or boilerplate.
- **Token Cost Optimization** for high-volume API implementations using expensive models like GPT-4o.
- **Fixed Terminology Enforcement** by pre-loading standard dictionaries into the compression layer.

### Known limitations
Requires `tiktoken` installed for token counting and symbol validation. Small dialogues may not see net savings due to the token cost of the dictionary header.

## Requires

- Python

use windows，need installed tiktoken，Can be used onDeepSeek Harness (DSH),Claude Code

## How to use this skill

1. Connect your AI agent to Agensi once: add the MCP server https://mcp.agensi.io/mcp/auth and sign in with your Agensi account (OAuth, no API key). Steps for Claude, ChatGPT, Claude Code, Codex, Cursor, VS Code and other agents: https://www.agensi.io/docs/mcp-setup
2. Ask your agent: "Use the context-compactor skill from Agensi" and describe your task.
3. Your agent loads the skill from Agensi when it needs it. Nothing is downloaded.

## Frequently asked questions

### What does context-compactor do?

Token-saving middleware that compresses long contexts using deterministic, reversible single-token symbol mapping.

### Which AI agents support context-compactor?

This skill uses the open SKILL.md standard. Use it through an MCP-compatible agent connected to Agensi, and check the compatibility information on this page for confirmed agents and required capabilities.

### How do I use context-compactor?

Connect your AI agent to Agensi through MCP, then ask it to use context-compactor on your task. Your agent loads the latest approved version each time. Paid skills require a purchase or active subscription. Skill files are not available for download; your licence does not allow you to save, copy or export any part of a skill.

---

Source: [Agensi](https://www.agensi.io), the marketplace of skills for AI agents. Any MCP-compatible agent (Claude, ChatGPT, Claude Code, Codex, Cursor, VS Code and more) connects at https://mcp.agensi.io/mcp/auth and signs in with an Agensi account; setup for every agent: https://www.agensi.io/docs/mcp-setup. Append .md to any skill, guide or docs address on agensi.io for its markdown version.
