tokenops 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- tokenops-0.2.0/PKG-INFO +165 -0
- tokenops-0.2.0/README.md +150 -0
- tokenops-0.2.0/pyproject.toml +36 -0
- tokenops-0.2.0/scripts/tokenops.egg-info/PKG-INFO +165 -0
- tokenops-0.2.0/scripts/tokenops.egg-info/SOURCES.txt +21 -0
- tokenops-0.2.0/scripts/tokenops.egg-info/dependency_links.txt +1 -0
- tokenops-0.2.0/scripts/tokenops.egg-info/entry_points.txt +2 -0
- tokenops-0.2.0/scripts/tokenops.egg-info/top_level.txt +2 -0
- tokenops-0.2.0/scripts/tokenops_cc/__init__.py +1 -0
- tokenops-0.2.0/scripts/tokenops_cc/ab.py +147 -0
- tokenops-0.2.0/scripts/tokenops_cc/caveman.py +66 -0
- tokenops-0.2.0/scripts/tokenops_cc/cost.py +45 -0
- tokenops-0.2.0/scripts/tokenops_cc/doctor.py +292 -0
- tokenops-0.2.0/scripts/tokenops_cc/guard.py +91 -0
- tokenops-0.2.0/scripts/tokenops_cc/prices.json +97 -0
- tokenops-0.2.0/scripts/tokenops_cc/prices.py +51 -0
- tokenops-0.2.0/scripts/tokenops_cc/prompts.py +362 -0
- tokenops-0.2.0/scripts/tokenops_cc/rtk.py +96 -0
- tokenops-0.2.0/scripts/tokenops_cc/savers.py +53 -0
- tokenops-0.2.0/scripts/tokenops_cc/settings.py +59 -0
- tokenops-0.2.0/scripts/tokenops_cc/transcript.py +99 -0
- tokenops-0.2.0/scripts/tokenops_cli.py +71 -0
- tokenops-0.2.0/setup.cfg +4 -0
tokenops-0.2.0/PKG-INFO
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: tokenops
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Which prompts ate your Claude Code limit, and why? Read from your own session logs.
|
|
5
|
+
Author: Susheem Koul, Tisha Chawla
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/theagentplane/tokenops/tree/main/integrations/claude-code
|
|
8
|
+
Project-URL: Repository, https://github.com/theagentplane/tokenops
|
|
9
|
+
Keywords: claude,claude-code,tokens,usage,rate-limit,prompt-cache
|
|
10
|
+
Classifier: Environment :: Console
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Requires-Python: >=3.9
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
|
|
16
|
+
# TokenOps for Claude Code
|
|
17
|
+
|
|
18
|
+
**Why am I hitting my limit?** One command names the prompts that cost you the most, says why,
|
|
19
|
+
and what to change. Read from your own session logs, on your machine.
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
/tokenops:doctor
|
|
23
|
+
|
|
24
|
+
Why am I hitting my limit? (last 7 days, 12 sessions, 304 prompts, $235 at API list prices)
|
|
25
|
+
|
|
26
|
+
37% of your Claude usage went to coming back after a break.
|
|
27
|
+
39 times the cache had expired, so Claude re-paid for the whole conversation.
|
|
28
|
+
/compact before you step away would have saved ~31%.
|
|
29
|
+
|
|
30
|
+
Your 3 most expensive prompts (9% of usage)
|
|
31
|
+
|
|
32
|
+
#2 Wed 21:56 project B (prompt hidden)
|
|
33
|
+
█████████░░░░░░░░░░░ 44% of that 5-hour window | 24x your typical prompt
|
|
34
|
+
CACHE EXPIRED away 4h 45m, past the 1h cache: one call re-wrote your
|
|
35
|
+
whole 965k-token conversation to the cache at 2x the input price
|
|
36
|
+
1 model call, paid $7.41 -> /compact before leaving ~$0.52 (14x less) | /clear + note ~$0.27
|
|
37
|
+
...
|
|
38
|
+
Where your usage goes
|
|
39
|
+
41% Re-reading context every call
|
|
40
|
+
37% Coming back after breaks (cache re-written)
|
|
41
|
+
12% Claude's output
|
|
42
|
+
11% Caching new context
|
|
43
|
+
|
|
44
|
+
What to change
|
|
45
|
+
1. Re-reading is 41%: every call reads the whole conversation again ... Use /clear when you
|
|
46
|
+
switch to an unrelated task.
|
|
47
|
+
2. Output is 12% of your usage, so a terse-output tool like caveman can save at most that ...
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Install
|
|
51
|
+
|
|
52
|
+
In Claude Code:
|
|
53
|
+
|
|
54
|
+
```
|
|
55
|
+
/plugin marketplace add theagentplane/tokenops
|
|
56
|
+
/plugin install tokenops@tokenops
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Then run `/tokenops:doctor` (last 7 days; `/tokenops:doctor 30` for 30). Needs Python 3.9+ on
|
|
60
|
+
your PATH; nothing to `pip install`.
|
|
61
|
+
|
|
62
|
+
Or, without installing anything, from any terminal with [uv](https://docs.astral.sh/uv/):
|
|
63
|
+
|
|
64
|
+
```
|
|
65
|
+
uvx tokenops doctor
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
(The `tokenops` package on PyPI is this CLI; the agent governance library is `agent-tokenops`.)
|
|
69
|
+
|
|
70
|
+
## Reading the report
|
|
71
|
+
|
|
72
|
+
- **Prompts.** One prompt you type can make many model calls (tool use, subagents). They are all
|
|
73
|
+
counted under the prompt that started them. The doctor shows the 3 most expensive prompts,
|
|
74
|
+
with the project they were typed in.
|
|
75
|
+
- **Tokens** are the size of the whole conversation that one call sent, not a sum over calls.
|
|
76
|
+
Models with a 1M-token context can carry a conversation that long.
|
|
77
|
+
- **CACHE EXPIRED** means the call came more than the cache lifetime (1 hour on a Claude plan,
|
|
78
|
+
5 minutes with an API key) after the previous one, and wrote most of the conversation to the
|
|
79
|
+
cache again at the cache-write price. The headline and the "Coming back after breaks" row are
|
|
80
|
+
the same number: the cache writes of those calls.
|
|
81
|
+
- **5-hour windows** are estimated from your first prompt in each, not read from Anthropic's
|
|
82
|
+
meter. The cheaper costs are estimates: only the calls a break made expensive are re-priced.
|
|
83
|
+
- **Sharing a screenshot?** From a terminal, `uvx tokenops doctor --redact` hides your prompts and
|
|
84
|
+
project names ("project A", "project B").
|
|
85
|
+
|
|
86
|
+
## Saver mode
|
|
87
|
+
|
|
88
|
+
`/tokenops:saver on` lets TokenOps act on what the doctor finds. Off by default; every hook
|
|
89
|
+
fails open.
|
|
90
|
+
|
|
91
|
+
- **Resume guard.** After a break longer than the cache lifetime, your next message re-reads the
|
|
92
|
+
whole conversation at the cache-write price. The guard holds that message once:
|
|
93
|
+
|
|
94
|
+
```
|
|
95
|
+
TokenOps: this session's cache expired (1.5 h since the last reply). Sending now re-reads
|
|
96
|
+
433k tokens at the cache-write price: about $3.47 API-equivalent, against $0.09 while the
|
|
97
|
+
cache was warm.
|
|
98
|
+
- Send the same message again to continue anyway.
|
|
99
|
+
- Or run /compact first: it keeps the gist in far fewer tokens.
|
|
100
|
+
- Or /clear and paste a 3-line summary of where you were (cheapest).
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
It only acts when the context is at least 50k tokens (`TOKENOPS_GUARD_MIN_TOKENS`).
|
|
104
|
+
|
|
105
|
+
- **Smart RTK.** With [RTK](https://github.com/rtk-ai/rtk) installed, shell commands Claude runs
|
|
106
|
+
are rewritten to their compact RTK form (`git status` -> `rtk git status`), using RTK's own
|
|
107
|
+
`rtk rewrite`. File reads (`cat`, `head`, `sed -n`), `git diff`/`git show`, and the next test run
|
|
108
|
+
after a failing one stay verbatim, because Claude needs that output exactly. If RTK's own hook
|
|
109
|
+
is installed, smart RTK stays off so nothing is rewritten twice: remove RTK's hook from your
|
|
110
|
+
Claude Code settings to let TokenOps decide.
|
|
111
|
+
|
|
112
|
+
- **Measured, not guessed.** Smart RTK and [caveman](https://github.com/JuliusBrussee/caveman) are
|
|
113
|
+
each switched on in a random half of your sessions. After 10 sessions on and 10 off, the doctor
|
|
114
|
+
shows what each one did to your usage per prompt, and the saver mode keeps it only if it saves:
|
|
115
|
+
|
|
116
|
+
```
|
|
117
|
+
Measured on your sessions (saver mode A/B, usage per prompt)
|
|
118
|
+
Smart RTK -6% usage per prompt (95% range -9% to -2%) -> kept on
|
|
119
|
+
caveman +1% usage per prompt (95% range -3% to +5%) -> turned off
|
|
120
|
+
Usage only: hooks cannot see whether the work went as well.
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
For caveman, set its own default to off (`"defaultMode": "off"` in caveman's config file, or
|
|
124
|
+
`CAVEMAN_DEFAULT_MODE=off`) so TokenOps decides per session.
|
|
125
|
+
|
|
126
|
+
`/tokenops:saver status` shows what is on; `/tokenops:saver off` turns it all off.
|
|
127
|
+
|
|
128
|
+
## How it reads your usage
|
|
129
|
+
|
|
130
|
+
Claude Code writes every model response to `~/.claude/projects/*/*.jsonl`, often on several
|
|
131
|
+
lines with the same message id. The doctor keeps the last line per response, includes subagent
|
|
132
|
+
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,
|
|
133
|
+
and cache writes (1.25x input for the 5-minute cache, 2x for the 1-hour cache). Prices come from
|
|
134
|
+
TokenOps' price book (`src/tokenops/framework/pricing.py`).
|
|
135
|
+
|
|
136
|
+
- **Coming back after breaks:** a cache write of 20,000+ tokens, at least half the conversation,
|
|
137
|
+
more than the cache lifetime after the previous call. The cache had expired, so the whole
|
|
138
|
+
conversation was written again.
|
|
139
|
+
- **Re-reading context:** cache reads. Every call reads the conversation again.
|
|
140
|
+
- **Caveman and RTK advice** is a ceiling from your own usage mix (what share is output, what
|
|
141
|
+
share of tool output came from shell commands RTK compacts), not a measured A/B.
|
|
142
|
+
|
|
143
|
+
On a Claude plan the dollar figures are API-equivalent: they show your share of usage, not a
|
|
144
|
+
bill. Nothing leaves your machine.
|
|
145
|
+
|
|
146
|
+
## What it runs, reads and stores
|
|
147
|
+
|
|
148
|
+
- **Runs:** `python3` (or `python`) with the scripts in this folder, standard library only. With
|
|
149
|
+
Saver mode on and [RTK](https://github.com/rtk-ai/rtk) installed, it also runs `rtk rewrite
|
|
150
|
+
<command>` to ask RTK how it would compact a shell command. Nothing else.
|
|
151
|
+
- **Reads:** your Claude Code session logs (`~/.claude/projects/*/*.jsonl`), your Claude Code
|
|
152
|
+
settings files (to see which token savers are installed), caveman's installed skill file and
|
|
153
|
+
config (only when caveman is installed), and this plugin's own `prices.json`.
|
|
154
|
+
- **Stores:** a small settings file and one small JSON file per session in Claude Code's plugin
|
|
155
|
+
data folder (`$CLAUDE_PLUGIN_DATA`): whether Saver mode is on, the A/B choices and results.
|
|
156
|
+
- **Sends:** nothing. It makes no network requests and keeps no copy of your prompts or code.
|
|
157
|
+
- **Changes:** with Saver mode on, it can hold one message after a break (you send it again to
|
|
158
|
+
go on) and rewrite a shell command to its RTK form. With Saver mode off, it only reads.
|
|
159
|
+
|
|
160
|
+
## Development
|
|
161
|
+
|
|
162
|
+
```
|
|
163
|
+
python scripts/sync-claude-prices.py # after changing DEFAULT_RATES
|
|
164
|
+
python -m pytest tests/claude_code
|
|
165
|
+
```
|
tokenops-0.2.0/README.md
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
# TokenOps for Claude Code
|
|
2
|
+
|
|
3
|
+
**Why am I hitting my limit?** One command names the prompts that cost you the most, says why,
|
|
4
|
+
and what to change. Read from your own session logs, on your machine.
|
|
5
|
+
|
|
6
|
+
```
|
|
7
|
+
/tokenops:doctor
|
|
8
|
+
|
|
9
|
+
Why am I hitting my limit? (last 7 days, 12 sessions, 304 prompts, $235 at API list prices)
|
|
10
|
+
|
|
11
|
+
37% of your Claude usage went to coming back after a break.
|
|
12
|
+
39 times the cache had expired, so Claude re-paid for the whole conversation.
|
|
13
|
+
/compact before you step away would have saved ~31%.
|
|
14
|
+
|
|
15
|
+
Your 3 most expensive prompts (9% of usage)
|
|
16
|
+
|
|
17
|
+
#2 Wed 21:56 project B (prompt hidden)
|
|
18
|
+
█████████░░░░░░░░░░░ 44% of that 5-hour window | 24x your typical prompt
|
|
19
|
+
CACHE EXPIRED away 4h 45m, past the 1h cache: one call re-wrote your
|
|
20
|
+
whole 965k-token conversation to the cache at 2x the input price
|
|
21
|
+
1 model call, paid $7.41 -> /compact before leaving ~$0.52 (14x less) | /clear + note ~$0.27
|
|
22
|
+
...
|
|
23
|
+
Where your usage goes
|
|
24
|
+
41% Re-reading context every call
|
|
25
|
+
37% Coming back after breaks (cache re-written)
|
|
26
|
+
12% Claude's output
|
|
27
|
+
11% Caching new context
|
|
28
|
+
|
|
29
|
+
What to change
|
|
30
|
+
1. Re-reading is 41%: every call reads the whole conversation again ... Use /clear when you
|
|
31
|
+
switch to an unrelated task.
|
|
32
|
+
2. Output is 12% of your usage, so a terse-output tool like caveman can save at most that ...
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Install
|
|
36
|
+
|
|
37
|
+
In Claude Code:
|
|
38
|
+
|
|
39
|
+
```
|
|
40
|
+
/plugin marketplace add theagentplane/tokenops
|
|
41
|
+
/plugin install tokenops@tokenops
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Then run `/tokenops:doctor` (last 7 days; `/tokenops:doctor 30` for 30). Needs Python 3.9+ on
|
|
45
|
+
your PATH; nothing to `pip install`.
|
|
46
|
+
|
|
47
|
+
Or, without installing anything, from any terminal with [uv](https://docs.astral.sh/uv/):
|
|
48
|
+
|
|
49
|
+
```
|
|
50
|
+
uvx tokenops doctor
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
(The `tokenops` package on PyPI is this CLI; the agent governance library is `agent-tokenops`.)
|
|
54
|
+
|
|
55
|
+
## Reading the report
|
|
56
|
+
|
|
57
|
+
- **Prompts.** One prompt you type can make many model calls (tool use, subagents). They are all
|
|
58
|
+
counted under the prompt that started them. The doctor shows the 3 most expensive prompts,
|
|
59
|
+
with the project they were typed in.
|
|
60
|
+
- **Tokens** are the size of the whole conversation that one call sent, not a sum over calls.
|
|
61
|
+
Models with a 1M-token context can carry a conversation that long.
|
|
62
|
+
- **CACHE EXPIRED** means the call came more than the cache lifetime (1 hour on a Claude plan,
|
|
63
|
+
5 minutes with an API key) after the previous one, and wrote most of the conversation to the
|
|
64
|
+
cache again at the cache-write price. The headline and the "Coming back after breaks" row are
|
|
65
|
+
the same number: the cache writes of those calls.
|
|
66
|
+
- **5-hour windows** are estimated from your first prompt in each, not read from Anthropic's
|
|
67
|
+
meter. The cheaper costs are estimates: only the calls a break made expensive are re-priced.
|
|
68
|
+
- **Sharing a screenshot?** From a terminal, `uvx tokenops doctor --redact` hides your prompts and
|
|
69
|
+
project names ("project A", "project B").
|
|
70
|
+
|
|
71
|
+
## Saver mode
|
|
72
|
+
|
|
73
|
+
`/tokenops:saver on` lets TokenOps act on what the doctor finds. Off by default; every hook
|
|
74
|
+
fails open.
|
|
75
|
+
|
|
76
|
+
- **Resume guard.** After a break longer than the cache lifetime, your next message re-reads the
|
|
77
|
+
whole conversation at the cache-write price. The guard holds that message once:
|
|
78
|
+
|
|
79
|
+
```
|
|
80
|
+
TokenOps: this session's cache expired (1.5 h since the last reply). Sending now re-reads
|
|
81
|
+
433k tokens at the cache-write price: about $3.47 API-equivalent, against $0.09 while the
|
|
82
|
+
cache was warm.
|
|
83
|
+
- Send the same message again to continue anyway.
|
|
84
|
+
- Or run /compact first: it keeps the gist in far fewer tokens.
|
|
85
|
+
- Or /clear and paste a 3-line summary of where you were (cheapest).
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
It only acts when the context is at least 50k tokens (`TOKENOPS_GUARD_MIN_TOKENS`).
|
|
89
|
+
|
|
90
|
+
- **Smart RTK.** With [RTK](https://github.com/rtk-ai/rtk) installed, shell commands Claude runs
|
|
91
|
+
are rewritten to their compact RTK form (`git status` -> `rtk git status`), using RTK's own
|
|
92
|
+
`rtk rewrite`. File reads (`cat`, `head`, `sed -n`), `git diff`/`git show`, and the next test run
|
|
93
|
+
after a failing one stay verbatim, because Claude needs that output exactly. If RTK's own hook
|
|
94
|
+
is installed, smart RTK stays off so nothing is rewritten twice: remove RTK's hook from your
|
|
95
|
+
Claude Code settings to let TokenOps decide.
|
|
96
|
+
|
|
97
|
+
- **Measured, not guessed.** Smart RTK and [caveman](https://github.com/JuliusBrussee/caveman) are
|
|
98
|
+
each switched on in a random half of your sessions. After 10 sessions on and 10 off, the doctor
|
|
99
|
+
shows what each one did to your usage per prompt, and the saver mode keeps it only if it saves:
|
|
100
|
+
|
|
101
|
+
```
|
|
102
|
+
Measured on your sessions (saver mode A/B, usage per prompt)
|
|
103
|
+
Smart RTK -6% usage per prompt (95% range -9% to -2%) -> kept on
|
|
104
|
+
caveman +1% usage per prompt (95% range -3% to +5%) -> turned off
|
|
105
|
+
Usage only: hooks cannot see whether the work went as well.
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
For caveman, set its own default to off (`"defaultMode": "off"` in caveman's config file, or
|
|
109
|
+
`CAVEMAN_DEFAULT_MODE=off`) so TokenOps decides per session.
|
|
110
|
+
|
|
111
|
+
`/tokenops:saver status` shows what is on; `/tokenops:saver off` turns it all off.
|
|
112
|
+
|
|
113
|
+
## How it reads your usage
|
|
114
|
+
|
|
115
|
+
Claude Code writes every model response to `~/.claude/projects/*/*.jsonl`, often on several
|
|
116
|
+
lines with the same message id. The doctor keeps the last line per response, includes subagent
|
|
117
|
+
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,
|
|
118
|
+
and cache writes (1.25x input for the 5-minute cache, 2x for the 1-hour cache). Prices come from
|
|
119
|
+
TokenOps' price book (`src/tokenops/framework/pricing.py`).
|
|
120
|
+
|
|
121
|
+
- **Coming back after breaks:** a cache write of 20,000+ tokens, at least half the conversation,
|
|
122
|
+
more than the cache lifetime after the previous call. The cache had expired, so the whole
|
|
123
|
+
conversation was written again.
|
|
124
|
+
- **Re-reading context:** cache reads. Every call reads the conversation again.
|
|
125
|
+
- **Caveman and RTK advice** is a ceiling from your own usage mix (what share is output, what
|
|
126
|
+
share of tool output came from shell commands RTK compacts), not a measured A/B.
|
|
127
|
+
|
|
128
|
+
On a Claude plan the dollar figures are API-equivalent: they show your share of usage, not a
|
|
129
|
+
bill. Nothing leaves your machine.
|
|
130
|
+
|
|
131
|
+
## What it runs, reads and stores
|
|
132
|
+
|
|
133
|
+
- **Runs:** `python3` (or `python`) with the scripts in this folder, standard library only. With
|
|
134
|
+
Saver mode on and [RTK](https://github.com/rtk-ai/rtk) installed, it also runs `rtk rewrite
|
|
135
|
+
<command>` to ask RTK how it would compact a shell command. Nothing else.
|
|
136
|
+
- **Reads:** your Claude Code session logs (`~/.claude/projects/*/*.jsonl`), your Claude Code
|
|
137
|
+
settings files (to see which token savers are installed), caveman's installed skill file and
|
|
138
|
+
config (only when caveman is installed), and this plugin's own `prices.json`.
|
|
139
|
+
- **Stores:** a small settings file and one small JSON file per session in Claude Code's plugin
|
|
140
|
+
data folder (`$CLAUDE_PLUGIN_DATA`): whether Saver mode is on, the A/B choices and results.
|
|
141
|
+
- **Sends:** nothing. It makes no network requests and keeps no copy of your prompts or code.
|
|
142
|
+
- **Changes:** with Saver mode on, it can hold one message after a break (you send it again to
|
|
143
|
+
go on) and rewrite a shell command to its RTK form. With Saver mode off, it only reads.
|
|
144
|
+
|
|
145
|
+
## Development
|
|
146
|
+
|
|
147
|
+
```
|
|
148
|
+
python scripts/sync-claude-prices.py # after changing DEFAULT_RATES
|
|
149
|
+
python -m pytest tests/claude_code
|
|
150
|
+
```
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# The Claude Code plugin's CLI as a small PyPI package, so `uvx tokenops why` runs without
|
|
2
|
+
# installing the plugin (ADR 0012). Standard library only; the plugin itself does not use this file.
|
|
3
|
+
[build-system]
|
|
4
|
+
requires = ["setuptools>=77"]
|
|
5
|
+
build-backend = "setuptools.build_meta"
|
|
6
|
+
|
|
7
|
+
[project]
|
|
8
|
+
name = "tokenops"
|
|
9
|
+
version = "0.2.0"
|
|
10
|
+
description = "Which prompts ate your Claude Code limit, and why? Read from your own session logs."
|
|
11
|
+
readme = "README.md"
|
|
12
|
+
requires-python = ">=3.9"
|
|
13
|
+
license = "MIT"
|
|
14
|
+
authors = [{ name = "Susheem Koul" }, { name = "Tisha Chawla" }]
|
|
15
|
+
keywords = ["claude", "claude-code", "tokens", "usage", "rate-limit", "prompt-cache"]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Environment :: Console",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
]
|
|
21
|
+
dependencies = []
|
|
22
|
+
|
|
23
|
+
[project.urls]
|
|
24
|
+
Homepage = "https://github.com/theagentplane/tokenops/tree/main/integrations/claude-code"
|
|
25
|
+
Repository = "https://github.com/theagentplane/tokenops"
|
|
26
|
+
|
|
27
|
+
[project.scripts]
|
|
28
|
+
tokenops = "tokenops_cli:main"
|
|
29
|
+
|
|
30
|
+
[tool.setuptools]
|
|
31
|
+
package-dir = { "" = "scripts" }
|
|
32
|
+
packages = ["tokenops_cc"]
|
|
33
|
+
py-modules = ["tokenops_cli"]
|
|
34
|
+
|
|
35
|
+
[tool.setuptools.package-data]
|
|
36
|
+
tokenops_cc = ["prices.json"]
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: tokenops
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Which prompts ate your Claude Code limit, and why? Read from your own session logs.
|
|
5
|
+
Author: Susheem Koul, Tisha Chawla
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/theagentplane/tokenops/tree/main/integrations/claude-code
|
|
8
|
+
Project-URL: Repository, https://github.com/theagentplane/tokenops
|
|
9
|
+
Keywords: claude,claude-code,tokens,usage,rate-limit,prompt-cache
|
|
10
|
+
Classifier: Environment :: Console
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Requires-Python: >=3.9
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
|
|
16
|
+
# TokenOps for Claude Code
|
|
17
|
+
|
|
18
|
+
**Why am I hitting my limit?** One command names the prompts that cost you the most, says why,
|
|
19
|
+
and what to change. Read from your own session logs, on your machine.
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
/tokenops:doctor
|
|
23
|
+
|
|
24
|
+
Why am I hitting my limit? (last 7 days, 12 sessions, 304 prompts, $235 at API list prices)
|
|
25
|
+
|
|
26
|
+
37% of your Claude usage went to coming back after a break.
|
|
27
|
+
39 times the cache had expired, so Claude re-paid for the whole conversation.
|
|
28
|
+
/compact before you step away would have saved ~31%.
|
|
29
|
+
|
|
30
|
+
Your 3 most expensive prompts (9% of usage)
|
|
31
|
+
|
|
32
|
+
#2 Wed 21:56 project B (prompt hidden)
|
|
33
|
+
█████████░░░░░░░░░░░ 44% of that 5-hour window | 24x your typical prompt
|
|
34
|
+
CACHE EXPIRED away 4h 45m, past the 1h cache: one call re-wrote your
|
|
35
|
+
whole 965k-token conversation to the cache at 2x the input price
|
|
36
|
+
1 model call, paid $7.41 -> /compact before leaving ~$0.52 (14x less) | /clear + note ~$0.27
|
|
37
|
+
...
|
|
38
|
+
Where your usage goes
|
|
39
|
+
41% Re-reading context every call
|
|
40
|
+
37% Coming back after breaks (cache re-written)
|
|
41
|
+
12% Claude's output
|
|
42
|
+
11% Caching new context
|
|
43
|
+
|
|
44
|
+
What to change
|
|
45
|
+
1. Re-reading is 41%: every call reads the whole conversation again ... Use /clear when you
|
|
46
|
+
switch to an unrelated task.
|
|
47
|
+
2. Output is 12% of your usage, so a terse-output tool like caveman can save at most that ...
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Install
|
|
51
|
+
|
|
52
|
+
In Claude Code:
|
|
53
|
+
|
|
54
|
+
```
|
|
55
|
+
/plugin marketplace add theagentplane/tokenops
|
|
56
|
+
/plugin install tokenops@tokenops
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Then run `/tokenops:doctor` (last 7 days; `/tokenops:doctor 30` for 30). Needs Python 3.9+ on
|
|
60
|
+
your PATH; nothing to `pip install`.
|
|
61
|
+
|
|
62
|
+
Or, without installing anything, from any terminal with [uv](https://docs.astral.sh/uv/):
|
|
63
|
+
|
|
64
|
+
```
|
|
65
|
+
uvx tokenops doctor
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
(The `tokenops` package on PyPI is this CLI; the agent governance library is `agent-tokenops`.)
|
|
69
|
+
|
|
70
|
+
## Reading the report
|
|
71
|
+
|
|
72
|
+
- **Prompts.** One prompt you type can make many model calls (tool use, subagents). They are all
|
|
73
|
+
counted under the prompt that started them. The doctor shows the 3 most expensive prompts,
|
|
74
|
+
with the project they were typed in.
|
|
75
|
+
- **Tokens** are the size of the whole conversation that one call sent, not a sum over calls.
|
|
76
|
+
Models with a 1M-token context can carry a conversation that long.
|
|
77
|
+
- **CACHE EXPIRED** means the call came more than the cache lifetime (1 hour on a Claude plan,
|
|
78
|
+
5 minutes with an API key) after the previous one, and wrote most of the conversation to the
|
|
79
|
+
cache again at the cache-write price. The headline and the "Coming back after breaks" row are
|
|
80
|
+
the same number: the cache writes of those calls.
|
|
81
|
+
- **5-hour windows** are estimated from your first prompt in each, not read from Anthropic's
|
|
82
|
+
meter. The cheaper costs are estimates: only the calls a break made expensive are re-priced.
|
|
83
|
+
- **Sharing a screenshot?** From a terminal, `uvx tokenops doctor --redact` hides your prompts and
|
|
84
|
+
project names ("project A", "project B").
|
|
85
|
+
|
|
86
|
+
## Saver mode
|
|
87
|
+
|
|
88
|
+
`/tokenops:saver on` lets TokenOps act on what the doctor finds. Off by default; every hook
|
|
89
|
+
fails open.
|
|
90
|
+
|
|
91
|
+
- **Resume guard.** After a break longer than the cache lifetime, your next message re-reads the
|
|
92
|
+
whole conversation at the cache-write price. The guard holds that message once:
|
|
93
|
+
|
|
94
|
+
```
|
|
95
|
+
TokenOps: this session's cache expired (1.5 h since the last reply). Sending now re-reads
|
|
96
|
+
433k tokens at the cache-write price: about $3.47 API-equivalent, against $0.09 while the
|
|
97
|
+
cache was warm.
|
|
98
|
+
- Send the same message again to continue anyway.
|
|
99
|
+
- Or run /compact first: it keeps the gist in far fewer tokens.
|
|
100
|
+
- Or /clear and paste a 3-line summary of where you were (cheapest).
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
It only acts when the context is at least 50k tokens (`TOKENOPS_GUARD_MIN_TOKENS`).
|
|
104
|
+
|
|
105
|
+
- **Smart RTK.** With [RTK](https://github.com/rtk-ai/rtk) installed, shell commands Claude runs
|
|
106
|
+
are rewritten to their compact RTK form (`git status` -> `rtk git status`), using RTK's own
|
|
107
|
+
`rtk rewrite`. File reads (`cat`, `head`, `sed -n`), `git diff`/`git show`, and the next test run
|
|
108
|
+
after a failing one stay verbatim, because Claude needs that output exactly. If RTK's own hook
|
|
109
|
+
is installed, smart RTK stays off so nothing is rewritten twice: remove RTK's hook from your
|
|
110
|
+
Claude Code settings to let TokenOps decide.
|
|
111
|
+
|
|
112
|
+
- **Measured, not guessed.** Smart RTK and [caveman](https://github.com/JuliusBrussee/caveman) are
|
|
113
|
+
each switched on in a random half of your sessions. After 10 sessions on and 10 off, the doctor
|
|
114
|
+
shows what each one did to your usage per prompt, and the saver mode keeps it only if it saves:
|
|
115
|
+
|
|
116
|
+
```
|
|
117
|
+
Measured on your sessions (saver mode A/B, usage per prompt)
|
|
118
|
+
Smart RTK -6% usage per prompt (95% range -9% to -2%) -> kept on
|
|
119
|
+
caveman +1% usage per prompt (95% range -3% to +5%) -> turned off
|
|
120
|
+
Usage only: hooks cannot see whether the work went as well.
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
For caveman, set its own default to off (`"defaultMode": "off"` in caveman's config file, or
|
|
124
|
+
`CAVEMAN_DEFAULT_MODE=off`) so TokenOps decides per session.
|
|
125
|
+
|
|
126
|
+
`/tokenops:saver status` shows what is on; `/tokenops:saver off` turns it all off.
|
|
127
|
+
|
|
128
|
+
## How it reads your usage
|
|
129
|
+
|
|
130
|
+
Claude Code writes every model response to `~/.claude/projects/*/*.jsonl`, often on several
|
|
131
|
+
lines with the same message id. The doctor keeps the last line per response, includes subagent
|
|
132
|
+
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,
|
|
133
|
+
and cache writes (1.25x input for the 5-minute cache, 2x for the 1-hour cache). Prices come from
|
|
134
|
+
TokenOps' price book (`src/tokenops/framework/pricing.py`).
|
|
135
|
+
|
|
136
|
+
- **Coming back after breaks:** a cache write of 20,000+ tokens, at least half the conversation,
|
|
137
|
+
more than the cache lifetime after the previous call. The cache had expired, so the whole
|
|
138
|
+
conversation was written again.
|
|
139
|
+
- **Re-reading context:** cache reads. Every call reads the conversation again.
|
|
140
|
+
- **Caveman and RTK advice** is a ceiling from your own usage mix (what share is output, what
|
|
141
|
+
share of tool output came from shell commands RTK compacts), not a measured A/B.
|
|
142
|
+
|
|
143
|
+
On a Claude plan the dollar figures are API-equivalent: they show your share of usage, not a
|
|
144
|
+
bill. Nothing leaves your machine.
|
|
145
|
+
|
|
146
|
+
## What it runs, reads and stores
|
|
147
|
+
|
|
148
|
+
- **Runs:** `python3` (or `python`) with the scripts in this folder, standard library only. With
|
|
149
|
+
Saver mode on and [RTK](https://github.com/rtk-ai/rtk) installed, it also runs `rtk rewrite
|
|
150
|
+
<command>` to ask RTK how it would compact a shell command. Nothing else.
|
|
151
|
+
- **Reads:** your Claude Code session logs (`~/.claude/projects/*/*.jsonl`), your Claude Code
|
|
152
|
+
settings files (to see which token savers are installed), caveman's installed skill file and
|
|
153
|
+
config (only when caveman is installed), and this plugin's own `prices.json`.
|
|
154
|
+
- **Stores:** a small settings file and one small JSON file per session in Claude Code's plugin
|
|
155
|
+
data folder (`$CLAUDE_PLUGIN_DATA`): whether Saver mode is on, the A/B choices and results.
|
|
156
|
+
- **Sends:** nothing. It makes no network requests and keeps no copy of your prompts or code.
|
|
157
|
+
- **Changes:** with Saver mode on, it can hold one message after a break (you send it again to
|
|
158
|
+
go on) and rewrite a shell command to its RTK form. With Saver mode off, it only reads.
|
|
159
|
+
|
|
160
|
+
## Development
|
|
161
|
+
|
|
162
|
+
```
|
|
163
|
+
python scripts/sync-claude-prices.py # after changing DEFAULT_RATES
|
|
164
|
+
python -m pytest tests/claude_code
|
|
165
|
+
```
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
README.md
|
|
2
|
+
pyproject.toml
|
|
3
|
+
scripts/tokenops_cli.py
|
|
4
|
+
scripts/tokenops.egg-info/PKG-INFO
|
|
5
|
+
scripts/tokenops.egg-info/SOURCES.txt
|
|
6
|
+
scripts/tokenops.egg-info/dependency_links.txt
|
|
7
|
+
scripts/tokenops.egg-info/entry_points.txt
|
|
8
|
+
scripts/tokenops.egg-info/top_level.txt
|
|
9
|
+
scripts/tokenops_cc/__init__.py
|
|
10
|
+
scripts/tokenops_cc/ab.py
|
|
11
|
+
scripts/tokenops_cc/caveman.py
|
|
12
|
+
scripts/tokenops_cc/cost.py
|
|
13
|
+
scripts/tokenops_cc/doctor.py
|
|
14
|
+
scripts/tokenops_cc/guard.py
|
|
15
|
+
scripts/tokenops_cc/prices.json
|
|
16
|
+
scripts/tokenops_cc/prices.py
|
|
17
|
+
scripts/tokenops_cc/prompts.py
|
|
18
|
+
scripts/tokenops_cc/rtk.py
|
|
19
|
+
scripts/tokenops_cc/savers.py
|
|
20
|
+
scripts/tokenops_cc/settings.py
|
|
21
|
+
scripts/tokenops_cc/transcript.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""TokenOps for Claude Code: hooks, cost ledger and reports (stdlib only)."""
|