Skip to main content

agent-token-forensics

CI

Read-only JSONL token, prompt-cache, compaction-point, and retry-loop forensics for autonomous AI coding agents (Claude Code, Codex CLI, Cursor, Gemini CLI, Antigravity).

Long agent sessions silently burn 30–55% of their quota on uncompacted context tails, broken prompt caches, and identical retry loops. agent-token-forensics inspects your local JSONL transcripts strictly read-only with zero external dependencies (pure Python 3.10+ stdlib, zero network calls) and pinpoints the exact turns costing you money.

  • Multi-vendor JSONL schema support: Claude Code / Anthropic (usage.input_tokens, cache_read_input_tokens), OpenAI / Codex / Cursor (prompt_tokens, prompt_tokens_details.cached_tokens), Gemini (usageMetadata), and Antigravity flat transcripts.
  • Compaction inflection finder: identifies the exact turn where context crosses --compact-threshold (default 80 000 tokens) and estimates realistic token & USD savings from compacting there.
  • Prompt-cache bust detector: catches turns where a warm cache (>=70% hit ratio) suddenly collapses (<30%).
  • Retry-loop & tool-bloat forensics: flags >=3 consecutive identical tool invocations and ranks tools by attributed token burn.

60-Second Install & Zero-Install Run

PyPI status: The agent-token-forensics package name is uncontested on PyPI for the upcoming PyPI release (pip install agent-token-forensics / pipx install agent-token-forensics once published). Until then, use zero-install or install from source below.

Linux / macOS (bash)

# Option 1: Zero-Install Direct Run (pure Python 3.10+ stdlib)
git clone https://github.com/BlackBadHope/agent-token-forensics
cd agent-token-forensics
python3 agent_token_forensics_cli.py analyze ./transcripts --markdown
python3 agent_token_forensics_cli.py analyze ./session.jsonl --json

# Option 2: Isolated CLI Install (uvx / pipx / .venv — PEP 668 safe)
uvx --from . agent-token-forensics analyze ./transcripts --markdown
pipx install . && agent-token-forensics analyze ./transcripts --markdown

python3 -m venv .venv
.venv/bin/python -m pip install --no-deps .
.venv/bin/agent-token-forensics analyze ./transcripts --markdown

Windows (pwsh 7)

# Option 1: Zero-Install Direct Run (pure Python 3.10+ stdlib)
git clone https://github.com/BlackBadHope/agent-token-forensics
Set-Location .\agent-token-forensics
python agent_token_forensics_cli.py analyze .\transcripts --markdown
python agent_token_forensics_cli.py analyze .\session.jsonl --json

# Option 2: Isolated virtual environment (.venv) from source
python -m venv .venv
.\.venv\Scripts\python.exe -m pip install --no-deps .
.\.venv\Scripts\agent-token-forensics.exe analyze .\transcripts --markdown

MCP Server (stdio)

agent-token-forensics ships a zero-dependency, strictly read-only MCP JSON-RPC 2.0 server over stdio (pure Python 3.10+ stdlib, zero writes, zero telemetry, zero network calls).

Exposed Read-Only Tools:

  • analyze_transcripts — analyzes a JSONL transcript file or directory (path, optional compact_threshold, post_compact_target, output_format: "json" | "markdown") and returns cache_bust_events, compaction_inflection_points, retry_loops, top_tools_by_tokens, and cost/savings summary.
  • forensics_about — returns version, supported transcript schemas (Claude Code/Anthropic, OpenAI/Codex/Cursor, Gemini usageMetadata, Antigravity), and zero-telemetry / read-only guarantees.

Run directly from a cloned repo or source install:

python -m agent_token_forensics.mcp_server
# or via the zero-install shim / installed CLI:
python agent_token_forensics_cli.py mcp-serve
agent-token-forensics mcp-serve

Claude Code / Cursor / Antigravity MCP config (mcpServers):

{
  "mcpServers": {
    "agent-token-forensics": {
      "command": "python",
      "args": ["/path/to/agent-token-forensics/agent_token_forensics_cli.py", "mcp-serve"]
    }
  }
}

Or if installed from source into your environment (or via uvx --from git+https://github.com/BlackBadHope/agent-token-forensics):

{
  "mcpServers": {
    "agent-token-forensics": {
      "command": "python",
      "args": ["-m", "agent_token_forensics.mcp_server"]
    }
  }
}

Troubleshooting & Clean Uninstall

1. Ubuntu / Debian 24.04: error: externally-managed-environment (PEP 668)

Modern Debian/Ubuntu blocks global pip install under PEP 668. Use either clean option (no --break-system-packages needed):

# Option A: Zero-install direct run (pure Python 3.10+ stdlib)
python3 agent_token_forensics_cli.py analyze ./session.jsonl --markdown

# Option B: Isolated virtual environment (zero external deps)
python3 -m venv .venv
.venv/bin/python -m pip install --no-deps .
.venv/bin/agent-token-forensics analyze ./transcripts --markdown

2. Empty Directories, Zero-Turn Files, and Corrupt JSONL Lines

agent-token-forensics is strictly read-only and designed to survive messy real-world logs:

  • Empty directory or no .jsonl files: exits 0 with a clean zero-turn summary (files_scanned: 0, turns: 0) rather than crashing.
  • Truncated, malformed, or non-UTF-8 JSONL lines: invalid lines and non-usage metadata events are skipped safely (errors="replace"), while all valid turns in the same file are still analyzed.
  • Non-existent path: fails explicitly with exit 2 and an error message on stderr.
  • No compaction recommendations shown: your sessions stayed under --compact-threshold (default 80000 tokens); lower it (e.g. --compact-threshold 40000) for smaller context windows.

3. Clean Uninstall / Reset-to-Zero

agent-token-forensics creates zero hidden state directories, caches, or background daemons. To uninstall and reset to zero:

# Linux / macOS
python3 -m pip uninstall -y agent-token-forensics 2>/dev/null || true
rm -rf ./agent-token-forensics   # or remove your .venv directory
# Windows (PowerShell 7)
python -m pip uninstall -y agent-token-forensics
Remove-Item -Recurse -Force ".\agent-token-forensics" -ErrorAction SilentlyContinue

Full Production Suite, Bundles & Cloud Control Plane

Development & Verification

ruff check agent_token_forensics agent_token_forensics_cli.py tests agensi-skill
python -m unittest discover -s tests -p "test_*.py" -v

License

MIT — see LICENSE.

Metadata

Release files for agent-token-forensics 0.1.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 agent-token-forensics 0.1.0
File Size Uploaded Provenance Actions
agent_token_forensics-0.1.0.tar.gz 25.1 kB Without provenance Details for agent_token_forensics-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for agent-token-forensics 0.1.0
File Interpreter ABI Platform Actions
agent_token_forensics-0.1.0-py3-none-any.whl Python 3 none any Details for agent_token_forensics-0.1.0-py3-none-any.whl

Total release size: 42.5 kB

Release files / agent_token_forensics-0.1.0.tar.gz

Download URL agent_token_forensics-0.1.0.tar.gz
Size 25.1 kB
Tags Source
SHA-256 checksum
How to use checksums
b11f567f29feb600c6aad7740dce0980c9ac0db65078a1ac1d83ac509e5fe401
BLAKE2b-256 checksum
How to use checksums
5d445099d5c6f03abc5ed9ba488f5cf07fe1d21e4a9430c02b6fdce9d310cf17
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / agent_token_forensics-0.1.0-py3-none-any.whl

Download URL agent_token_forensics-0.1.0-py3-none-any.whl
Size 17.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e10d626e43dfdf193d1c8105298f815a5a485609a99692a12b743e86166741a3
BLAKE2b-256 checksum
How to use checksums
b2c1dd2923cb30975f4ba734fd91061309f668abe34c2d900638942870b52ce4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for agent-token-forensics 0.1.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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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