Retrieved article excerpt
Open article · Retrieved 2026-10-12T12:27:54.675241+00:00
[tokenleak logo](https://github.com/Pazificateur69/tokenleak/blob/main/assets/logo.png)
# tokenleak
**Find and plug your coding agent's token leaks.**
Not leaked API keys: the tokens your AI agent burns on context it didn't need.
[tests](https://github.com/Pazificateur69/tokenleak/actions/workflows/test.yml)
[Security: A on Skills Directory](https://www.skillsdirectory.com/skills/pazificateur69-tokenleak)
[zero dependencies](https://camo.githubusercontent.com/aae95fbaa83bc6a3f4597f3a75da45ea46ec236fc324617f0e5a2f15e07fe750/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f646570656e64656e636965732d302d627269676874677265656e)
[MIT license](https://camo.githubusercontent.com/b8cadaa967891081f8f165695470689986c028821dd8a040132f6e661795dc0d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c7565)
[Website](https://pazificateur69.github.io/tokenleak/) · [Install](https://github.com/Pazificateur69/tokenleak#install) · [Audit](https://github.com/Pazificateur69/tokenleak#the-audit) · [Benchmark](https://github.com/Pazificateur69/tokenleak#benchmark) · [Rules](https://github.com/Pazificateur69/tokenleak#the-rules) · [Français](https://github.com/Pazificateur69/tokenleak/blob/main/README.fr.md)
[Animation: a 12k-token test log read at call 3 is re-sent by every later call (252,000 tokens); with tokenleak it is bounded to 800 tokens (16,800).](https://github.com/Pazificateur69/tokenleak/blob/main/assets/stack.svg)
**−32% cost over 80 A/B runs on Sonnet 5 with the rules, same pass rate (40/40). −20% to −28% on Haiku 4.5 with the read guard (two batches, 120 runs).** [Details](https://github.com/Pazificateur69/tokenleak#benchmark).
**Try it first, nothing to install:** see where your own tokens went in ten seconds (Python 3.8+, reads your local transcripts, sends nothing).
```
curl -fsSL https://raw.githubusercontent.com/Pazificateur69/tokenleak/main/skills/tokenleak/scripts/tokenleak.py | python3 - --since 7d
```
## Why
In an agent loop, **every tool result you let into context is re-sent on every later call** until the context is compacted. A 6k-token test log read at call 5 of a 60-call session gets paid ~55 more times. Every extra round-trip re-sends the whole context.
So the bill isn't driven by how chatty the agent is. It's driven by **what it lets into its context, and how early**. In the [benchmark](https://github.com/Pazificateur69/tokenleak#benchmark)'s runs without tokenleak, everything the model wrote (replies, tool calls, thinking) was 7% of a run's cost on Sonnet and 10 to 11% on Haiku (medians); the rest was context. Shorter replies save the small part. The audit shows that split on your own sessions.
tokenleak does two things:
1. **Rules** (a skill, ~770 tokens) that make any coding agent let in only what the next decision needs: locate then read, compute instead of reading data, bound every command's output, never re-read, batch independent calls, diffs over files, edit don't rewrite. With guardrails, so it never saves tokens by guessing or skipping verification.
2. **An audit** that reads *your own* Claude Code and Codex transcripts and shows where the tokens actually went: every oversized read, noisy log, wasted call and serial round-trip, priced by the **context tax** it imposed, with the rule that fixes it. Local, read-only, zero dependencies.
## Install
**Claude Code (plugin, recommended).** Rules injected at session start, after compaction, and into every subagent, plus a **read guard**: a whole-file read of a big text file (over ~24 KB) is stopped, and for code and Markdown the agent gets the file's outline instead (definitions or headings with line numbers) so it can read just the part it needs; passing `limit` always goes through. Adds the `/tokenleak` skill. Turn the guard off with `TOKENLEAK_GUARD=0`, or move its threshold with `TOKENLEAK_GUARD_BYTES`.
```
/plugin marketplace add Pazificateur69/tokenleak
/plugin install tokenleak@tokenleak
```
**Codex (plugin).** Same rules at session start (and in subagents where your Codex version fires that hook), plus the skill:
```
codex plugin marketplace add Pazificateur69/tokenleak
codex plugin add tokenleak@tokenleak
```
Then open `/hooks` in Codex to review and trust its lifecycle hook, and start a new thread.
**Any agent that supports Agent Skills** (Codex, Cursor, Gemini CLI, GitHub Copilot, Windsurf, Cline, OpenCode and [50+ more](https://github.com/vercel-labs/skills#supported-agents)):
```
npx skills add Pazificateur69/tokenleak -g
```
**Always on, any agent, no install.** Append the rules to your instructions file (`AGENTS.md`, `CLAUDE.md`, `GEMINI.md`, `.cursorrules`…):
```
curl -fsSL https://raw.githubusercontent.com/Pazificateur69/tokenleak/main/skills/tokenleak/SKILL.md | awk '/^## Audit/{exit} f>=2; /^---$/{f++}' >> AGENTS.md
```
## The audit
```
# no install: Python 3.8+, stdlib only, reads ~/.claude/projects and ~/.codex/sessions
curl -fsSL https://raw.githubusercontent.com/Pazificateur69/tokenleak/main/skills/tokenleak/scripts/tokenleak.py | python3 - --since 30d
```
The report starts with where the money went (`cost split`: context, read from the cache or written to it, versus output), then every leak priced by its estimated context tax.
Or install the CLI: `uvx --from git+https://github.com/Pazificateur69/tokenleak tokenleak` (or `pipx install git+https://github.com/Pazificateur69/tokenleak`), or run it from a clone: `python3 skills/tokenleak/scripts/tokenleak.py`. Or just ask your agent: *"audit my token leaks"*.
[tokenleak audit output: leak score, leak kinds with the rule that fixes each, top leaks](https://github.com/Pazificateur69/tokenleak/blob/main/assets/demo.svg)
| flag | |
| --- | --- |
| `--since 24h | 7d | 4w | 2026-10-01 | all` | time window (default `7d`) |
| `--here` / `--current` | sessions in this directory / only the latest one |
| `PATH…` | audit specific transcript files or folders instead of the defaults |
| `--project TEXT`, `--session ID`, `--agent all|claude|codex` | filters |
| `--compare 2026-10-01` | before vs after a date, e.g. the day you installed the rules: leak score and cost per call |
| `--top N` | individual leaks to list (default 10) |
| `--json` | machine-readable, for CI or dashboards |
| `--share` | small anonymized card for posting |
| `--html report.html` | self-contained HTML report to open, screenshot or send (add `--redact` before sharing) |
| `--redact` | hide commands, paths and project names |
| `--no-color` | plain output |
### Project rules from your own leaks
```
cd your-project && tokenleak suggest # or: python3 /path/to/tokenleak.py suggest
```
Reads the last 30 days of sessions in that project and prints a block to paste into its `CLAUDE.md` or `AGENTS.md`, written from what actually leaked there. Illustrative output (yours depends on your sessions):
```
<!-- tokenleak suggest: 23 sessions in shop-api since 2026-09-12, leak score 11.2% -->
## Token hygiene for this repo
Keep tool output out of the context; these are this repo's biggest repeat offenders:
- `npm test` printed ~9.1k tokens per run (23x). Run it as `npm test --silent 2>&1 | tail -n 40` (iterate on the one failing test).
- `git diff` printed ~3.0k tokens per run (5x). Run it as `git diff --stat` first, then `git diff -- <path>` for the files you need.
- Large files: `src/data/catalog.ts` (~18.2k tokens, read 14x). `grep -n` them, then read only the lines you need.
```
Only real project commands (tests, builds, installs, linters, git, docker) and files that exist in the project make the list. The skill offers to add it for you, and asks first.
The full report also ranks your projects by leak score (`by project`).
### Live leak meter (Claude Code status line)
See the current session's leak score while you work. Add to `~/.claude/settings.json`:
```
"statusLine": {
"type": "command",
"command": "python3 ~/.claude/plugins/marketplaces/tokenleak/skills/tokenleak/scripts/tokenleak.py statusline"
}
```
It prints `leak 3.1% A · ctx 84.2k · top big-read` (point the path at wherever `tokenleak.py` lives if you didn't install the plugin).
### What it finds
| leak | what happened | rule that fixes it |
| --- | --- | --- |
| `big-read` | a file read far bigger than the part you needed | Locate, then read |
| `noisy-shell` | tests, builds, installs or logs dumped into context | Bound every command |
| `re-read` | a file read again, unchanged, while still in context | Never re-read |
| `wasted-call` | the same call, same answer, again (incl. Claude Code's "file unchanged" round-trips) | Never re-read |
| `serial-call` | two independent reads/searches done one turn after the other | Batch independent calls |
| `wide-search` | a search that returned pages of matches | Narrow searches |
| `big-fetch` | a web page or MCP/API payload dumped whole | Narrow fetches |
| `rewrite` | a whole file rewritten to change a few lines | Edit, don't rewrite |
| `big-result` | a subagent or another tool returned a huge result | Narrow searches and fetches |
| `verbose-reply` | long recap prose | Answer lean |
It also shows what's **always loaded**: your `CLAUDE.md` / `AGENTS.md` / memory files and the base context (system prompt, tools, MCP servers) re-sent on every call, with their share of your cost. Not counted as leaks, but often the cheapest win.
### How it's computed
For each tool result: **context tax = avoidable tokens × (cache write + every later call in the same context × cache read)**, using each provider's relative prices (Claude: cache read 0.1×, cache write 1.25–2×, output 5×; Codex/GPT-5: cached 0.1×, output 8×). *Avoidable* is an estimate: only the part above a sane budget counts (e.g. a 6k-token read counts as 4k avoidable), the full size only for exact repeats. The audit can't know whether a big read was truly needed, so treat each finding as a suspect, not a verdict, and judge the trend with `--compare`. The **leak score** is the share of your total token cost that this estimate calls avoidable. Dollar amounts come from Claude Code's own per-session cost (prorated when a session started before the `--since` window), never from a price table we maintain.
Everything stays on your machine: no network, no telemetry, ~1,500 lines of stdlib Python you can read in ten minutes.
## Benchmark
A/B tests in Claude Code (`claude -p`): same prompt, a fresh copy of the same small repo for every run. The **only** difference is what tokenleak adds: the rules in the system prompt (what the plugin injects), and in the `guard` arm the plugin's read guard too. Every run is checked by a script: tests green, build passing, exact answer, feature working.
**Sonnet 5**, 10 runs per task and arm (two batches, 80 runs):
| task | without | with the rules | change | passed |
| --- | --- | --- | --- | --- |
| fix a failing test (chatty test logs) | $0.148 | $0.140 | −5% | 10/10 · 10/10 |
| answer a question about data in a big module | $0.205 | $0.160 | **−22%** | 10/10 · 10/10 |
| fix a build buried in 600 lines of output | $0.270 | $0.206 | **−24%** | 10/10 · 10/10 |
| add a feature *(held out, see below)* | $0.450 | $0.222 | **−51%** | 10/10 · 10/10 |
| **total** | **$1.07** | **$0.73** | **−32%** | **40/40 · 40/40** |
**Haiku 4.5**, 5 runs per task and arm, two batches (120 runs):
| batch | without | rules only | rules + read guard (v1.4) | rules + read guard with outline (v1.5) | passed |
| --- | --- | --- | --- | --- | --- |
| 1 | $0.513 | $0.505 (−1%) | **$0.408 (−20%)** | | 60/60 |
| 2 | $0.530 | | **$0.396 (−25%)** | **$0.379 (−28%)** | 60/60 |
Medians of the API-equivalent cost per run, as reported by Claude Code. On Sonnet, context tokens −17% and output tokens −11%. The screenshot under [The audit](https://github.com/Pazificateur69/tokenleak#the-audit) is the audit of 20 runs *without* the rules.
**Read it honestly.**
- **Runs vary a lot.** The first Sonnet batch gave −40%, a second batch the same day −21%;