Skip to main content

TokenOps for Claude Code

Why am I hitting my limit? One command names the prompts that cost you the most, says why, and what to change. Read from your own session logs, on your machine.

/tokenops:doctor

  Why am I hitting my limit?   (last 7 days, 12 sessions, 304 prompts, $235 at API list prices)

  37% of your Claude usage went to the first message after a break.
  39 times the cache had expired, so Claude reloaded the whole conversation.
  /compact before you step away would have saved ~31%.

  Your 3 most expensive prompts  (9% of usage)

  #2  Wed 21:56  project B  (prompt hidden)
      █████████░░░░░░░░░░░ 44% of that 5-hour window  |  24x your typical prompt
      CACHE EXPIRED  away 4h 45m, past the 1h cache: one call re-wrote your
                     whole 965k-token conversation to the cache at 2x the input price
      1 model call, paid $7.41  ->  /compact before leaving ~$0.52 (14x less) | /clear + note ~$0.27
  ...
  Where your usage goes
     41%  Re-reading context every call
     37%  First message after a break (cache reloaded)
     12%  Claude's output
     11%  Caching new context

  What to change
    1. Re-reading is 41%: every call reads the whole conversation again ... Use /clear when you
       switch to an unrelated task.
    2. Output is 12% of your usage, so a terse-output tool like caveman can save at most that ...

Install

In Claude Code:

/plugin marketplace add theagentplane/tokenops
/plugin install tokenops@tokenops

Then run /tokenops:doctor (last 7 days; /tokenops:doctor 30 for 30). Needs Python 3.9+ on your PATH; nothing to pip install.

Or, without installing anything, from any terminal with uv:

uvx tokenops doctor

(The tokenops package on PyPI is this CLI; the agent governance library is agent-tokenops.)

Reading the report

  • Prompts. One prompt you type can make many model calls (tool use, subagents). They are all counted under the prompt that started them. The doctor shows the 3 most expensive prompts, with the project they were typed in.
  • Tokens are the size of the whole conversation that one call sent, not a sum over calls. Models with a 1M-token context can carry a conversation that long.
  • CACHE EXPIRED means the call came more than the cache lifetime (1 hour on a Claude plan, 5 minutes with an API key) after the previous one, and wrote most of the conversation to the cache again at the cache-write price. The headline and the "First message after a break" row are the same number: the cache writes of those calls.
  • 5-hour windows are estimated from your first prompt in each, not read from Anthropic's meter. The cheaper costs are estimates: only the calls a break made expensive are re-priced.
  • Share yours. The report ends with one line you can paste as is: your headline share and the command, with no prompts, project names or dollars.
  • Sharing a screenshot? From a terminal, uvx tokenops doctor --redact hides your prompts and project names ("project A", "project B").

Saver mode

/tokenops:saver on lets TokenOps act on what the doctor finds. Off by default; every hook fails open.

  • Resume guard. After a break longer than the cache lifetime, your next message re-reads the whole conversation at the cache-write price. The guard holds that message once:

    TokenOps: this session's cache expired (1.5 h since the last reply). Sending now re-reads
    433k tokens at the cache-write price: about $3.47 API-equivalent, against $0.09 while the
    cache was warm.
      - Send the same message again to continue anyway.
      - Or run /compact first: it keeps the gist in far fewer tokens.
      - Or /clear and paste a 3-line summary of where you were (cheapest).
    

    It only acts when the context is at least 50k tokens (TOKENOPS_GUARD_MIN_TOKENS).

  • Smart RTK. With RTK installed, shell commands Claude runs are rewritten to their compact RTK form (git status -> rtk git status), using RTK's own rtk rewrite. File reads (cat, head, sed -n), git diff/git show, and the next test run after a failing one stay verbatim, because Claude needs that output exactly. If RTK's own hook is installed, smart RTK stays off so nothing is rewritten twice: remove RTK's hook from your Claude Code settings to let TokenOps decide.

  • Measured, not guessed. Smart RTK and caveman are each switched on in a random half of your sessions. After 10 sessions on and 10 off, the doctor shows what each one did to your usage per prompt, and the saver mode keeps it only if it saves:

    Measured on your sessions (saver mode A/B, usage per prompt)
      Smart RTK  -6% usage per prompt (95% range -9% to -2%) -> kept on
      caveman    +1% usage per prompt (95% range -3% to +5%) -> turned off
      Usage only: hooks cannot see whether the work went as well.
    

    For caveman, set its own default to off ("defaultMode": "off" in caveman's config file, or CAVEMAN_DEFAULT_MODE=off) so TokenOps decides per session.

/tokenops:saver status shows what is on; /tokenops:saver off turns it all off.

How it reads your usage

Claude Code writes every model response to ~/.claude/projects/*/*.jsonl, often on several lines with the same message id. The doctor keeps the last line per response, includes subagent transcripts, counts a response once even when a forked session copies it, and prices each response at Anthropic's list rates: input, output, cache reads, and cache writes (1.25x input for the 5-minute cache, 2x for the 1-hour cache). Prices come from TokenOps' price book (src/tokenops/framework/pricing.py).

  • First message after a break: a cache write of 20,000+ tokens, at least half the conversation, more than the cache lifetime after the previous call. The cache had expired, so the whole conversation was written again.
  • Re-reading context: cache reads. Every call reads the conversation again.
  • Caveman and RTK advice is a ceiling from your own usage mix (what share is output, what share of tool output came from shell commands RTK compacts), not a measured A/B.

On a Claude plan the dollar figures are API-equivalent: they show your share of usage, not a bill. Nothing leaves your machine.

What it runs, reads and stores

  • Runs: python3 (or python) with the scripts in this folder, standard library only. With Saver mode on and RTK installed, it also runs rtk rewrite <command> to ask RTK how it would compact a shell command. Nothing else.
  • Reads: your Claude Code session logs (~/.claude/projects/*/*.jsonl), your Claude Code settings files (to see which token savers are installed), caveman's installed skill file and config (only when caveman is installed), and this plugin's own prices.json.
  • Stores: a small settings file and one small JSON file per session in Claude Code's plugin data folder ($CLAUDE_PLUGIN_DATA): whether Saver mode is on, the A/B choices and results.
  • Sends: nothing. It makes no network requests and keeps no copy of your prompts or code.
  • Changes: with Saver mode on, it can hold one message after a break (you send it again to go on) and rewrite a shell command to its RTK form. With Saver mode off, it only reads.

Development

python scripts/sync-claude-prices.py   # after changing DEFAULT_RATES
python -m pytest tests/claude_code

Metadata

Release files for tokenops 0.2.2

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

Source distribution (sdist)

Source distribution for tokenops 0.2.2
File Size Uploaded Provenance Actions
tokenops-0.2.2.tar.gz 24.1 kB Without provenance Details for tokenops-0.2.2.tar.gz

Built distribution (wheel)

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

Total release size: 53.8 kB

Release files / tokenops-0.2.2.tar.gz

Download URL tokenops-0.2.2.tar.gz
Size 24.1 kB
Tags Source
SHA-256 checksum
How to use checksums
48b53dfde05025bce886d3b5fb375d4f750c2fcf96efe15e1a48a5ccaebe5c1e
BLAKE2b-256 checksum
How to use checksums
36156b60f6061a09fa971556ae27029e11a3b8c56a4e357f89825de58cd40cfc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.7 {"installer":{"name":"uv","version":"0.12.7","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 / tokenops-0.2.2-py3-none-any.whl

Download URL tokenops-0.2.2-py3-none-any.whl
Size 29.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
50586a7247df12a80ac63b8cc0c994d2077ba63e8d1527288c91739fc9039a0f
BLAKE2b-256 checksum
How to use checksums
61e5d3cc16e908c0fe1a095b8f6344998f19c7ceba075b8f4374323f474ebcab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.7 {"installer":{"name":"uv","version":"0.12.7","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 tokenops 0.2.2

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.2.2 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