Skip to main content

tokenaudit

Root-cause diagnosis of wasted LLM tokens — from the traces your agents already emit.

Status: alpha (0.2.0). Interfaces may change before 1.0.

Every LLM cost tool can tell you how much you spent and who spent it. Almost none can tell you why a specific request wasted money. The biggest lever on an LLM bill — prompt caching, up to a 10× discount on repeated input — fails silently: change one byte of a cached prefix, drop below a model's minimum cacheable size, let a timer expire, and the provider just bills you full price. No error, no warning. There are documented cases of agents running at ~0% cache hit rate for months, at 30–40% unnecessary cost, because of a single line of prompt construction.

tokenaudit reads the traces agents already produce and, for each expensive request, answers four questions: what was wasted, why, what it cost, and what to change.

It works: diagnosing one of those documented cases produced a 9-file patch that took TradingAgents' cache hit rate from 7.5% to 32.4% (4.3×) in a controlled, pre-registered before/after experiment — the first case study.

Quickstart

pip install tokenaudit
tokenaudit diagnose --sample          # diagnose the bundled demo trace
tokenaudit diagnose mytraces/ --html report.html

Zero runtime dependencies; reads OTLP/JSON files and OTel SDK JSONL. Try it on this repo's real captured runs: tokenaudit diagnose corpus/captured/runs. Prefer a browser? The demo app diagnoses a dropped trace; uploads are parsed in memory and never stored.

The plan, in one line each:

  • Ingest standard traces — OpenTelemetry GenAI in all three of its cache-field naming generations, OpenInference, raw provider SDK logs — into one normalized accounting model.
  • Run a library of deterministic detectors for the silent failure modes: prefix drift, sub-minimum cache blocks, model switches, TTL expiry, breakpoint and lookback limits, tool-schema churn and bloat, context bloat, retry storms.
  • Price each finding against a counterfactual — what the bill would have been under the fix — as a bounded range, never a point guess.
  • Publish per-detector precision and recall against a labeled corpus, so you know how much to trust each finding.

Catch regressions in CI

Prompt-caching regressions are silent: no test fails when a reordered tool list doubles your input bill. If CI already produces traces, fail the pull request instead (full docs):

- uses: yashmyeole/tokenaudit@v0.2.0
  with:
    traces: traces/
    baseline: tokenaudit-baseline.json   # tokenaudit check traces/ --baseline ... --update

This repo gates itself: every build re-diagnoses the captured TradingAgents runs against tokenaudit-baseline.json.

Prior art

This space is crowded, and being precise about what already exists is the point of this section.

Tool What it does
CacheLens Closest prior art. Dev-time SDK wrapper that meters cache hit rate and prefix stability per prompt layer in apps you instrument. Doesn't read existing traces, and prefix stability is its one diagnosis.
Helicone Observability proxy: cost and token accounting per user/session/feature, including cache token counts
Langfuse Open-source LLM engineering platform: tracing, evals, per-trace cost accounting
Datadog LLM Observability Enterprise monitoring: per-request token and cost tracking, quality-issue clustering
Braintrust Evals and observability; its Loop assistant root-causes quality failures
LiteLLM Gateway with budgets, rate limits and spend tracking across providers
ccusage CLI usage and cost reporting from Claude Code's local logs
Cloudflare AI Gateway Platform-level caching, spend caps and alerts

All of these answer "how much", and several now count cache tokens. The ones that do automatic root-cause analysis target response quality, not cost. The closest tool diagnoses one failure mode, for one integration style, going forward only. What none of them do is take the telemetry any agent already emitted — no code changes, any vendor — and say: these 44 requests missed cache because the system prompt embeds a timestamp; here is the evidence, and here is what it cost. That attribution step is this project.

If you know a tool that already does this, please open an issue — I would genuinely rather find out now.

What tokenaudit is not

  • Not a dashboard. The tools above already report spend well.
  • Not a proxy, gateway, or wrapper. It reads traces after the fact and never sits in the request path, so it can't change what it measures and doesn't need your code.
  • Not an LLM reading your logs. Verdicts come from explicit rules with unit tests and a published false-positive rate (decision 001).
  • Not a savings guarantee. Estimates are ranges with stated assumptions, and requests whose telemetry fails a plausibility check are flagged as unpriceable, not silently priced.

Planned layout

src/tokenaudit/
  ingest/          trace readers + schema normalization
  model/           normalized requests/sessions + token accounting
  detect/          the diagnosis engine (deterministic detectors)
  counterfactual/  what the bill would have been under a fix
  report/          CLI, JSON and HTML output
  ci/              cost-regression gate for pipelines

Roadmap

  • Prior-art survey, positioning, project scaffold
  • Labeled trace corpus: synthetic known-answer fixtures + captured runs from real public agents
  • Trace reader and normalizer with a data-quality gate (bad telemetry is refused, not zero-filled)
  • Provider caching rulebook: minimums, TTL semantics, invalidation rules — versioned, with sources
  • Detectors, starting with prefix drift, sub-minimum blocks, and model/version switch
  • Evaluation harness: per-detector results published in eval/results.md
  • tokenaudit diagnose CLI, HTML report, cache health score
  • CI cost-regression mode: fail a build that tanks the cache hit rate (docs/ci.md)
  • Demo app: drag a trace in, get the report (demo/)
  • Case studies on public agents, with reproduction commands (TradingAgents: 4.3× cache hit rate)
  • Detectors for misordered shared context and cross-run prefix drift
  • Counterfactual pricing with per-model read rates and dollar ranges
  • Raw provider SDK log ingest, tool-schema bloat detector
  • Public demo deployment, case study on an Anthropic-model agent

Development

python -m venv .venv && source .venv/bin/activate
pip install -e ".[dev]"
pre-commit install
pytest

To release, bump the version in pyproject.toml and src/tokenaudit/__init__.py, update CHANGELOG.md, and push a vX.Y.Z tag.

License

Apache-2.0

Metadata

Release files for tokenaudit 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tokenaudit 0.2.0
File Size Uploaded Provenance Actions
tokenaudit-0.2.0.tar.gz 2.8 MB With provenance Details for tokenaudit-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for tokenaudit 0.2.0
File Interpreter ABI Platform Actions
tokenaudit-0.2.0-py3-none-any.whl Python 3 none any Details for tokenaudit-0.2.0-py3-none-any.whl

Total release size: 2.9 MB

Release files / tokenaudit-0.2.0.tar.gz

Download URL tokenaudit-0.2.0.tar.gz
Size 2.8 MB
Tags Source
SHA-256 checksum
How to use checksums
acecc904019af817e4f1bcb7fb08aff483061c74c95af25e35251624a660e8cc
BLAKE2b-256 checksum
How to use checksums
fbdccb7836d1858bac9a4ee56860e383f68242b2ac4bfaffbe11f6319a893ddf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 12, 2026.

Transparency log

Release files / tokenaudit-0.2.0-py3-none-any.whl

Download URL tokenaudit-0.2.0-py3-none-any.whl
Size 66.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f381204f7cb6f7bd0ceba62d6b4b9bb7065f56832ca5c59dba94ad38a5387cde
BLAKE2b-256 checksum
How to use checksums
3774445d445f73b94af37c9d30e24c322d4558f91e8a7a5271055a5285e7fc59
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 12, 2026.

Transparency log

Security for tokenaudit 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for tokenaudit-0.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 12, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page