Skip to main content

Track and optimize your LLM spend — find duplicates, wasted tokens, and cost savings.

Project description

TokenLens Python SDK

Track and optimize your LLM spend. Find duplicate prompts, wasted tokens, and cost savings — automatically.

Install

pip install tokenlens

Quick Start (2 lines)

import tokenlens
tokenlens.init(api_key="tl_...")  # Get your key at tokenlens.co/app → Account

# All OpenAI and Anthropic calls are now tracked automatically.
# No other code changes needed.

How It Works

The SDK patches the OpenAI and Anthropic Python clients to capture usage metadata (model, token counts, prompt hashes) after every completion call. Events are batched in the background and sent to your TokenLens dashboard every 30 seconds.

What's captured: model name, token counts, prompt text (for duplicate detection), cost, timestamps.

What's NOT captured: API keys, response content beyond the first 2KB, any data outside of LLM calls.

With OpenAI

import tokenlens
tokenlens.init(api_key="tl_...")

from openai import OpenAI
client = OpenAI()

# This call is automatically tracked
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Summarize this article"}]
)

With Anthropic

import tokenlens
tokenlens.init(api_key="tl_...")

import anthropic
client = anthropic.Anthropic()

# This call is automatically tracked
message = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Explain quantum computing"}]
)

With LiteLLM

import litellm
from tokenlens.litellm_callback import TokenLensCallback

litellm.callbacks = [TokenLensCallback(api_key="tl_...")]

# Every litellm.completion() call is now tracked
response = litellm.completion(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello"}]
)

LiteLLM Proxy (config.yaml)

litellm_settings:
  callbacks:
    - tokenlens

environment_variables:
  TOKENLENS_API_KEY: "tl_..."

Manual Tracking

For custom LLM clients or pre-computed metrics:

import tokenlens
tokenlens.init(api_key="tl_...")

tokenlens.track(
    model="gpt-4o",
    prompt="Summarize the quarterly report",
    tokens_in=150,
    tokens_out=500,
    cost=0.0035,
    caller_tag="report-service",
)

Environment Variables

Variable Description
TOKENLENS_API_KEY Your API key (alternative to passing api_key=)
TOKENLENS_API_BASE Custom API endpoint (default: https://tokenlens.co)

Configuration

tokenlens.init(
    api_key="tl_...",
    auto_patch=True,       # Patch OpenAI/Anthropic automatically (default: True)
    batch_size=50,         # Events per batch (default: 50)
    flush_interval=30,     # Seconds between flushes (default: 30)
    debug=False,           # Enable debug logging (default: False)
)

SDK Stats

print(tokenlens.stats())
# {'queued': 3, 'total_sent': 147, 'total_errors': 0}

Graceful Shutdown

Events are flushed automatically on process exit. For explicit control:

tokenlens.flush()     # Send all pending events now
tokenlens.shutdown()  # Flush and stop background thread

Privacy

  • Prompt text is sent for duplicate detection (hashed on our servers, never stored in plain text)
  • Completion text is capped at 2KB and used only for quality analysis
  • No API keys, secrets, or credentials are ever captured
  • All data is encrypted in transit (HTTPS/TLS)
  • Data is scoped to your team — never shared across accounts

Requirements

  • Python 3.8+
  • No required dependencies (works standalone)
  • Optional: openai>=1.0, anthropic>=0.20, litellm>=1.0

Links

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

tokenlens-0.2.0.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

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

tokenlens-0.2.0-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file tokenlens-0.2.0.tar.gz.

File metadata

  • Download URL: tokenlens-0.2.0.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for tokenlens-0.2.0.tar.gz
Algorithm Hash digest
SHA256 7f0b962e6cbcf68470818dfb6135cdf96d0e89787a785f1cb21b4a0513ad1d53
MD5 4dcf55718eb74c2c4b1d73a6a9d4d0c2
BLAKE2b-256 479b93ad483cae352c9e21529ebf76c78bc9df578e472f523671dfd2dd25a3c1

See more details on using hashes here.

File details

Details for the file tokenlens-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: tokenlens-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 11.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for tokenlens-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b640fc9bf0a94c9b48b17e409b9dd1c13a53e724bf6e3b0a740bac8f674cf260
MD5 b9e4b7ac470970f5f59a2910301fa01a
BLAKE2b-256 2b353067f4698aa7e30bdfcbf8fc977253b816e4c870e9341ad8dd9306c392f3

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