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.1.0.tar.gz (762.3 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.1.0-py3-none-any.whl (20.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for promptmeter-0.1.0.tar.gz
Algorithm Hash digest
SHA256 109d05e7c31f11fd88fa126347e535908f5d7e4eab59f21fab26e3254c454d4d
MD5 46920648af2852d39ee2b7bbeab73889
BLAKE2b-256 862f60bd8ebc1a1a4c338dc0c8095d6b34d2b36a130cd3c154c67010eb5c9d27

See more details on using hashes here.

Provenance

The following attestation bundles were made for promptmeter-0.1.0.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.1.0-py3-none-any.whl.

File metadata

  • Download URL: promptmeter-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 20.6 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 79a522710b0d3646a241ddb1056469e0ed9976d36cb81ac266217fbd37dc2e01
MD5 de6e3860c0024b9af1425781ac23ddd7
BLAKE2b-256 587d02660013a4b548cb6d462cf2bc648986ccbd227602bc594a8bd785544cfa

See more details on using hashes here.

Provenance

The following attestation bundles were made for promptmeter-0.1.0-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