Skip to main content
  _        _                                             
 | |_ ___ | | _____ _ __  ___  __ _ _   _  ___  ___ _______ 
 | __/ _ \| |/ / _ \ '_ \/ __|/ _` | | | |/ _ \/ _ \_  / _ \
 | || (_) |   <  __/ | | \__ \ (_| | |_| |  __/  __// /  __/
  \__\___/|_|\_\___|_| |_|___/\__, |\__,_|\___|\___/___\___|
                                 |_|                         

Reduce LLM conversation tokens by up to 91%. Drop-in compression for any OpenAI-compatible API.

CI License: MIT Python 3.10+


Why

Every turn in an LLM conversation resends the entire history. A 30-turn conversation can burn 50,000+ tokens per request — most of it redundant context the model has already processed.

That's wasted money and wasted context window.

tokensqueeze compresses your conversation history before each API call. One line of code, 91% fewer tokens.

Real-World Results

Tested on 20 real Claude Code conversations (1,200 messages):

  114,481 total tokens → 10,146 tokens (91.1% savings)
Conversation Messages Before After Saved
Coding session #1 60 7,686 261 96.6%
Coding session #2 60 11,356 377 96.7%
Coding session #3 60 6,524 246 96.2%
Average (20 conversations) 60 5,724 507 91.1%

These aren't synthetic benchmarks — they're actual development conversations with tool calls, file reads, and multi-step debugging.

Install

pip install tokensqueeze

For LLM-powered compression (recommended), install Ollama and pull a small model:

curl -fsSL https://ollama.com/install.sh | sh
ollama pull llama3.2:3b   # 2GB, runs on any machine

Quick Start

Use with Claude Code, Cursor, or any AI tool

tokensqueeze runs as a local proxy — no code changes needed:

# Terminal 1: start the proxy
tokensqueeze proxy

# Terminal 2: use Claude Code through it
ANTHROPIC_BASE_URL=http://localhost:8080 claude

That's it. Every API call now gets compressed automatically. The proxy logs savings in real time:

[tokensqueeze] /v1/messages — 12,847 → 3,854 tokens (70% saved)
[tokensqueeze] /v1/messages — 18,231 → 5,102 tokens (72% saved)

Works with anything that lets you set a base URL: Claude Code, Cursor, OpenAI SDK apps, LangChain, etc.

For maximum compression (requires Ollama):

tokensqueeze proxy --use-llm

Use as a Python library

If you're building your own app, one line:

from openai import OpenAI
from tokensqueeze import squeeze

client = OpenAI()
messages = [...]  # your conversation history

result = squeeze(messages, use_llm=True)
print(result)  # CompressionResult(5,724 → 507 tokens, saved 91.1%)

response = client.chat.completions.create(
    model="gpt-4o",
    messages=result.messages  # compressed
)

How It Works

Four strategies applied in sequence, each targeting a different type of waste:

messages → [Dedup] → [Trim] → [Condense] → [Summarize] → compressed
# Strategy What it does Savings
1 Deduplication Removes repeated content across messages (re-pasted code, duplicate tool results) 5-15%
2 Tool result trimming Shrinks large file reads and API responses that have already been processed 15-25%
3 Condensation Compresses individual verbose messages in place via local LLM 20-40%
4 LLM summarization Compresses old turns into structured fact-preserving notes 30-50%

Strategies are composable — use the defaults, pick specific ones, or write your own.

What compressed context looks like

[Conversation context — 14 earlier messages]
- Topic: Troubleshooting Flask application issues
- ERROR: ImportError: cannot import name celery_app → Fix: separate celery_config.py
- ERROR: ValueError: invalid literal for int() → FIX: @app.route(/users/<int:id>)
- ISSUE: stale cache data → FIX: cache.delete(f"user:{user_id}") after commit
- ERROR: .exe upload accepted → FIX: extension allowlist + python-magic MIME check
- ISSUE: race condition on inventory → FIX: SELECT FOR UPDATE with_for_update()

Exact error messages, function names, and decisions are preserved — not just a vague summary.

Two Modes

Extractive LLM (Ollama)
How Keyword extraction + sliding window Local LLM condensation + structured summarization
Speed <1ms 1-15s
Requires tiktoken only Ollama + llama3.2:3b (free)
Savings 40-70% 85-97%
Quality 81/100 79-89/100
# Extractive — fast, no dependencies
result = squeeze(messages)

# LLM — much better compression, requires Ollama
result = squeeze(messages, use_llm=True)

If Ollama isn't running, use_llm=True falls back to extractive automatically.

CLI

# Start the compression proxy
tokensqueeze proxy                              # Anthropic (default)
tokensqueeze proxy --target openai              # OpenAI
tokensqueeze proxy --use-llm                    # LLM compression via Ollama
tokensqueeze proxy --port 9000                  # Custom port

# Compress a conversation file
tokensqueeze compress conversation.json -o compressed.json
tokensqueeze compress --use-llm --stats-only conversation.json
tokensqueeze compress --use-llm --score conversation.json

Quality Scoring

tokensqueeze includes a built-in quality judge that measures whether compressed conversations still produce equivalent LLM responses:

result = squeeze(messages, use_llm=True)
quality = result.score_quality()

print(quality)         # QualityScore(overall=79/100, faith=80, comp=70, coher=90)
print(quality.grade)   # "good"
Score Grade Meaning
90-100 Lossless All meaningful context preserved
70-89 Good Minor details lost, core meaning intact
50-69 Degraded Noticeable information loss
<50 Broken Too much context destroyed

Advanced Usage

Custom strategy pipeline
from tokensqueeze import squeeze, LLMSummarizeStrategy, ToolResultTrimStrategy, CondenseStrategy

result = squeeze(
    messages,
    strategies=[
        ToolResultTrimStrategy(max_tokens=100, preserve_recent=1),
        CondenseStrategy(min_tokens=50),
        LLMSummarizeStrategy(window_size=2, ollama_model="llama3.2:3b"),
    ]
)
Compressor instance with timeout
from tokensqueeze import Compressor

compressor = Compressor(model="gpt-4o", max_time=30.0)  # 30s pipeline timeout
result = compressor.compress(messages)

print(f"Saved {result.saved_tokens:,} tokens ({result.savings_pct:.1f}%)")
for strategy, saved in result.strategy_stats.items():
    print(f"  {strategy}: -{saved:,}")
Custom strategy
from tokensqueeze import Strategy

class MyStrategy(Strategy):
    def compress(self, messages, **kwargs):
        # Your compression logic here
        return messages
Remote or alternative LLM
result = squeeze(
    messages,
    use_llm=True,
    ollama_model="mistral:7b",
    ollama_url="http://your-server:11434",
)

Any OpenAI-compatible endpoint works — Ollama, vLLM, LM Studio, or a remote server.

Input Format

Standard OpenAI message format:

[
  {"role": "system", "content": "You are a helpful assistant."},
  {"role": "user", "content": "Hello"},
  {"role": "assistant", "content": "Hi there!"},
  {"role": "user", "content": "..."}
]

How tokensqueeze Compares

Other tools in the token reduction space solve different parts of the problem:

tokensqueeze Caveman RTK
Compresses Conversation history (input) LLM responses (output) Shell command output (input)
Technique Dedup + condense + LLM summarize Prompt engineering ("be terse") Per-command Rust filters
Reduction 67-97% input tokens ~65% output tokens 60-90% tool output
Target Any LLM app or tool (proxy mode) AI coding agents AI coding agents
Integration One env var or one line of code Config file install Shell hook

These tools are complementary. Use all three together for maximum savings:

  • RTK compresses tool results as they enter the conversation
  • Caveman makes the LLM respond more concisely
  • tokensqueeze compresses the accumulated history before each API call

Measuring Your Savings

tokensqueeze reports savings on every call:

result = squeeze(messages, use_llm=True)
print(result)                 # CompressionResult(5,724 → 507 tokens, saved 91.1%)
print(result.strategy_stats)  # per-strategy breakdown

For tracking overall token costs across your workflow, ccusage is a useful companion — it reads usage logs from coding agents and generates cost reports:

npx ccusage@latest
ccusage claude daily  # compare before and after adding tokensqueeze

License

MIT

Metadata

Release files for tokensqueeze 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tokensqueeze 0.2.0
File Size Uploaded Provenance
tokensqueeze-0.2.0.tar.gz 42.3 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for tokensqueeze 0.2.0
File Interpreter ABI Platform
tokensqueeze-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 70.8 kB

Release files / tokensqueeze-0.2.0.tar.gz

Download URL tokensqueeze-0.2.0.tar.gz
Size 42.3 kB
Tags Source
SHA-256 checksum
How to use checksums
13e9dec3621f58ce1953eab79366f665d5d4c9ae01c00d6be7b0cb5fdd77b47f
BLAKE2b-256 checksum
How to use checksums
819e7f33378deaf63d0c7d8ee05e74bb05f71926420e8a1e0d4b7c507fc70841
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 15, 2026.

Transparency log

Release files / tokensqueeze-0.2.0-py3-none-any.whl

Download URL tokensqueeze-0.2.0-py3-none-any.whl
Size 28.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a80e4a4608f7b8afdbbd227750039cd8ed09b6c3eeccd6db432bccb10b715076
BLAKE2b-256 checksum
How to use checksums
deb913e36a545191ecc9443d60551dba61d9fa703612fbb874c6233bff6411e7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 15, 2026.

Transparency log

Security for tokensqueeze 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for tokensqueeze-0.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 15, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page