toolgz 0.1.0
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.
- package/LICENSE +201 -0
- package/NOTICE +16 -0
- package/README.md +245 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +309 -0
- package/dist/providers/index.d.ts +69 -0
- package/dist/providers/index.js +108 -0
- package/dist/recommend.d.ts +41 -0
- package/dist/recommend.js +56 -0
- package/dist/render/index.d.ts +34 -0
- package/dist/render/index.js +143 -0
- package/dist/runtime/similar.d.ts +26 -0
- package/dist/runtime/similar.js +86 -0
- package/dist/runtime/validate.d.ts +11 -0
- package/dist/runtime/validate.js +87 -0
- package/dist/types.d.ts +112 -0
- package/dist/types.js +2 -0
- package/package.json +71 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
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,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising 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
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Daniel Perussina
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
toolgz
|
|
2
|
+
Copyright 2026 Daniel Perussina
|
|
3
|
+
|
|
4
|
+
This product includes software developed by the toolgz contributors.
|
|
5
|
+
|
|
6
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
you may not use this file except in compliance with the License.
|
|
8
|
+
You may obtain a copy of the License at
|
|
9
|
+
|
|
10
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
|
|
12
|
+
Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
See the License for the specific language governing permissions and
|
|
16
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
<h1>toolgz</h1>
|
|
2
|
+
|
|
3
|
+
<p><strong>Your agent spends 30–50k tokens of context on tool definitions before the user types a word. toolgz gets ~80% of it back.</strong></p>
|
|
4
|
+
|
|
5
|
+
<p>
|
|
6
|
+
<a href="#measured-results">420-run cross-provider sweep</a> ·
|
|
7
|
+
4 frontier models ·
|
|
8
|
+
zero runtime dependencies ·
|
|
9
|
+
<a href="docs/BEFORE-AFTER.md">generated before/after</a>
|
|
10
|
+
</p>
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install toolgz
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## The problem
|
|
19
|
+
|
|
20
|
+
You connect a few MCP servers. Each ships 20–50 tools. Every tool is a JSON Schema with a
|
|
21
|
+
sentence of prose per parameter. That block renders at the **front of every single request**.
|
|
22
|
+
|
|
23
|
+
A realistic tool definition is ~420 tokens, and roughly 400 of them are prose the model
|
|
24
|
+
doesn't need in order to pick correctly. Fifty tools is 20k tokens. A hundred is 40k.
|
|
25
|
+
|
|
26
|
+
Prompt caching makes those tokens *cheap*. It does not make them take up less *room*.
|
|
27
|
+
Reclaiming the room is what this does.
|
|
28
|
+
|
|
29
|
+
## The fix, in three lines
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import { compress, forAnthropic } from "toolgz";
|
|
33
|
+
|
|
34
|
+
const c = compress(myTools); // your existing MCP/SDK tool array
|
|
35
|
+
const { tools, system } = forAnthropic(c); // send these instead
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Then translate the model's call back before you dispatch:
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
const r = c.resolve(block.name, block.input);
|
|
42
|
+
if (r.kind === "call") await myDispatch(r.name, r.args); // real name, real args
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
`myDispatch` gets exactly what it got before. **Nothing downstream changes.**
|
|
46
|
+
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
## Measured results
|
|
50
|
+
|
|
51
|
+
Four frontier models, seven strategies, five tool-selection tasks, 3 reps —
|
|
52
|
+
**420 runs** on the current sweep, 1,200+ across all rounds. Every raw per-run record is
|
|
53
|
+
committed in [`bench/results/`](bench/results/); recompute any figure with
|
|
54
|
+
`npx tsx bench/analyze-multi.ts`.
|
|
55
|
+
|
|
56
|
+
<picture>
|
|
57
|
+
<source media="(prefers-color-scheme: dark)" srcset="docs/img/savings-dark.svg">
|
|
58
|
+
<img src="docs/img/savings-light.svg" alt="Prompt tokens saved versus uncompressed tool definitions, by compression level, for each of four providers">
|
|
59
|
+
</picture>
|
|
60
|
+
|
|
61
|
+
| Provider | Model | Tool block | Prompt tokens | Cost | Latency | Tasks |
|
|
62
|
+
|---|---|---:|---:|---:|---:|:-:|
|
|
63
|
+
| Anthropic | `claude-opus-5` | 9,242 → **1,284** | 30,817 → **4,628** (−85%) | **−78%** | 15.0s → **12.1s** | 15/15 |
|
|
64
|
+
| xAI | `grok-4.5` | 6,421 → **775** | 17,522 → **2,663** (−85%) | **−70%** | 6.1s → **4.6s** | 15/15 |
|
|
65
|
+
| Google | `gemini-3.1-pro-preview` | 5,264 → **732** | 10,948 → **2,302** (−79%) | **−62%** | 5.6s → **5.5s** | 15/15 |
|
|
66
|
+
| OpenAI | `gpt-5.6-sol` | 2,752 → **573** | 7,694 → **2,196** (−71%) | **−7%** | 6.8s → **5.6s** | 15/15 |
|
|
67
|
+
|
|
68
|
+
Reasoning is enabled on all four at high effort, so this is a like-for-like frontier
|
|
69
|
+
comparison. **60/60 tasks completed, zero hallucinated tool names, zero malformed
|
|
70
|
+
arguments** — and it is faster than uncompressed on every provider.
|
|
71
|
+
|
|
72
|
+
### It does not make the model worse
|
|
73
|
+
|
|
74
|
+
That was the thing to disprove, and we tried hard to. The task suite is built from
|
|
75
|
+
deliberately confusable tool clusters — `search_issues` vs `list_issues`, comment-vs-update,
|
|
76
|
+
approve-vs-merge, the same three products side by side — where the correct choice turns on
|
|
77
|
+
the tool name that compression takes away.
|
|
78
|
+
|
|
79
|
+
<picture>
|
|
80
|
+
<source media="(prefers-color-scheme: dark)" srcset="docs/img/reliability-dark.svg">
|
|
81
|
+
<img src="docs/img/reliability-light.svg" alt="Task completion by level-3 map style and provider, showing bare names failing on grok-4.5">
|
|
82
|
+
</picture>
|
|
83
|
+
|
|
84
|
+
The model doesn't lose the ability to choose — it converts a recall problem into a retrieval
|
|
85
|
+
problem and looks up what it needs. The default map style exists because of the red cell:
|
|
86
|
+
bare tool names failed on `grok-4.5` **deterministically**, 3 of 3 attempts on one scenario,
|
|
87
|
+
answering with zero tool calls and no error raised. Naming the required arguments fixed it.
|
|
88
|
+
|
|
89
|
+
### Cost, and why it took two rounds to get right
|
|
90
|
+
|
|
91
|
+
<picture>
|
|
92
|
+
<source media="(prefers-color-scheme: dark)" srcset="docs/img/cost-dark.svg">
|
|
93
|
+
<img src="docs/img/cost-light.svg" alt="Prompt tokens and cost both reduced on all four providers">
|
|
94
|
+
</picture>
|
|
95
|
+
|
|
96
|
+
The first cross-provider sweep found cost going **up 15% on OpenAI** even while context fell
|
|
97
|
+
69%. The dispatcher was spending extra turns, and on a reasoning model every turn pays for a
|
|
98
|
+
fresh round of thinking.
|
|
99
|
+
|
|
100
|
+
So we captured the calls that were being rejected instead of guessing, and found three bugs
|
|
101
|
+
in *this library*: models pass `query` to a parameter named `q` (14 of 18 rejections), they
|
|
102
|
+
sometimes call the map code as the tool name, and they sometimes pass arguments flat instead
|
|
103
|
+
of nested. Fixing all three took OpenAI from **+15% to −7%** and drove malformed arguments to
|
|
104
|
+
**zero on every provider**.
|
|
105
|
+
|
|
106
|
+
OpenAI's −7% is still the smallest saving, and honestly so: reasoning output dominates its
|
|
107
|
+
bill, so a smaller prompt moves the total less. **Context-window occupancy remains the
|
|
108
|
+
primary claim** — cost follows from it, by an amount that depends on your reasoning settings.
|
|
109
|
+
|
|
110
|
+
---
|
|
111
|
+
|
|
112
|
+
## Which level to use
|
|
113
|
+
|
|
114
|
+
Ask the library. It returns 1 or 3, never 2, and explains itself:
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
import { recommendLevel } from "toolgz";
|
|
118
|
+
const { level, reason } = recommendLevel(myTools);
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
| Level | Sends | Real names | Provider schema enforcement | Use when |
|
|
122
|
+
|:-:|---|:-:|:-:|---|
|
|
123
|
+
| **1** | one native tool each, signature-line descriptions | yes | **yes** | **default.** Small or wide-and-sparse tool sets. Zero measured downside. |
|
|
124
|
+
| 2 | one compound tool per namespace | yes | no | you need readable op names on the wire. Otherwise skip. |
|
|
125
|
+
| **3** | one dispatcher + one lookup tool | codes | no | **large, deep tool sets.** The 80% number above. |
|
|
126
|
+
|
|
127
|
+
*(Level 0 is a passthrough, for A/B testing inside your own app.)*
|
|
128
|
+
|
|
129
|
+
**Level 1 is free** — measured: fewer tokens, zero malformed arguments, zero extra turns,
|
|
130
|
+
latency no worse. **Level 2 is dominated by level 3** on every axis, including producing more
|
|
131
|
+
malformed arguments; it is not a stepping stone.
|
|
132
|
+
|
|
133
|
+
### What level 3 actually looks like
|
|
134
|
+
|
|
135
|
+
Two tools on the wire regardless of how many you start with, and a map in the system prompt
|
|
136
|
+
behind a cache breakpoint:
|
|
137
|
+
|
|
138
|
+
```
|
|
139
|
+
<toolmap>
|
|
140
|
+
a0 github_create_issue owner,repo,title
|
|
141
|
+
a1 github_search_issues q
|
|
142
|
+
b0 slack_post_message channel,text
|
|
143
|
+
</toolmap>
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
The model calls `t(f="a0", a={…})`, and `q(c="a0")` expands a code to its full signature when
|
|
147
|
+
it needs the optional parameters.
|
|
148
|
+
|
|
149
|
+
If you want to remove those lookups entirely, put the whole signature in the map:
|
|
150
|
+
|
|
151
|
+
```ts
|
|
152
|
+
compress(myTools, { level: 3, mapStyle: "signature" });
|
|
153
|
+
// a0 github_create_issue(owner,repo,title,body?,labels?)
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Measured: lookups drop to zero and it was the **fastest and cheapest** arm on OpenAI (4.0s,
|
|
157
|
+
−17% cost). It is slightly larger, and on xAI it was worse than the default, so it is an
|
|
158
|
+
option rather than the default.
|
|
159
|
+
|
|
160
|
+
**The trade:** at levels 2–3 the model fills a generic argument object, so the provider's
|
|
161
|
+
sampler no longer enforces your schema. toolgz validates against your *original* schema and
|
|
162
|
+
returns a model-readable error instead. That is why `validate` defaults to on — leave it on.
|
|
163
|
+
|
|
164
|
+
**Every artifact above is generated by running the library** — see
|
|
165
|
+
**[docs/BEFORE-AFTER.md](docs/BEFORE-AFTER.md)** for the full tools array and system prompt,
|
|
166
|
+
before and after, at every level, with real token counts and a live encode → decode round
|
|
167
|
+
trip. A test asserts that file matches the code, so it cannot drift.
|
|
168
|
+
|
|
169
|
+
---
|
|
170
|
+
|
|
171
|
+
## Documentation
|
|
172
|
+
|
|
173
|
+
| | |
|
|
174
|
+
|---|---|
|
|
175
|
+
| **[Complete guide](docs/GUIDE.md)** | Install → working agent loop. Per-provider setup for all four, prompt caching, MCP aggregation, troubleshooting. Start here. |
|
|
176
|
+
| **[Before / after](docs/BEFORE-AFTER.md)** | Generated, not illustrated. Both artifacts toolgz modifies, at every level. |
|
|
177
|
+
| **[Full results](docs/RESULTS.md)** | Every number, the methodology, and what it does **not** establish. |
|
|
178
|
+
| **[Announcement drafts](docs/ANNOUNCEMENT.md)** | Ready-to-post write-ups for HN and LinkedIn, plus claims *not* to make. |
|
|
179
|
+
| **[Releasing](docs/RELEASING.md)** | Publishing to npm over OIDC, with no long-lived token. |
|
|
180
|
+
|
|
181
|
+
## Providers
|
|
182
|
+
|
|
183
|
+
```ts
|
|
184
|
+
import { forAnthropic, forOpenAI, forOpenAIResponses, forGemini } from "toolgz";
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
Pure functions; they never mutate what you pass them.
|
|
188
|
+
|
|
189
|
+
| Adapter | Endpoint | Handles |
|
|
190
|
+
|---|---|---|
|
|
191
|
+
| `forAnthropic` | Messages API | Places one `cache_control` breakpoint; skips deferred tools, which the API rejects |
|
|
192
|
+
| `forOpenAIResponses` | `/v1/responses` | Flat tool shape. **Required if you want tools *and* reasoning** |
|
|
193
|
+
| `forOpenAI` | `/v1/chat/completions` | Nested tool shape |
|
|
194
|
+
| `forGemini` | `generateContent` | One `functionDeclarations` array |
|
|
195
|
+
|
|
196
|
+
xAI is OpenAI-compatible — use `forOpenAI` with `baseURL: "https://api.x.ai/v1"`.
|
|
197
|
+
|
|
198
|
+
---
|
|
199
|
+
|
|
200
|
+
## What this does not do
|
|
201
|
+
|
|
202
|
+
- **The size of the cost saving is not the size of the token saving.** Measured 62–78%
|
|
203
|
+
cheaper on three providers but only 7% on OpenAI, where reasoning output dominates the
|
|
204
|
+
bill. The claim is context-window occupancy; cost follows, by a variable amount.
|
|
205
|
+
- **It does not beat Anthropic's native tool search on tool-block size.** It composes with
|
|
206
|
+
it, works where there is no equivalent, and is more reliable below the frontier tier —
|
|
207
|
+
`defer_loading` completed only 6/30 tasks on Haiku 4.5, silently, because it lets the model
|
|
208
|
+
*choose* whether to discover tools. A dispatcher makes discovery the entry point.
|
|
209
|
+
- **It has not been measured on a non-frontier model at level 3.** On Haiku 4.5, argument
|
|
210
|
+
errors rose sharply (17 of 30 runs) — all caught and retried, no task lost, but that is the
|
|
211
|
+
known edge.
|
|
212
|
+
- **It is not magic on ten tools.** Under ~15 tools there is little to reclaim;
|
|
213
|
+
`recommendLevel()` will tell you so.
|
|
214
|
+
|
|
215
|
+
## Determinism
|
|
216
|
+
|
|
217
|
+
`compress()` is referentially transparent: same tools in, byte-identical payload out. Tools
|
|
218
|
+
are sorted, never left in iteration order.
|
|
219
|
+
|
|
220
|
+
This is a correctness property, not tidiness — prompt caching is a prefix match, so one
|
|
221
|
+
reordered tool silently re-bills your whole prompt. There is a test asserting byte-stability,
|
|
222
|
+
and it does not get deleted.
|
|
223
|
+
|
|
224
|
+
## Development
|
|
225
|
+
|
|
226
|
+
```bash
|
|
227
|
+
npm test # 131 tests, offline, no cost
|
|
228
|
+
npm run build # tsc → dist/ with .d.ts
|
|
229
|
+
|
|
230
|
+
npx tsx bench/harness/run-multi.ts --provider=all --reps=3 --variants # costs money
|
|
231
|
+
npx tsx bench/analyze-multi.ts
|
|
232
|
+
npx tsx docs/generate-examples.ts
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
Methodology and repo conventions: [AGENTS.md](AGENTS.md).
|
|
236
|
+
Principles specs are checked against: [docs/CONSTITUTION.md](docs/CONSTITUTION.md).
|
|
237
|
+
|
|
238
|
+
## License
|
|
239
|
+
|
|
240
|
+
Apache-2.0 — see [LICENSE](LICENSE) and [NOTICE](NOTICE).
|
|
241
|
+
|
|
242
|
+
Apache-2.0 rather than MIT deliberately: it carries an express patent grant and
|
|
243
|
+
a patent-retaliation clause, which matters for a library implementing a
|
|
244
|
+
technique rather than just glue code, and it is the license most enterprises
|
|
245
|
+
prefer in a dependency.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* toolgz — shrink LLM tool definitions without making the model dumber.
|
|
3
|
+
*
|
|
4
|
+
* import { compress } from "toolgz";
|
|
5
|
+
*
|
|
6
|
+
* const c = compress(myTools, { level: 1 });
|
|
7
|
+
* const res = await client.messages.create({ ...,
|
|
8
|
+
* system: [{ type: "text", text: SYSTEM + c.systemPreamble }],
|
|
9
|
+
* tools: c.tools,
|
|
10
|
+
* });
|
|
11
|
+
* for (const block of res.content.filter(b => b.type === "tool_use")) {
|
|
12
|
+
* const r = c.resolve(block.name, block.input);
|
|
13
|
+
* if (r.kind === "call") await myDispatch(r.name, r.args);
|
|
14
|
+
* }
|
|
15
|
+
*/
|
|
16
|
+
import type { CompressOptions, CompressResult, Tool } from "./types.js";
|
|
17
|
+
export * from "./types.js";
|
|
18
|
+
export { flattenSchema, signatureLine, countSchemaTokensApprox } from "./render/index.js";
|
|
19
|
+
export declare function compress(input: Tool[], options?: CompressOptions): CompressResult;
|
|
20
|
+
export { recommendLevel } from "./recommend.js";
|
|
21
|
+
export type { Recommendation } from "./recommend.js";
|
|
22
|
+
export { forAnthropic, forOpenAI, forOpenAIResponses, forGemini } from "./providers/index.js";
|
|
23
|
+
export type { CacheTtl } from "./providers/index.js";
|