Skip to main content

Token Reducer

Cut Claude API costs by 90%+ with intelligent context compression

Claude Code Plugin License: MIT Release Python 3.11+ SQLite

The open-source alternative to expensive context management tools.

Easy Install • Features • Documentation • Contributing


The Problem

Every time you use Claude with a large codebase, you're paying for thousands of tokens that aren't relevant to your query. Most context management tools either:

  • Send everything (expensive)
  • Truncate blindly (loses important context)
  • Require heavy Language Servers (slow, resource-intensive)

The Solution

Token Reducer is a local-first, intelligent context compression pipeline that:

  • Reduces tokens by 90-98% while preserving semantic relevance
  • Runs entirely locally — no API calls, no data leaving your machine
  • Works in milliseconds — faster than Language Server alternatives
  • Understands code semantically — AST parsing, not just text matching
┌─────────────────┐     ┌───────────────┐     ┌──────────────────┐
│  Your Codebase  │────▶│ Token Reducer │────▶│  Compressed      │
│  (50,000 tokens)│     │   Pipeline    │     │  Context (500t)  │
└─────────────────┘     └───────────────┘     └──────────────────┘
                              │
                    ┌─────────┴─────────┐
                    │  - AST Chunking   │
                    │  - BM25 + Vector  │
                    │  - TextRank       │
                    │  - Import Graph   │
                    │  - 2-Hop Symbols  │
                    └───────────────────┘

Easy Install

Option 1 — Claude Code /plugin Command (Recommended)

Step 1: Register the marketplace (one-time setup):

/plugin marketplace add Madhan230205/token-reducer

This registers the marketplace as Madhan230205-token-reducer.

Step 2: Install:

/plugin install token-reducer@Madhan230205-token-reducer

For project-scoped install:

/plugin install token-reducer@Madhan230205-token-reducer --scope project

Already ran Step 1 before? Just run /plugin install token-reducer@Madhan230205-token-reducer — no need to add the marketplace again.


Option 2 — Git Clone (Manual)

# 1. Clone into your Claude plugins folder
git clone https://github.com/Madhan230205/token-reducer.git ~/.claude/plugins/token-reducer

# 2. Install dependencies (optional but recommended for best results)
pip install -r ~/.claude/plugins/token-reducer/requirements-optional.txt

Windows users: Replace ~/.claude/plugins/ with %USERPROFILE%\.claude\plugins\

Then open ~/.claude/settings.json and add:

{
  "plugins": ["~/.claude/plugins/token-reducer"]
}

Restart Claude Code. Done.


What requirements-optional.txt installs:

Package Purpose
sentence-transformers Neural embeddings for smarter retrieval
hnswlib / faiss-cpu Fast approximate nearest-neighbor search
tree-sitter + language grammars AST-based code chunking (Python, JS, TS, Go, Rust, Java, C/C++, Ruby)

If you skip this step, Token Reducer still works using hash embeddings and regex chunking — no ML libraries required.


Option 3 — Zero-Dependency Quick Start

No pip, no ML libs — runs immediately after cloning:

git clone https://github.com/Madhan230205/token-reducer.git
cd token-reducer
python scripts/context_pipeline.py run \
  --inputs ./src \
  --query "Find auth logic" \
  --embedding-backend hash \
  --db .cache/index.db

Features

Core Pipeline

  • Hybrid Retrieval — BM25 + semantic vector search with intelligent fallback
  • AST-Based Chunking — Tree-sitter parsing for Python, TypeScript, Go, Rust, Java, and more
  • TextRank Compression — Graph-based sentence scoring for intelligent summarization
  • Sub-100ms Queries — SQLite FTS5 + HNSW indexes for instant results
  • Local-First — Everything runs on your machine, no external APIs

LSP-Killer Features

  • Import Graph — Automatically maps file dependencies without Language Server
  • 2-Hop Symbol Expansion — Auto "go-to-definition" for referenced functions
  • Diff Protocol — SEARCH/REPLACE edit format with automatic application
  • Semantic Clustering — Groups similar chunks to avoid redundancy

Enterprise Ready

  • Fully Configurable — 40+ tunable parameters in settings.json
  • Embedding Flexibility — ML models or hash fallback (zero dependencies)
  • Query Caching — Intelligent TTL-based caching for repeated queries
  • Session Memory — Tracks context across conversation turns

Documentation

How It Works

Query → FTS(BM25) → (Vector fallback if needed) → Merge → Top 5 → Compress

Full pipeline:

PREPROCESS → INDEX → RETRIEVE → RE-RANK → COMPRESS → CONTEXT PACKET

Basic Usage

# Index your codebase
python scripts/context_pipeline.py index --inputs ./src --db .cache/index.db

# Query with compression
python scripts/context_pipeline.py query \
  --query "How does authentication work?" \
  --db .cache/index.db \
  --json

# One-shot: index + query
python scripts/context_pipeline.py run \
  --inputs ./src \
  --query "Find the database connection logic" \
  --db .cache/index.db

Configuration

All settings in settings.json:

{
  "tokenReducer": {
    "chunkSizeWords": 220,
    "embeddingModel": "jinaai/jina-embeddings-v2-base-code",
    "hybridMode": "fallback",
    "astChunkingEnabled": true,
    "textRankEnabled": true,
    "lspFeatures": {
      "importGraphEnabled": true,
      "twoHopExpansionEnabled": true
    }
  }
}
Full Configuration Reference
Setting Default Description
chunkSizeWords 220 Target words per chunk
embeddingBackend "ml" "ml" for neural, "hash" for zero-dep
embeddingModel jina-v2-code Code-optimized embeddings
hybridMode "fallback" "fallback" or "always" for vector
astChunkingEnabled true Use tree-sitter AST parsing
textRankEnabled true Graph-based sentence scoring
importGraphEnabled true Track file dependencies
twoHopExpansionEnabled true Auto-expand referenced symbols
compressionWordBudget 350 Max words in compressed output

Zero-Dependency Mode

Run without any ML libraries:

python scripts/context_pipeline.py run \
  --inputs ./src \
  --query "Find auth logic" \
  --embedding-backend hash \
  --db .cache/index.db

Apply Code Edits

python scripts/apply_diff.py --input claude_response.txt --dir ./src
python scripts/apply_diff.py --input response.txt --dry-run

Architecture

Technology Stack

  • Storage: SQLite with FTS5 + custom embeddings table
  • Chunking: Tree-sitter AST parsing with regex fallback
  • Embeddings: Jina Code v2 (or zero-dependency hash embeddings)
  • ANN Search: HNSW via hnswlib (with FAISS fallback)
  • Compression: TextRank + query-relevance scoring

Repository Structure

token-reducer/
├── .claude-plugin/plugin.json
├── .mcp.json
├── .env.example
├── settings.json
├── requirements-optional.txt
├── scripts/
├── hooks/
├── commands/
├── agents/
├── skills/
└── evals/

Contributing

If anyone is interested in contributing, this project is open to contributions. Please see contribute.md for contribution guidelines.

git clone https://github.com/Madhan230205/token-reducer.git
cd token-reducer
pip install -e ".[dev]"
python scripts/context_pipeline.py self-test

License

MIT License — see LICENSE for details.


Acknowledgments


Star this repo if Token Reducer saves you money!

Report Bug • Request Feature • Discussions

Metadata

Release files for claude-token-reducer 1.4.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 claude-token-reducer 1.4.0
File Size Uploaded
claude_token_reducer-1.4.0.tar.gz 43.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for claude-token-reducer 1.4.0
File Interpreter ABI Platform
claude_token_reducer-1.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 91.0 kB

Release files / claude_token_reducer-1.4.0.tar.gz

Download URL claude_token_reducer-1.4.0.tar.gz
Size 43.9 kB
Tags Source
SHA-256 checksum
How to use checksums
f49a56b79a604e6ac0397f667735cbf8b9388b7124fa30a96be7102bc0569045
BLAKE2b-256 checksum
How to use checksums
3561e333808affab23f600d698a7e3dd0c258f26b0dfdd87abe5a2170da425bf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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 Apr 3, 2026.

Transparency log

Release files / claude_token_reducer-1.4.0-py3-none-any.whl

Download URL claude_token_reducer-1.4.0-py3-none-any.whl
Size 47.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0904efd9f3eb7545dc04194404223b259498a6e5d6c6781b8b545a12b397ded1
BLAKE2b-256 checksum
How to use checksums
f450b8af928f176385a690aaeb6fb61443c8cbce10be9091ecbfa34ee4a32c9f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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 Apr 3, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.4.0 This release

2 release files

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