tokensaver-agent 0.6.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.
- tokensaver_agent-0.6.0/LICENSE +163 -0
- tokensaver_agent-0.6.0/PKG-INFO +273 -0
- tokensaver_agent-0.6.0/README.md +251 -0
- tokensaver_agent-0.6.0/pyproject.toml +37 -0
- tokensaver_agent-0.6.0/setup.cfg +4 -0
- tokensaver_agent-0.6.0/tests/test_case_studies.py +32 -0
- tokensaver_agent-0.6.0/tests/test_demo.py +92 -0
- tokensaver_agent-0.6.0/tests/test_install.py +62 -0
- tokensaver_agent-0.6.0/tests/test_integrations.py +166 -0
- tokensaver_agent-0.6.0/tests/test_mcp_server.py +179 -0
- tokensaver_agent-0.6.0/tests/test_panel.py +83 -0
- tokensaver_agent-0.6.0/tests/test_planner.py +49 -0
- tokensaver_agent-0.6.0/tests/test_profile.py +125 -0
- tokensaver_agent-0.6.0/tests/test_runtime.py +308 -0
- tokensaver_agent-0.6.0/tests/test_update.py +37 -0
- tokensaver_agent-0.6.0/tests/test_version.py +14 -0
- tokensaver_agent-0.6.0/tokensaver/__init__.py +8 -0
- tokensaver_agent-0.6.0/tokensaver/benchmark.py +138 -0
- tokensaver_agent-0.6.0/tokensaver/brief.py +200 -0
- tokensaver_agent-0.6.0/tokensaver/cli.py +722 -0
- tokensaver_agent-0.6.0/tokensaver/context_review.py +94 -0
- tokensaver_agent-0.6.0/tokensaver/demo.py +149 -0
- tokensaver_agent-0.6.0/tokensaver/diagnosis.py +866 -0
- tokensaver_agent-0.6.0/tokensaver/eval.py +65 -0
- tokensaver_agent-0.6.0/tokensaver/install.py +435 -0
- tokensaver_agent-0.6.0/tokensaver/integrations.py +400 -0
- tokensaver_agent-0.6.0/tokensaver/mcp_server.py +395 -0
- tokensaver_agent-0.6.0/tokensaver/models.py +73 -0
- tokensaver_agent-0.6.0/tokensaver/planner.py +137 -0
- tokensaver_agent-0.6.0/tokensaver/profile.py +336 -0
- tokensaver_agent-0.6.0/tokensaver/runtime.py +267 -0
- tokensaver_agent-0.6.0/tokensaver/store.py +642 -0
- tokensaver_agent-0.6.0/tokensaver/task_classifier.py +81 -0
- tokensaver_agent-0.6.0/tokensaver/tokenizer.py +34 -0
- tokensaver_agent-0.6.0/tokensaver/update.py +210 -0
- tokensaver_agent-0.6.0/tokensaver_agent.egg-info/PKG-INFO +273 -0
- tokensaver_agent-0.6.0/tokensaver_agent.egg-info/SOURCES.txt +38 -0
- tokensaver_agent-0.6.0/tokensaver_agent.egg-info/dependency_links.txt +1 -0
- tokensaver_agent-0.6.0/tokensaver_agent.egg-info/entry_points.txt +4 -0
- tokensaver_agent-0.6.0/tokensaver_agent.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction, and
|
|
10
|
+
distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by the
|
|
13
|
+
copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all other
|
|
16
|
+
entities that control, are controlled by, or are under common control with
|
|
17
|
+
that entity. For the purposes of this definition, "control" means (i) the
|
|
18
|
+
power, direct or indirect, to cause the direction or management of such
|
|
19
|
+
entity, whether by contract or otherwise, or (ii) ownership of fifty percent
|
|
20
|
+
(50%) or more of the outstanding shares, or (iii) beneficial ownership of
|
|
21
|
+
such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising
|
|
24
|
+
permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation source, and
|
|
28
|
+
configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical transformation or
|
|
31
|
+
translation of a Source form, including but not limited to compiled object
|
|
32
|
+
code, generated documentation, and conversions to other media types.
|
|
33
|
+
|
|
34
|
+
"Work" shall mean the work of authorship, whether in Source or Object form,
|
|
35
|
+
made available under the License, as indicated by a copyright notice that is
|
|
36
|
+
included in or attached to the work.
|
|
37
|
+
|
|
38
|
+
"Derivative Works" shall mean any work, whether in Source or Object form,
|
|
39
|
+
that is based on (or derived from) the Work and for which the editorial
|
|
40
|
+
revisions, annotations, elaborations, or other modifications represent, as a
|
|
41
|
+
whole, an original work of authorship. For the purposes of this License,
|
|
42
|
+
Derivative Works shall not include works that remain separable from, or merely
|
|
43
|
+
link (or bind by name) to the interfaces of, the Work and Derivative Works
|
|
44
|
+
thereof.
|
|
45
|
+
|
|
46
|
+
"Contribution" shall mean any work of authorship, including the original
|
|
47
|
+
version of the Work and any modifications or additions to that Work or
|
|
48
|
+
Derivative Works thereof, that is intentionally submitted to Licensor for
|
|
49
|
+
inclusion in the Work by the copyright owner or by an individual or Legal
|
|
50
|
+
Entity authorized to submit on behalf of the copyright owner. For the purposes
|
|
51
|
+
of this definition, "submitted" means any form of electronic, verbal, or
|
|
52
|
+
written communication sent to the Licensor or its representatives, including
|
|
53
|
+
but not limited to communication on electronic mailing lists, source code
|
|
54
|
+
control systems, and issue tracking systems that are managed by, or on behalf
|
|
55
|
+
of, the Licensor for the purpose of discussing and improving the Work, but
|
|
56
|
+
excluding communication that is conspicuously marked or otherwise designated
|
|
57
|
+
in writing by the copyright owner as "Not a Contribution."
|
|
58
|
+
|
|
59
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on
|
|
60
|
+
behalf of whom a Contribution has been received by Licensor and subsequently
|
|
61
|
+
incorporated within the Work.
|
|
62
|
+
|
|
63
|
+
2. Grant of Copyright License. Subject to the terms and conditions of this
|
|
64
|
+
License, each Contributor hereby grants to You a perpetual, worldwide,
|
|
65
|
+
non-exclusive, no-charge, royalty-free, irrevocable copyright license to
|
|
66
|
+
reproduce, prepare Derivative Works of, publicly display, publicly perform,
|
|
67
|
+
sublicense, and distribute the Work and such Derivative Works in Source or
|
|
68
|
+
Object form.
|
|
69
|
+
|
|
70
|
+
3. Grant of Patent License. Subject to the terms and conditions of this
|
|
71
|
+
License, each Contributor hereby grants to You a perpetual, worldwide,
|
|
72
|
+
non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this
|
|
73
|
+
section) patent license to make, have made, use, offer to sell, sell, import,
|
|
74
|
+
and otherwise transfer the Work, where such license applies only to those
|
|
75
|
+
patent claims licensable by such Contributor that are necessarily infringed by
|
|
76
|
+
their Contribution(s) alone or by combination of their Contribution(s) with
|
|
77
|
+
the Work to which such Contribution(s) was submitted. If You institute patent
|
|
78
|
+
litigation against any entity (including a cross-claim or counterclaim in a
|
|
79
|
+
lawsuit) alleging that the Work or a Contribution incorporated within the Work
|
|
80
|
+
constitutes direct or contributory patent infringement, then any patent
|
|
81
|
+
licenses granted to You under this License for that Work shall terminate as of
|
|
82
|
+
the date such litigation is filed.
|
|
83
|
+
|
|
84
|
+
4. Redistribution. You may reproduce and distribute copies of the Work or
|
|
85
|
+
Derivative Works thereof in any medium, with or without modifications, and in
|
|
86
|
+
Source or Object form, provided that You meet the following conditions:
|
|
87
|
+
|
|
88
|
+
(a) You must give any other recipients of the Work or Derivative Works a copy
|
|
89
|
+
of this License; and
|
|
90
|
+
|
|
91
|
+
(b) You must cause any modified files to carry prominent notices stating that
|
|
92
|
+
You changed the files; and
|
|
93
|
+
|
|
94
|
+
(c) You must retain, in the Source form of any Derivative Works that You
|
|
95
|
+
distribute, all copyright, patent, trademark, and attribution notices from the
|
|
96
|
+
Source form of the Work, excluding those notices that do not pertain to any
|
|
97
|
+
part of the Derivative Works; and
|
|
98
|
+
|
|
99
|
+
(d) If the Work includes a "NOTICE" text file as part of its distribution,
|
|
100
|
+
then any Derivative Works that You distribute must include a readable copy of
|
|
101
|
+
the attribution notices contained within such NOTICE file, excluding those
|
|
102
|
+
notices that do not pertain to any part of the Derivative Works, in at least
|
|
103
|
+
one of the following places: within a NOTICE text file distributed as part of
|
|
104
|
+
the Derivative Works; within the Source form or documentation, if provided
|
|
105
|
+
along with the Derivative Works; or within a display generated by the
|
|
106
|
+
Derivative Works, if and wherever such third-party notices normally appear.
|
|
107
|
+
The contents of the NOTICE file are for informational purposes only and do not
|
|
108
|
+
modify the License. You may add Your own attribution notices within
|
|
109
|
+
Derivative Works that You distribute, alongside or as an addendum to the
|
|
110
|
+
NOTICE text from the Work, provided that such additional attribution notices
|
|
111
|
+
cannot be construed as modifying the License.
|
|
112
|
+
|
|
113
|
+
You may add Your own copyright statement to Your modifications and may provide
|
|
114
|
+
additional or different license terms and conditions for use, reproduction, or
|
|
115
|
+
distribution of Your modifications, or for any such Derivative Works as a
|
|
116
|
+
whole, provided Your use, reproduction, and distribution of the Work otherwise
|
|
117
|
+
complies with the conditions stated in this License.
|
|
118
|
+
|
|
119
|
+
5. Submission of Contributions. Unless You explicitly state otherwise, any
|
|
120
|
+
Contribution intentionally submitted for inclusion in the Work by You to the
|
|
121
|
+
Licensor shall be under the terms and conditions of this License, without any
|
|
122
|
+
additional terms or conditions. Notwithstanding the above, nothing herein
|
|
123
|
+
shall supersede or modify the terms of any separate license agreement you may
|
|
124
|
+
have executed with Licensor regarding such Contributions.
|
|
125
|
+
|
|
126
|
+
6. Trademarks. This License does not grant permission to use the trade names,
|
|
127
|
+
trademarks, service marks, or product names of the Licensor, except as
|
|
128
|
+
required for reasonable and customary use in describing the origin of the Work
|
|
129
|
+
and reproducing the content of the NOTICE file.
|
|
130
|
+
|
|
131
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in
|
|
132
|
+
writing, Licensor provides the Work (and each Contributor provides its
|
|
133
|
+
Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
134
|
+
KIND, either express or implied, including, without limitation, any warranties
|
|
135
|
+
or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
136
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
137
|
+
appropriateness of using or redistributing the Work and assume any risks
|
|
138
|
+
associated with Your exercise of permissions under this License.
|
|
139
|
+
|
|
140
|
+
8. Limitation of Liability. In no event and under no legal theory, whether in
|
|
141
|
+
tort (including negligence), contract, or otherwise, unless required by
|
|
142
|
+
applicable law (such as deliberate and grossly negligent acts) or agreed to in
|
|
143
|
+
writing, shall any Contributor be liable to You for damages, including any
|
|
144
|
+
direct, indirect, special, incidental, or consequential damages of any
|
|
145
|
+
character arising as a result of this License or out of the use or inability
|
|
146
|
+
to use the Work (including but not limited to damages for loss of goodwill,
|
|
147
|
+
work stoppage, computer failure or malfunction, or any and all other
|
|
148
|
+
commercial damages or losses), even if such Contributor has been advised of
|
|
149
|
+
the possibility of such damages.
|
|
150
|
+
|
|
151
|
+
9. Accepting Warranty or Additional Liability. While redistributing the Work
|
|
152
|
+
or Derivative Works thereof, You may choose to offer, and charge a fee for,
|
|
153
|
+
acceptance of support, warranty, indemnity, or other liability obligations
|
|
154
|
+
and/or rights consistent with this License. However, in accepting such
|
|
155
|
+
obligations, You may act only on Your own behalf and on Your sole
|
|
156
|
+
responsibility, not on behalf of any other Contributor, and only if You agree
|
|
157
|
+
to indemnify, defend, and hold each Contributor harmless for any liability
|
|
158
|
+
incurred by, or claims asserted against, such Contributor by reason of your
|
|
159
|
+
accepting any such warranty or additional liability.
|
|
160
|
+
|
|
161
|
+
END OF TERMS AND CONDITIONS
|
|
162
|
+
|
|
163
|
+
Copyright 2026 TokenSaver Contributors
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: tokensaver-agent
|
|
3
|
+
Version: 0.6.0
|
|
4
|
+
Summary: Open-source runtime ROI diagnosis toolkit for AI Agent applications.
|
|
5
|
+
Author: TokenSaver Contributors
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/zhangtao-jayce/TokenSaver
|
|
8
|
+
Project-URL: Documentation, https://github.com/zhangtao-jayce/TokenSaver#readme
|
|
9
|
+
Project-URL: Issues, https://github.com/zhangtao-jayce/TokenSaver/issues
|
|
10
|
+
Project-URL: Changelog, https://github.com/zhangtao-jayce/TokenSaver/blob/main/CHANGELOG.md
|
|
11
|
+
Keywords: llm,agent,token,mcp,observability,roi,runtime
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
18
|
+
Requires-Python: >=3.10
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
License-File: LICENSE
|
|
21
|
+
Dynamic: license-file
|
|
22
|
+
|
|
23
|
+
# TokenSaver
|
|
24
|
+
|
|
25
|
+
[](https://github.com/zhangtao-jayce/TokenSaver/actions/workflows/ci.yml)
|
|
26
|
+
[](https://www.python.org/)
|
|
27
|
+
[](LICENSE)
|
|
28
|
+
[](#privacy-by-default)
|
|
29
|
+
[](#see-it-in-30-seconds)
|
|
30
|
+
|
|
31
|
+
**Find wasted context, tool calls, model calls, and workflow routes in AI agents, locally. Then generate a repair brief for Codex or Claude Code.**
|
|
32
|
+
|
|
33
|
+
TokenSaver records real Agent runs, diagnoses low-ROI patterns with deterministic local rules, and produces an offline report showing what to repair next.
|
|
34
|
+
|
|
35
|
+
```text
|
|
36
|
+
Agent run -> Local trace -> ROI diagnosis -> Repair brief -> Before/after comparison
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
No hosted account. No required LLM call. No prompt or trace upload by default.
|
|
40
|
+
|
|
41
|
+

|
|
42
|
+
|
|
43
|
+
## See It In 30 Seconds
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
uvx tokensaver-agent demo
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
The offline demo writes a before/after benchmark and local HTML panel to `.tokensaver-demo/`.
|
|
50
|
+
|
|
51
|
+
Or install it:
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
python3 -m pip install tokensaver-agent
|
|
55
|
+
tokensaver demo
|
|
56
|
+
tokensaver open
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
```text
|
|
60
|
+
Input tokens: 32540 -> 2460 (-92.4%)
|
|
61
|
+
Output tokens: 7400 -> 580 (-92.2%)
|
|
62
|
+
Latency: 31000 -> 1700 (-94.5%)
|
|
63
|
+
ROI score: 35 -> 100 (+65)
|
|
64
|
+
Result: ACCEPTED
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
These numbers come from the bundled deterministic demo fixture. They demonstrate the workflow and are not a claim about every Agent application.
|
|
68
|
+
|
|
69
|
+
The generated `share-card.svg` can be attached to a PR, issue, release, or post without exposing prompts or tool payloads.
|
|
70
|
+
|
|
71
|
+
## What It Finds
|
|
72
|
+
|
|
73
|
+
TokenSaver currently detects patterns such as:
|
|
74
|
+
|
|
75
|
+
- short requests routed through deep research workflows
|
|
76
|
+
- oversized or repeated context
|
|
77
|
+
- raw tool payloads and repeated uncached tool calls
|
|
78
|
+
- excessive model input and ReAct loop amplification
|
|
79
|
+
- slow tools, latency budget violations, and missing fallbacks
|
|
80
|
+
- answers that are too long for the delivery channel
|
|
81
|
+
- quality guardrail regressions during optimization
|
|
82
|
+
|
|
83
|
+
It writes:
|
|
84
|
+
|
|
85
|
+
```text
|
|
86
|
+
.tokensaver/
|
|
87
|
+
runs.jsonl
|
|
88
|
+
reports/latest.md
|
|
89
|
+
briefs/latest.md
|
|
90
|
+
panel/index.html
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Integrate With A Coding Agent
|
|
94
|
+
|
|
95
|
+
Paste this into Codex or Claude Code inside your Agent repository:
|
|
96
|
+
|
|
97
|
+
```text
|
|
98
|
+
Integrate TokenSaver into this Agent application:
|
|
99
|
+
https://github.com/zhangtao-jayce/TokenSaver
|
|
100
|
+
|
|
101
|
+
Find the user-message entrypoint, trace route/context/tool/model/final-answer
|
|
102
|
+
data for each run, keep all data local, run one test request, and show:
|
|
103
|
+
- .tokensaver/reports/latest.md
|
|
104
|
+
- .tokensaver/briefs/latest.md
|
|
105
|
+
- .tokensaver/panel/index.html
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
The detailed integration prompt and verification checklist are in [docs/集成指南.md](docs/集成指南.md).
|
|
109
|
+
|
|
110
|
+
## Minimal Python Integration
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
from tokensaver import TokenSaver
|
|
114
|
+
from tokensaver.integrations import trace_openai_chat_completion
|
|
115
|
+
|
|
116
|
+
tokensaver = TokenSaver(app="my-agent", channel="chat")
|
|
117
|
+
|
|
118
|
+
def handle_message(message: str) -> str:
|
|
119
|
+
with tokensaver.run(user_message=message) as run:
|
|
120
|
+
run.set_task(task_type="quick_question", route="default")
|
|
121
|
+
run.add_context("ticket", load_ticket(message), kind="crm")
|
|
122
|
+
|
|
123
|
+
response = trace_openai_chat_completion(
|
|
124
|
+
run,
|
|
125
|
+
client=openai_client,
|
|
126
|
+
model="gpt-4.1-mini",
|
|
127
|
+
messages=[{"role": "user", "content": message}],
|
|
128
|
+
)
|
|
129
|
+
answer = response.choices[0].message.content
|
|
130
|
+
run.record_final_answer(answer)
|
|
131
|
+
return answer
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
Dependency-free adapters are included for:
|
|
135
|
+
|
|
136
|
+
- OpenAI Chat Completions and Responses
|
|
137
|
+
- Anthropic Messages
|
|
138
|
+
- LiteLLM
|
|
139
|
+
- LangChain and LangGraph callbacks
|
|
140
|
+
- framework-agnostic callbacks
|
|
141
|
+
- TypeScript and Vercel AI SDK JSON imports
|
|
142
|
+
|
|
143
|
+
## Compare A Repair
|
|
144
|
+
|
|
145
|
+
After changing the Agent workflow, record an equivalent run and compare it:
|
|
146
|
+
|
|
147
|
+
```bash
|
|
148
|
+
tokensaver compare \
|
|
149
|
+
--before BEFORE_RUN_ID \
|
|
150
|
+
--after AFTER_RUN_ID
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
TokenSaver reports token, latency, ROI score, resolved findings, new findings, and quality blockers. An optimization is rejected when it introduces tracked quality regressions.
|
|
154
|
+
|
|
155
|
+
Generate a public Markdown report and anonymous SVG card directly from two run files:
|
|
156
|
+
|
|
157
|
+
```bash
|
|
158
|
+
tokensaver benchmark \
|
|
159
|
+
--before-file before.json \
|
|
160
|
+
--after-file after.json \
|
|
161
|
+
--output-dir .tokensaver-benchmark
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
Three deterministic cases are included:
|
|
165
|
+
|
|
166
|
+
- [LangGraph repeated tool calls](examples/case-studies/langgraph-repeated-tools)
|
|
167
|
+
- [OpenAI coding agent context waste](examples/case-studies/openai-context-waste)
|
|
168
|
+
- [RAG oversized retrieval payload](examples/case-studies/rag-oversized-retrieval)
|
|
169
|
+
|
|
170
|
+
See [examples/case-studies/README.md](examples/case-studies/README.md) for exact commands.
|
|
171
|
+
|
|
172
|
+
## CLI
|
|
173
|
+
|
|
174
|
+
```bash
|
|
175
|
+
# Product demo
|
|
176
|
+
tokensaver demo
|
|
177
|
+
tokensaver open
|
|
178
|
+
|
|
179
|
+
# Installation and environment checks
|
|
180
|
+
tokensaver version --verbose
|
|
181
|
+
tokensaver doctor
|
|
182
|
+
tokensaver init-profile --template coding-agent
|
|
183
|
+
|
|
184
|
+
# Record and inspect a run
|
|
185
|
+
tokensaver record-run --file examples/run.json
|
|
186
|
+
tokensaver latest --kind summary
|
|
187
|
+
tokensaver latest --kind brief
|
|
188
|
+
tokensaver latest --kind panel
|
|
189
|
+
|
|
190
|
+
# Analyze multiple runs
|
|
191
|
+
tokensaver list --limit 20
|
|
192
|
+
tokensaver top-tools --last 50
|
|
193
|
+
tokensaver compare --before RUN_ID --after RUN_ID
|
|
194
|
+
tokensaver benchmark --before-file before.json --after-file after.json
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
If the console script is not on `PATH`, use `python3 -m tokensaver.cli` in place of `tokensaver`.
|
|
198
|
+
|
|
199
|
+
## Profiles
|
|
200
|
+
|
|
201
|
+
Profiles keep project-specific budgets and quality requirements outside application code:
|
|
202
|
+
|
|
203
|
+
```yaml
|
|
204
|
+
app: my_agent
|
|
205
|
+
channel: chat
|
|
206
|
+
budgets:
|
|
207
|
+
quick_question:
|
|
208
|
+
input_tokens: 3000
|
|
209
|
+
output_tokens: 500
|
|
210
|
+
latency_ms: 20000
|
|
211
|
+
required_fields:
|
|
212
|
+
quick_question:
|
|
213
|
+
- conclusion
|
|
214
|
+
- next_action
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
Built-in templates:
|
|
218
|
+
|
|
219
|
+
```text
|
|
220
|
+
chatbot, coding-agent, crm-agent, finance-assistant,
|
|
221
|
+
legal-assistant, research-agent, support-bot
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
## MCP
|
|
225
|
+
|
|
226
|
+
Start the dependency-free stdio server:
|
|
227
|
+
|
|
228
|
+
```bash
|
|
229
|
+
tokensaver-mcp
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
Main tools include:
|
|
233
|
+
|
|
234
|
+
- `tokensaver.plan_task`
|
|
235
|
+
- `tokensaver.record_agent_run`
|
|
236
|
+
- `tokensaver.diagnose_roi`
|
|
237
|
+
- `tokensaver.generate_repair_brief`
|
|
238
|
+
- `tokensaver.eval_fixtures`
|
|
239
|
+
- `tokensaver.doctor`
|
|
240
|
+
|
|
241
|
+
## Privacy By Default
|
|
242
|
+
|
|
243
|
+
TokenSaver is local-first:
|
|
244
|
+
|
|
245
|
+
- prompts, context, traces, and tool results are not uploaded by default
|
|
246
|
+
- the core diagnosis loop does not call an LLM
|
|
247
|
+
- stored traces omit raw context and tool text after estimating their size
|
|
248
|
+
- the HTML panel is a static offline file
|
|
249
|
+
|
|
250
|
+
See [OPEN_SOURCE_SCOPE.md](OPEN_SOURCE_SCOPE.md) and [SECURITY.md](SECURITY.md) for the current boundary.
|
|
251
|
+
|
|
252
|
+
## Project Status
|
|
253
|
+
|
|
254
|
+
TokenSaver is beta software. The local trace, diagnosis, repair brief, comparison, GUI panel, integration helpers, CLI, and MCP server are implemented. It is not currently a hosted observability platform or automatic LLM gateway.
|
|
255
|
+
|
|
256
|
+
Useful project documents:
|
|
257
|
+
|
|
258
|
+
- [Integration guide](docs/集成指南.md)
|
|
259
|
+
- [Changelog](CHANGELOG.md)
|
|
260
|
+
- [Contributing](CONTRIBUTING.md)
|
|
261
|
+
- [Open-source scope](OPEN_SOURCE_SCOPE.md)
|
|
262
|
+
|
|
263
|
+
## Development
|
|
264
|
+
|
|
265
|
+
```bash
|
|
266
|
+
git clone https://github.com/zhangtao-jayce/TokenSaver.git
|
|
267
|
+
cd TokenSaver
|
|
268
|
+
python3 -m unittest discover -s tests
|
|
269
|
+
python3 -m py_compile tokensaver/*.py
|
|
270
|
+
python3 -m tokensaver.cli demo --store-dir /private/tmp/tokensaver-demo
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
Contributions that improve real Agent integrations, diagnosis rules, benchmark fixtures, and before/after case studies are especially useful.
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
# TokenSaver
|
|
2
|
+
|
|
3
|
+
[](https://github.com/zhangtao-jayce/TokenSaver/actions/workflows/ci.yml)
|
|
4
|
+
[](https://www.python.org/)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
[](#privacy-by-default)
|
|
7
|
+
[](#see-it-in-30-seconds)
|
|
8
|
+
|
|
9
|
+
**Find wasted context, tool calls, model calls, and workflow routes in AI agents, locally. Then generate a repair brief for Codex or Claude Code.**
|
|
10
|
+
|
|
11
|
+
TokenSaver records real Agent runs, diagnoses low-ROI patterns with deterministic local rules, and produces an offline report showing what to repair next.
|
|
12
|
+
|
|
13
|
+
```text
|
|
14
|
+
Agent run -> Local trace -> ROI diagnosis -> Repair brief -> Before/after comparison
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
No hosted account. No required LLM call. No prompt or trace upload by default.
|
|
18
|
+
|
|
19
|
+

|
|
20
|
+
|
|
21
|
+
## See It In 30 Seconds
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
uvx tokensaver-agent demo
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
The offline demo writes a before/after benchmark and local HTML panel to `.tokensaver-demo/`.
|
|
28
|
+
|
|
29
|
+
Or install it:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
python3 -m pip install tokensaver-agent
|
|
33
|
+
tokensaver demo
|
|
34
|
+
tokensaver open
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
```text
|
|
38
|
+
Input tokens: 32540 -> 2460 (-92.4%)
|
|
39
|
+
Output tokens: 7400 -> 580 (-92.2%)
|
|
40
|
+
Latency: 31000 -> 1700 (-94.5%)
|
|
41
|
+
ROI score: 35 -> 100 (+65)
|
|
42
|
+
Result: ACCEPTED
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
These numbers come from the bundled deterministic demo fixture. They demonstrate the workflow and are not a claim about every Agent application.
|
|
46
|
+
|
|
47
|
+
The generated `share-card.svg` can be attached to a PR, issue, release, or post without exposing prompts or tool payloads.
|
|
48
|
+
|
|
49
|
+
## What It Finds
|
|
50
|
+
|
|
51
|
+
TokenSaver currently detects patterns such as:
|
|
52
|
+
|
|
53
|
+
- short requests routed through deep research workflows
|
|
54
|
+
- oversized or repeated context
|
|
55
|
+
- raw tool payloads and repeated uncached tool calls
|
|
56
|
+
- excessive model input and ReAct loop amplification
|
|
57
|
+
- slow tools, latency budget violations, and missing fallbacks
|
|
58
|
+
- answers that are too long for the delivery channel
|
|
59
|
+
- quality guardrail regressions during optimization
|
|
60
|
+
|
|
61
|
+
It writes:
|
|
62
|
+
|
|
63
|
+
```text
|
|
64
|
+
.tokensaver/
|
|
65
|
+
runs.jsonl
|
|
66
|
+
reports/latest.md
|
|
67
|
+
briefs/latest.md
|
|
68
|
+
panel/index.html
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Integrate With A Coding Agent
|
|
72
|
+
|
|
73
|
+
Paste this into Codex or Claude Code inside your Agent repository:
|
|
74
|
+
|
|
75
|
+
```text
|
|
76
|
+
Integrate TokenSaver into this Agent application:
|
|
77
|
+
https://github.com/zhangtao-jayce/TokenSaver
|
|
78
|
+
|
|
79
|
+
Find the user-message entrypoint, trace route/context/tool/model/final-answer
|
|
80
|
+
data for each run, keep all data local, run one test request, and show:
|
|
81
|
+
- .tokensaver/reports/latest.md
|
|
82
|
+
- .tokensaver/briefs/latest.md
|
|
83
|
+
- .tokensaver/panel/index.html
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
The detailed integration prompt and verification checklist are in [docs/集成指南.md](docs/集成指南.md).
|
|
87
|
+
|
|
88
|
+
## Minimal Python Integration
|
|
89
|
+
|
|
90
|
+
```python
|
|
91
|
+
from tokensaver import TokenSaver
|
|
92
|
+
from tokensaver.integrations import trace_openai_chat_completion
|
|
93
|
+
|
|
94
|
+
tokensaver = TokenSaver(app="my-agent", channel="chat")
|
|
95
|
+
|
|
96
|
+
def handle_message(message: str) -> str:
|
|
97
|
+
with tokensaver.run(user_message=message) as run:
|
|
98
|
+
run.set_task(task_type="quick_question", route="default")
|
|
99
|
+
run.add_context("ticket", load_ticket(message), kind="crm")
|
|
100
|
+
|
|
101
|
+
response = trace_openai_chat_completion(
|
|
102
|
+
run,
|
|
103
|
+
client=openai_client,
|
|
104
|
+
model="gpt-4.1-mini",
|
|
105
|
+
messages=[{"role": "user", "content": message}],
|
|
106
|
+
)
|
|
107
|
+
answer = response.choices[0].message.content
|
|
108
|
+
run.record_final_answer(answer)
|
|
109
|
+
return answer
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Dependency-free adapters are included for:
|
|
113
|
+
|
|
114
|
+
- OpenAI Chat Completions and Responses
|
|
115
|
+
- Anthropic Messages
|
|
116
|
+
- LiteLLM
|
|
117
|
+
- LangChain and LangGraph callbacks
|
|
118
|
+
- framework-agnostic callbacks
|
|
119
|
+
- TypeScript and Vercel AI SDK JSON imports
|
|
120
|
+
|
|
121
|
+
## Compare A Repair
|
|
122
|
+
|
|
123
|
+
After changing the Agent workflow, record an equivalent run and compare it:
|
|
124
|
+
|
|
125
|
+
```bash
|
|
126
|
+
tokensaver compare \
|
|
127
|
+
--before BEFORE_RUN_ID \
|
|
128
|
+
--after AFTER_RUN_ID
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
TokenSaver reports token, latency, ROI score, resolved findings, new findings, and quality blockers. An optimization is rejected when it introduces tracked quality regressions.
|
|
132
|
+
|
|
133
|
+
Generate a public Markdown report and anonymous SVG card directly from two run files:
|
|
134
|
+
|
|
135
|
+
```bash
|
|
136
|
+
tokensaver benchmark \
|
|
137
|
+
--before-file before.json \
|
|
138
|
+
--after-file after.json \
|
|
139
|
+
--output-dir .tokensaver-benchmark
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
Three deterministic cases are included:
|
|
143
|
+
|
|
144
|
+
- [LangGraph repeated tool calls](examples/case-studies/langgraph-repeated-tools)
|
|
145
|
+
- [OpenAI coding agent context waste](examples/case-studies/openai-context-waste)
|
|
146
|
+
- [RAG oversized retrieval payload](examples/case-studies/rag-oversized-retrieval)
|
|
147
|
+
|
|
148
|
+
See [examples/case-studies/README.md](examples/case-studies/README.md) for exact commands.
|
|
149
|
+
|
|
150
|
+
## CLI
|
|
151
|
+
|
|
152
|
+
```bash
|
|
153
|
+
# Product demo
|
|
154
|
+
tokensaver demo
|
|
155
|
+
tokensaver open
|
|
156
|
+
|
|
157
|
+
# Installation and environment checks
|
|
158
|
+
tokensaver version --verbose
|
|
159
|
+
tokensaver doctor
|
|
160
|
+
tokensaver init-profile --template coding-agent
|
|
161
|
+
|
|
162
|
+
# Record and inspect a run
|
|
163
|
+
tokensaver record-run --file examples/run.json
|
|
164
|
+
tokensaver latest --kind summary
|
|
165
|
+
tokensaver latest --kind brief
|
|
166
|
+
tokensaver latest --kind panel
|
|
167
|
+
|
|
168
|
+
# Analyze multiple runs
|
|
169
|
+
tokensaver list --limit 20
|
|
170
|
+
tokensaver top-tools --last 50
|
|
171
|
+
tokensaver compare --before RUN_ID --after RUN_ID
|
|
172
|
+
tokensaver benchmark --before-file before.json --after-file after.json
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
If the console script is not on `PATH`, use `python3 -m tokensaver.cli` in place of `tokensaver`.
|
|
176
|
+
|
|
177
|
+
## Profiles
|
|
178
|
+
|
|
179
|
+
Profiles keep project-specific budgets and quality requirements outside application code:
|
|
180
|
+
|
|
181
|
+
```yaml
|
|
182
|
+
app: my_agent
|
|
183
|
+
channel: chat
|
|
184
|
+
budgets:
|
|
185
|
+
quick_question:
|
|
186
|
+
input_tokens: 3000
|
|
187
|
+
output_tokens: 500
|
|
188
|
+
latency_ms: 20000
|
|
189
|
+
required_fields:
|
|
190
|
+
quick_question:
|
|
191
|
+
- conclusion
|
|
192
|
+
- next_action
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
Built-in templates:
|
|
196
|
+
|
|
197
|
+
```text
|
|
198
|
+
chatbot, coding-agent, crm-agent, finance-assistant,
|
|
199
|
+
legal-assistant, research-agent, support-bot
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
## MCP
|
|
203
|
+
|
|
204
|
+
Start the dependency-free stdio server:
|
|
205
|
+
|
|
206
|
+
```bash
|
|
207
|
+
tokensaver-mcp
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
Main tools include:
|
|
211
|
+
|
|
212
|
+
- `tokensaver.plan_task`
|
|
213
|
+
- `tokensaver.record_agent_run`
|
|
214
|
+
- `tokensaver.diagnose_roi`
|
|
215
|
+
- `tokensaver.generate_repair_brief`
|
|
216
|
+
- `tokensaver.eval_fixtures`
|
|
217
|
+
- `tokensaver.doctor`
|
|
218
|
+
|
|
219
|
+
## Privacy By Default
|
|
220
|
+
|
|
221
|
+
TokenSaver is local-first:
|
|
222
|
+
|
|
223
|
+
- prompts, context, traces, and tool results are not uploaded by default
|
|
224
|
+
- the core diagnosis loop does not call an LLM
|
|
225
|
+
- stored traces omit raw context and tool text after estimating their size
|
|
226
|
+
- the HTML panel is a static offline file
|
|
227
|
+
|
|
228
|
+
See [OPEN_SOURCE_SCOPE.md](OPEN_SOURCE_SCOPE.md) and [SECURITY.md](SECURITY.md) for the current boundary.
|
|
229
|
+
|
|
230
|
+
## Project Status
|
|
231
|
+
|
|
232
|
+
TokenSaver is beta software. The local trace, diagnosis, repair brief, comparison, GUI panel, integration helpers, CLI, and MCP server are implemented. It is not currently a hosted observability platform or automatic LLM gateway.
|
|
233
|
+
|
|
234
|
+
Useful project documents:
|
|
235
|
+
|
|
236
|
+
- [Integration guide](docs/集成指南.md)
|
|
237
|
+
- [Changelog](CHANGELOG.md)
|
|
238
|
+
- [Contributing](CONTRIBUTING.md)
|
|
239
|
+
- [Open-source scope](OPEN_SOURCE_SCOPE.md)
|
|
240
|
+
|
|
241
|
+
## Development
|
|
242
|
+
|
|
243
|
+
```bash
|
|
244
|
+
git clone https://github.com/zhangtao-jayce/TokenSaver.git
|
|
245
|
+
cd TokenSaver
|
|
246
|
+
python3 -m unittest discover -s tests
|
|
247
|
+
python3 -m py_compile tokensaver/*.py
|
|
248
|
+
python3 -m tokensaver.cli demo --store-dir /private/tmp/tokensaver-demo
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
Contributions that improve real Agent integrations, diagnosis rules, benchmark fixtures, and before/after case studies are especially useful.
|