Catalog PyPI
PACKAGE EXPLORERView in registry

agent-ctx-compress

PyPI

Smart context compression for LLM agents — preserve critical info, reduce tokens 40-80%

agentaiasynccompressioncontextllmstreamingtokens
Versions collected1
Direct dependencies5From release 0.2.0
Last observed20 Oct, 21:46 UTC

README

Version 0.2.0 documentation

Agent Context Compressor 🗜️

Smart context compression for LLM agents. Preserves critical information (decisions, errors, preferences, code) while reducing token usage by 40-80%.

Problem

LLM context windows are expensive. 90% of agent conversations contain filler — greetings, acknowledgments, repeated tool outputs, verbose explanations. But naive truncation drops critical info like decisions, errors, and user preferences.

Solution

Context Compressor scores every message by importance, then strategically drops low-value content while guaranteeing critical information is preserved.

Before: 603 tokens, 14 messages
After:  410 tokens,  5 messages (32% compressed, 98.7% confidence)

Features

  • 🧠 Smart scoring — classifies messages as decisions, errors, code, preferences, noise
  • 🔒 Critical preservation — decisions, errors, preferences NEVER dropped
  • 🔄 Deduplication — removes near-duplicate messages (Jaccard similarity)
  • 📝 Summarization — long messages compressed instead of dropped
  • 📊 Confidence tracking — know exactly how much info is preserved
  • 🛠️ Multiple interfaces — Python library, CLI, REST API

Installation

pip install agent-ctx-compress

With optional dependencies:

# For API server
pip install agent-ctx-compress[server]

# For accurate token counting
pip install agent-ctx-compress[tiktoken]

# For LLM summarization
pip install agent-ctx-compress[openai]

# Everything
pip install agent-ctx-compress[all]

Quick Start

from context_compressor import compress

messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "yo"},
    {"role": "assistant", "content": "Hey! How can I help?"},
    {"role": "user", "content": "deploy the app to production"},
    {"role": "assistant", "content": "Deployed! Status: ✅ running"},
    {"role": "user", "content": "thanks"},
    {"role": "assistant", "content": "👍"},
]

result = compress(messages, target_ratio=0.3)
print(f"Saved {result.tokens_saved} tokens ({result.compression_ratio:.0%})")
print(f"Confidence: {result.confidence:.0%}")
print(result.compressed)

CLI

# Install from PyPI
pip install agent-ctx-compress

# Compress from file
cat conversation.json | ctxcompress --ratio 0.3

# Compress with stats
ctxcompress --input chat.json --ratio 0.2 --format full

# Stats only
ctxcompress --input chat.json --stats-only

# JSON output
ctxcompress --input chat.json --json

API Server

# Install with server deps
pip install agent-ctx-compress[server]

# Start server
ctxcompress-server
# → http://localhost:8000

# Compress via API
curl -X POST http://localhost:8000/compress \
  -H "Content-Type: application/json" \
  -d '{
    "messages": [
      {"role": "user", "content": "hello"},
      {"role": "assistant", "content": "Hi!"},
      {"role": "user", "content": "deploy app"},
      {"role": "assistant", "content": "Deployed successfully"}
    ],
    "target_ratio": 0.3
  }'

How It Works

Input Messages
     │
     ▼
┌─────────────┐
│   Scorer    │ Classify: decision/error/code/preference/noise
└──────┬──────┘
       │
       ▼
┌─────────────┐
│  Deduplicat │ Remove near-duplicate messages
└──────┬──────┘
       │
       ▼
┌─────────────┐
│  Merge Tool │ Combine consecutive tool results
└──────┬──────┘
       │
       ▼
┌─────────────┐
│  Priority   │ Drop lowest-scored until target ratio
│  Drop       │ Preserve CRITICAL messages always
└──────┬──────┘
       │
       ▼
┌─────────────┐
│ Summarize   │ Compress long messages instead of dropping
└──────┬──────┘
       │
       ▼
Compressed Context + Metadata

Scoring Categories

CategoryImportanceDroppableExamples
systemCRITICAL❌System prompts
decisionCRITICAL❌"Let's use approach A"
errorCRITICAL❌Error messages, fixes
preferenceCRITICAL❌"I prefer dark mode"
codeHIGH⚠️Code blocks, scripts
tool_resultHIGH⚠️API responses, outputs
user_queryHIGH⚠️User questions
structured_responseMEDIUM✅Lists, explanations
explanationMEDIUM✅Long explanations
brief_responseLOW✅Short replies
noiseNOISE✅"yo", "sip", "👍"

Use Cases

  1. Agent context management — Keep conversations within token limits
  2. Cost optimization — Reduce API costs by 40-80%
  3. Session handoff — Compress before switching models
  4. Memory systems — Store compressed conversation summaries
  5. Multi-agent — Share compressed context between agents

License

MIT

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Versions 1

Collected releases
VersionPublishedDirect dependenciesOpen version
0.2.02 Jul 20265

Package advisories

0
No matching advisories collected

This describes our collected data; it is not a security assessment.

Repository

ACCESSIBLE
xbyteid/context-compressor

Shared repository data · GitHub

License detected in repository
MIT
License branch / ref
master
Last push
6 Jul 2026
Archived
No
Contributor identities
— · partial collection

Public sponsor counts have not been collected. These signals belong to the repository owner.

Downloads & popularity

0count
Gauge · observed snapshots21 Oct 2026
10.50
2026-10-21: 0 · observed 2026-10-21T14:14:56.0417128Z
21 Oct21 Oct

github · 1 observations shown. Missing days are unknown.

View observed values
Date / source periodValueObserved at
2026-10-2102026-10-21T14:14:56.0417128Z

Metadata history

Meaningful changes
Current collected state Current

Observed since 20 Oct, 21:46 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package:ac938e201acdf77cfced54adf20545235de52de3b1383d907d68687cfbbefefa