Skip to main content

Token Meter - Lossless Prompt Optimizer for LLMs

Project description

TKNMTR: Token Meter 🔢

Lossless Prompt Optimizer for LLMs

Context sync (2026-02-20): TKNMTR Auto is positioned as a semantic, policy-driven, multi-provider gateway (not a closed vendor "Auto" mode).

Reduce your LLM API costs by 30-50% by automatically compressing prompts while preserving semantic fidelity.

Python 3.12+ License: MIT

Features

  • 🔧 Multi-Provider Support - Works with Gemini, OpenAI, and Claude
  • 💰 Cost Estimation - Calculate savings across 30+ LLM models
  • Fidelity Verification - Ensures optimized prompts preserve meaning
  • 🚀 Multiple Interfaces - SDK, CLI, and REST API
  • 📊 Built-in Test Suite - Validate optimization quality

Auth Modes (important)

  • /v1/optimize uses x-api-key
  • /v1/chat/completions uses Authorization: Bearer tkn_xxx
  • /v1/billing/* uses Supabase JWT Bearer user token

Quick Start

Installation

# From PyPI (when published)
pip install tknmtr

# From source
pip install -e .

# With API server
pip install -e ".[api]"

# With dev tools
pip install -e ".[dev]"

Configuration

Create a .env file:

# At least one API key required
GEMINI_API_KEY=your_gemini_key
OPENAI_API_KEY=your_openai_key      # Optional
ANTHROPIC_API_KEY=your_anthropic_key # Optional

Usage

Python SDK

from tknmtr import TKNMTR

# Initialize
client = TKNMTR()

# Optimize a prompt
result = client.optimize("Hello! Can you please write me a Python script that reads a CSV file?")

print(result.optimized)        # "Write Python script: read CSV file."
print(f"{result.savings_pct}% saved")  # "42.5% saved"
print(result.fidelity)         # "PASS"

# Batch optimization
results = client.batch([
    "Please write me a query...",
    "Can you help me with..."
])

# List available models
models = client.list_models()

Command Line

# Optimize a single prompt
tknmtr -p "Hello! Please write me a Python script..."

# Interactive REPL mode
tknmtr -i

# Optimize from file
tknmtr -f prompt.txt -o result.json

# Run test suite
tknmtr -l 3 -o results.json

# List available models and pricing
tknmtr --list-models

# Use specific provider and target model
tknmtr --provider openai -m claude-opus-4 -l 1

Web UI

Start the server and open http://localhost:8000 in your browser:

uvicorn tknmtr.api:app --reload

The Web UI provides a modern interface for testing prompts with real-time optimization.

REST API

# Start the server
uvicorn tknmtr.api:app --reload

# Or with docker
docker-compose up

Endpoints:

Method Path Description
POST /v1/optimize Optimize single prompt
POST /v1/batch Batch optimization
GET /v1/models List available models
GET /health Health check

Example request:

curl -X POST http://localhost:8000/v1/optimize \
  -H "Content-Type: application/json" \
  -d '{"prompt": "Hello! Please help me...", "target_model": "gpt-5.2"}'

Supported Models (30+)

Provider Models
OpenAI gpt-5.2, gpt-5-mini, gpt-5.3-codex
Anthropic claude-opus-4.6, claude-sonnet-5, claude-haiku-4.5
Google gemini-3-pro, gemini-3-flash
Mistral mistral-large-3, mistral-medium-3
DeepSeek deepseek-v3, deepseek-r1
xAI grok-2, grok-2-vision
Meta llama-3.1-8b, llama-3.3-70b, llama-3.1-405b

How It Works

  1. Compression: Uses an LLM to rewrite prompts more concisely
  2. Fidelity Check: Another LLM verifies the meaning is preserved
  3. Token Counting: Uses tiktoken for accurate token estimation
  4. Cost Calculation: Estimates savings based on model pricing

Why not just use provider "Auto"?

Provider "Auto" modes are usually black-box decisions inside a single ecosystem. TKNMTR acts as a semantic gateway you control:

  • Multi-provider routing (OpenAI, Anthropic, Gemini, etc.)
  • Transparent and configurable policies based on workload intent
  • Unified auth, usage, and billing controls at one gateway layer
  • Business-optimized decisions (cost, latency, quality) instead of vendor-only optimization

Development

# Install dev dependencies
pip install -e ".[dev]"

# Run tests
pytest

# Lint
ruff check src/

# Type check
mypy src/

License

MIT License - see LICENSE


Made with 🫀 by Francisco Vozzi

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

tknmtr-1.0.0.tar.gz (64.4 MB view details)

Uploaded Source

Built Distribution

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

tknmtr-1.0.0-py3-none-any.whl (51.0 kB view details)

Uploaded Python 3

File details

Details for the file tknmtr-1.0.0.tar.gz.

File metadata

  • Download URL: tknmtr-1.0.0.tar.gz
  • Upload date:
  • Size: 64.4 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for tknmtr-1.0.0.tar.gz
Algorithm Hash digest
SHA256 2049c84ed5d61657a81b666fb7402687b987b562b497c8716a3020ebd08db286
MD5 246ad57cdb88aca18c6b1fda9c59c4f8
BLAKE2b-256 bf78a511fb18c7dc8ccb91b8b893bf5dbfb4b7dde3342883d571ddafaaf58f96

See more details on using hashes here.

File details

Details for the file tknmtr-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: tknmtr-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 51.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for tknmtr-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e79dd9cc8c893210b17a04694fdc33b57f34f416e8360d35a7f131b4ba8424a7
MD5 b8ad912d50c74b080be2946488aafa0b
BLAKE2b-256 5d83102a08152bd7588b06c720622c8400cc9e4ac443d565139f4df83bbd350a

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