Skip to main content

Reduce LLM conversation tokens by up to 70%

Project description

  _        _                                             
 | |_ ___ | | _____ _ __  ___  __ _ _   _  ___  ___ _______ 
 | __/ _ \| |/ / _ \ '_ \/ __|/ _` | | | |/ _ \/ _ \_  / _ \
 | || (_) |   <  __/ | | \__ \ (_| | |_| |  __/  __// /  __/
  \__\___/|_|\_\___|_| |_|___/\__, |\__,_|\___|\___/___\___|
                                 |_|                         

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

from openai import OpenAI
from tokensqueeze import squeeze

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

# One line — that's it
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

# Compress a conversation
tokensqueeze conversation.json -o compressed.json

# LLM-powered compression
tokensqueeze --use-llm conversation.json -o compressed.json

# Just show stats
cat conversation.json | tokensqueeze --use-llm --stats-only

# Score compression quality
tokensqueeze --use-llm --score conversation.json

# Use a different Ollama model
tokensqueeze --use-llm --ollama-model mistral:7b 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 app calling an LLM API AI coding agents AI coding agents

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tokensqueeze-0.1.0.tar.gz (39.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tokensqueeze-0.1.0-py3-none-any.whl (24.9 kB view details)

Uploaded Python 3

File details

Details for the file tokensqueeze-0.1.0.tar.gz.

File metadata

  • Download URL: tokensqueeze-0.1.0.tar.gz
  • Upload date:
  • Size: 39.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for tokensqueeze-0.1.0.tar.gz
Algorithm Hash digest
SHA256 146b9efba5947cc0dd63697af5ea2b539c452147c54f1d47ffcc02f12997e469
MD5 b958b60c9ccbc7d44b7cb14c59216780
BLAKE2b-256 cb049e414eafc1089c14cfa3fc12db3e9172e48cb123672eeeea4e7728a179bf

See more details on using hashes here.

Provenance

The following attestation bundles were made for tokensqueeze-0.1.0.tar.gz:

Publisher: publish.yml on StuckInTheNet/tokensqueeze

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tokensqueeze-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: tokensqueeze-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 24.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for tokensqueeze-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4a84d19b3298ba099a2b47ea5ff5a0b673d81ad5f3181d0bc4d34dc247459d00
MD5 66c4f5c3fe5433ae03f33b9ea3fa2874
BLAKE2b-256 dc4e7892f801b40c19a2957d571c2da77f28a622872dd9815f714037ae68a1c1

See more details on using hashes here.

Provenance

The following attestation bundles were made for tokensqueeze-0.1.0-py3-none-any.whl:

Publisher: publish.yml on StuckInTheNet/tokensqueeze

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page