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.1.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.1.0-py3-none-any.whl (51.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tknmtr-1.1.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.1.0.tar.gz
Algorithm Hash digest
SHA256 fa5e060d0fa8f1216f470f1eb448acbc5bcc57b42e124005565515d6de2c0cc1
MD5 8514e8ac73d259f7dfe3699c43c4ab16
BLAKE2b-256 4a36c44e1f2b256c59194f250174c8796573cbb24d917f76bf34f26e9f8d81b7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tknmtr-1.1.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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 95d515f1215cf1f92d07aead6dc85551e47f7725a75ce91293b93563d2388b82
MD5 8c5520b729fafbe72c54cc5cdcf4fdb9
BLAKE2b-256 2fb4330b46b898bbb6081085c9f3c1c519992517f6555d4ac13920c0cf5f71b7

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