Skip to main content

Universal token optimizer for coding agents โ€” save 40-70% on LLM costs

Project description

Tokenxygen Banner

๐Ÿš€ Tokenxygen

PyPI Python License Language Last commit

Universal token optimizer for coding agents โ€” save 40-70% on LLM costs.

Tokenxygen is a drop-in proxy that sits between your coding agent (Claude Code, Cursor, Windsurf, Codex) and LLM APIs, automatically optimizing tokens in-flight. Zero code changes required.

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Coding      โ”‚โ”€โ”€โ”€โ”€โ–ถโ”‚  Tokenxygen    โ”‚โ”€โ”€โ”€โ”€โ–ถโ”‚  OpenAI  โ”‚
โ”‚  Agent       โ”‚โ—€โ”€โ”€โ”€โ”€โ”‚  Proxy         โ”‚โ—€โ”€โ”€โ”€โ”€โ”‚  API     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                    ๐ŸŽฏ Cache | Compress | Route | Budget

โšก Quick Start

pip install tokenxygen

# Start the proxy
tokenxygen serve

# In another terminal โ€” use your coding agent normally
export OPENAI_BASE_URL=http://127.0.0.1:8420/v1
claude  # or cursor, windsurf, etc.

That's it. Tokens are optimized automatically.

๐Ÿ”ง How It Works

Tokenxygen applies multiple optimization strategies in order:

# Strategy What It Does Typical Savings
1 Semantic Cache Reuse identical/similar responses 30-60%
2 File Deduplication Remove repeated file contents 10-30%
3 Plugin Strategies Custom compression (diffs, imports, tool outputs) 5-20%
4 LLMLingua-2 Token-level compression (when installed) 40-60%
5 Smart Routing Send simple queries to cheap models 40-70%
6 Budget Guards Enforce daily limits, auto-downgrade Variable
7 Failover Automatic provider switching on failure Availability

Combined, these typically save 40-70% on token costs without noticeable quality loss.

๐Ÿ“Š Dashboard

Start the proxy with a real-time web dashboard:

tokenxygen serve --dashboard-port 8421
# Open http://127.0.0.1:8421

๐Ÿณ Docker Deployment

# Quick start with Docker Compose
cp .env.example .env
# Edit .env with your API keys
docker compose up -d

# Services:
# - Proxy: http://localhost:8420
# - Dashboard: http://localhost:8421
# - Prometheus: http://localhost:9091

๐Ÿ›ก๏ธ Security Features

Tokenxygen includes comprehensive security hardening:

Feature Description
SSRF Prevention Blocks requests to localhost, private IPs, and non-HTTP protocols
Rate Limiting Per-IP sliding window with Redis support for distributed deployments
Security Headers CSP, HSTS, X-Content-Type-Options, X-Frame-Options, and more
Admin Authentication API key protection for sensitive endpoints
Request Validation Path traversal and null byte injection prevention
Size Limits 10MB maximum request body
Secure Logging Authorization headers never logged; correlation IDs for audit trails

Environment Variables

# Security settings
export TOKENXYGEN_ADMIN_KEY="your-admin-api-key"  # Protect admin endpoints
export TOKENXYGEN_REDIS_URL="redis://localhost:6379/0"  # Distributed rate limiting

# Production settings
export OPENAI_BASE_URL="http://localhost:8420/v1"
export OPENAI_API_KEY="sk-..."

๐Ÿ“ฆ Installation

# Basic install
pip install tokenxygen

# With LLMLingua-2 for deeper compression
pip install tokenxygen[llmlingua]

# Full install (all features)
pip install tokenxygen[full]

# Development
pip install -e ".[dev]"

๐Ÿ› ๏ธ CLI Commands

# Start the proxy server + dashboard
tokenxygen serve
tokenxygen serve --json-logs --log-level DEBUG

# View token usage stats
tokenxygen stats

# Show daily cost history
tokenxygen history --days 30

# Count tokens in text
tokenxygen tokens "Hello, world!"

# Show compression results
tokenxygen compress "your code here"
tokenxygen deep-compress "long text here"

# List available models and costs
tokenxygen providers

# Compare cost between two models
tokenxygen compare gpt-4o gpt-4o-mini --tokens 5000

# Show cheapest model for different sizes
tokenxygen cheapest

# Set budget limit
tokenxygen budget 20.00

# Check budget status
tokenxygen budget-status

# Cache statistics
tokenxygen cache-stats

# List loaded plugins
tokenxygen plugins

# Show failover pool status
tokenxygen failover

# Show metrics
tokenxygen metrics

# Get setup instructions
tokenxygen setup

# Print shell exports
eval "$(tokenxygen env)"

# Reset analytics
tokenxygen reset --clear-cache

๐Ÿ”Œ Multi-Provider Support

Provider Models Cost
OpenAI GPT-4o, GPT-4o-mini, GPT-4-turbo $0.15-30/1K tokens
Anthropic Claude 3.5 Sonnet, Claude 3 Haiku $0.25-75/1K tokens
Google Gemini 1.5 Pro, Gemini 1.5 Flash $0.075-10.5/1K tokens
Ollama Llama 3.1, CodeLlama, DeepSeek Coder Free (local)

๐Ÿ›ก๏ธ Budget Guards

Utilization Action Effect
< 80% PASS No changes
80-95% DOWNGRADE Route to cheaper model
95-100% AGGRESSIVE_COMPRESS Maximum compression
> 100% BLOCK Return 429 error

๐Ÿ“Š Monitoring

Prometheus Metrics

# Metrics endpoint
curl http://localhost:8420/metrics

# JSON metrics
curl http://localhost:8420/metrics/json

Response Headers

X-Tokenxygen-Original-Tokens: 12450
X-Tokenxygen-Optimized-Tokens: 4320
X-Tokenxygen-Saved: 8130
X-Tokenxygen-Strategies: file_dedup,plugin:compress_diffs

โš™๏ธ Configuration

from tokenxygen.config import settings

# Core settings
settings.cache.enabled = True
settings.compress.aggressiveness = 0.4
settings.router.enabled = True
settings.budget.daily_limit_usd = 20.0

# Security settings
settings.proxy.enable_hsts = True  # Enable when behind HTTPS proxy
settings.proxy.cors_origins = ["https://yourdomain.com"]  # CORS origins
settings.proxy.trust_forwarded_headers = True  # When behind reverse proxy
settings.proxy.redis_url = "redis://localhost:6379/0"  # Distributed rate limiting

๐Ÿงช Development

git clone https://github.com/Dream-Pixels-Forge/tokenxygen.git
cd tokenxygen
pip install -e ".[dev]"

# Run tests
pytest

# Run with auto-reload
tokenxygen serve --reload

๐Ÿ—บ๏ธ Roadmap

  • v0.1 โ€” Core proxy, cache, compression, analytics
  • v0.2 โ€” Smart routing, budget guards, web dashboard
  • v0.3 โ€” Multi-provider support, plugin system
  • v0.4 โ€” LLMLingua-2 compression, failover, load balancing
  • v0.5 โ€” Production hardening, metrics, Docker, security
  • v1.0 โ€” Production ready (rate limiting, auth, API keys)

๐Ÿ“„ 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

tokenxygen-0.5.2.tar.gz (65.3 kB view details)

Uploaded Source

Built Distribution

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

tokenxygen-0.5.2-py3-none-any.whl (58.1 kB view details)

Uploaded Python 3

File details

Details for the file tokenxygen-0.5.2.tar.gz.

File metadata

  • Download URL: tokenxygen-0.5.2.tar.gz
  • Upload date:
  • Size: 65.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for tokenxygen-0.5.2.tar.gz
Algorithm Hash digest
SHA256 3e769a9267fa68f5e53fd3c7dbbb5c0baa2cc5dc82402df986a4d5f434a074f9
MD5 615d3386877c5244f65fe9e44a72b93b
BLAKE2b-256 0d861f271576f470bbd29b0f1abac54056b1932a7cece09e16d5fcae0e7683a3

See more details on using hashes here.

File details

Details for the file tokenxygen-0.5.2-py3-none-any.whl.

File metadata

  • Download URL: tokenxygen-0.5.2-py3-none-any.whl
  • Upload date:
  • Size: 58.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for tokenxygen-0.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8b6d103e5db57a57730da43ba73c5d7e14dd5da28678c0d11785c19af64ccacd
MD5 85f53f4f85f83f0ff956d790644e4536
BLAKE2b-256 8ec7088ac5d0fa793a2c1bb029027db43d1c5137b1b8ea1f07feebe55078fb05

See more details on using hashes here.

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