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, OTel SDK JSONL, and raw
OpenAI or Anthropic request/response logs. 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, shared content sent behind different openings, per-session values that stop a prompt being reused across sessions, and prompts that never ask a cache-on-request provider to cache them.
- 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 diagnoseCLI, 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-session drift: they now find the TradingAgents problems on their own (case study, section 6)
- Counterfactual pricing with per-model read and write rates and dollar ranges
- Raw provider SDK log ingest (OpenAI Responses, Chat Completions, Anthropic Messages), tool-schema bloat detector
- A detector for prompts that never ask Anthropic to cache them
- Public demo deployment (
render.yamlis ready; see demo/) - Case study on an Anthropic-model agent (the harness is ready: experiments/tradingagents-claude)
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.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| tokenaudit-0.4.0.tar.gz | 2.9 MB | With provenance | Details for tokenaudit-0.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| tokenaudit-0.4.0-py3-none-any.whl | Python 3 | none | any | Details for tokenaudit-0.4.0-py3-none-any.whl |
Total release size: 3.0 MB
Release files / tokenaudit-0.4.0.tar.gz
| Download URL | tokenaudit-0.4.0.tar.gz |
|---|---|
| Size | 2.9 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a43504bd4b364daa82255b0a83ffb12f919d71e7e73eda6fab48ecf391b34fec
|
|
BLAKE2b-256 checksum How to use checksums |
a9d3db1fc8160f3bab6bd4dcd6e86aa1236a97a15a6e8d08b738d97952cd5f8c
|
| 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 logRelease files / tokenaudit-0.4.0-py3-none-any.whl
| Download URL | tokenaudit-0.4.0-py3-none-any.whl |
|---|---|
| Size | 84.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2bb6df459c3cbee8b408ed6f2322a89ff8b3b90bfa106e46c9f0f57c57b3de01
|
|
BLAKE2b-256 checksum How to use checksums |
97e358ee7cae98bc63f4193cd55fe5f6b30b1e93afaadf859c13584aaab51d62
|
| 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 logSecurity for tokenaudit 0.4.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.
Attestation for tokenaudit-0.4.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 logA 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.