Skip to main content

Pre-flight token analyzer and usage tracker for Claude — gas optimization for LLM prompts

Project description

Token Tracker

Gas optimization for LLM prompts. A pre-flight analyzer + usage tracker for the Claude API. Catches vague, wasteful, or open-ended prompts before they hit the wire — and suggests a leaner rewrite.

PyPI version Python License: MIT CI


The idea

Solidity devs use a gas estimator before deploying. LLM devs send blind prompts and wait for the bill.

Token Tracker fixes that. It wraps the Anthropic SDK and adds three layers:

Layer What it does Analogy
Token Tracker Logs every API call — tokens in/out, cost, session Gas meter
Pre-flight Analyzer Scores your prompt before sending — flags 8 patterns of waste Gas estimator + linter
Prompt Optimizer Rewrites bad prompts into leaner versions, side-by-side Compiler optimizer

All analysis is offline — zero extra API calls. Storage is local SQLite.


Demo

Token Tracker demo

Run tt demo yourself to see all features without using any API credits.


Quickstart

pip install promptmeter
export ANTHROPIC_API_KEY="sk-ant-..."

As a drop-in for the Anthropic SDK

from token_tracker import TrackedClient

client = TrackedClient(session_name="my-app")

response = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=512,
    messages=[{"role": "user", "content": "List 3 Python web frameworks."}],
)

That's it. Every call is now analyzed, logged, and reportable.

As a standalone CLI

tt analyze "Please could you kindly tell me something about Python"
tt report                # today's usage summary
tt report --week         # last 7 days
tt cost-model            # cost breakdown by model
tt top-waste             # most expensive flagged prompts
tt sessions              # list named sessions
tt demo                  # guided walkthrough — zero API calls

What the pre-flight catches

8 rules, each with severity and a suggested fix:

Rule Severity Detects
VAGUE_INTENT high "tell me something about X" — no clear task
OPEN_ENDED_TASK high "explain everything about X" — unbounded scope
WALL_OF_TEXT high unstructured 500+ token dumps
MISSING_FORMAT medium asks for a list without specifying format
MISSING_SCOPE medium "explain X" with no length/depth limit
REDUNDANT_CONTEXT medium the same sentence twice
FILLER_WORDS low "please", "could you kindly", "thank you in advance"
AMBIGUOUS_PRONOUN low "fix it" — fix what?

Each rule contributes to a 0–100 efficiency score. Below 60 triggers a warning. Below 40 hard-blocks (in interactive mode).


Try the walkthrough

tt demo

Runs through every feature with three example prompts (good, bad, terrible) and renders the live dashboard. No API calls, safe to run anytime.


How it actually saves tokens

Without Token Tracker With Token Tracker
Vague prompt → Claude asks clarifying questions → 2-3 round trips Pre-flight catches vagueness → fix once → one round trip
"explain X" → Claude writes 800 words when you needed 100 MISSING_SCOPE flags it → add "in 3 sentences" → 6× cheaper output
Same context pasted in every message REDUNDANT_CONTEXT warns → use prompt caching → 90% cheaper input
No visibility into spend → no behavior change Daily report shows cost per session → habits adjust

Conservative real-world reduction: 30–50% of token spend for a developer who acts on warnings.


Configuration

TrackedClient(
    api_key="sk-ant-...",
    session_name="my-app",      # tag this session in the DB
    analyze=True,               # run pre-flight before every call
    interactive=True,           # prompt user on flagged prompts (s/o/c menu)
    warn_threshold=60,          # show warning when score drops below this
)

Where data lives

A single SQLite file at ~/.token_tracker/usage.db. Two tables: sessions, usage_records. Open it with DB Browser for SQLite to poke around.

Token Tracker stores a hash of each prompt — never the full text. Your prompts stay in your process.


Contributing

The lowest-friction first contribution is adding a new rule in token_tracker/analyzer/rules.py — about 10 lines of Python. See CONTRIBUTING.md.

Found a false positive? Open an issue with the prompt and the rule that misfired.


License

MIT — see LICENSE.

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

promptmeter-0.2.1.tar.gz (767.5 kB view details)

Uploaded Source

Built Distribution

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

promptmeter-0.2.1-py3-none-any.whl (24.4 kB view details)

Uploaded Python 3

File details

Details for the file promptmeter-0.2.1.tar.gz.

File metadata

  • Download URL: promptmeter-0.2.1.tar.gz
  • Upload date:
  • Size: 767.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for promptmeter-0.2.1.tar.gz
Algorithm Hash digest
SHA256 1084e9b9da048598991dbfe048ee4639cc104c7b68e235678e86ff3c3b9f7996
MD5 c6e404e230f955b44afca419a1d72577
BLAKE2b-256 f4f86fa473572709e5e9ce6de7d5f8abdde6b5a58471cd2ef6ebe5c6dc11035a

See more details on using hashes here.

Provenance

The following attestation bundles were made for promptmeter-0.2.1.tar.gz:

Publisher: publish.yml on emam07/Token-tracker

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file promptmeter-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: promptmeter-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 24.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for promptmeter-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 278d68ae104c581fa0170b6a8d96bcfb9c5f43160d20aa2368626e9e9efbba3c
MD5 02fa2421a3d5ba03792f8c6a22e38102
BLAKE2b-256 75e0301ff97123eafa3aec9380864e172fd224a1f0f3663bc3a2a9b9434eae4f

See more details on using hashes here.

Provenance

The following attestation bundles were made for promptmeter-0.2.1-py3-none-any.whl:

Publisher: publish.yml on emam07/Token-tracker

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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