@bivelio/savings-layer 0.2.1 → 0.3.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/README.md +32 -36
- package/dist/index.d.ts +504 -135
- package/dist/index.js +23 -21
- package/package.json +19 -24
package/README.md
CHANGED
|
@@ -55,7 +55,7 @@ console.log(report.cost.netSavings); // → verified savings, per request
|
|
|
55
55
|
```
|
|
56
56
|
|
|
57
57
|
Ask for a **structured contract** and you also get `data`, validated against your
|
|
58
|
-
schema — the JSON-repair and
|
|
58
|
+
schema — the JSON-repair and output contracts levers switch on here:
|
|
59
59
|
|
|
60
60
|
```ts
|
|
61
61
|
const { data, text } = await layer.generate<{ answer: number }>({
|
|
@@ -158,18 +158,20 @@ an action that has not run yet cannot be a saving.
|
|
|
158
158
|
> **disabled in this published package** — a valid `licenseKey` is the only way to
|
|
159
159
|
> run it. They are not a way to use the product for free.
|
|
160
160
|
|
|
161
|
-
Every component ships with an in‑memory default and is independently swappable
|
|
162
|
-
|
|
163
|
-
|
|
161
|
+
Every component ships with an in‑memory default and is independently swappable:
|
|
162
|
+
plug in your own cache, vector store, tokenizer, compressor or constraint backend
|
|
163
|
+
behind the same interfaces.
|
|
164
164
|
|
|
165
165
|
## The optimization order (cheapest first)
|
|
166
166
|
|
|
167
|
-
1. **Avoid the call entirely**
|
|
168
|
-
2. **Avoid auxiliary calls** —
|
|
169
|
-
3. **Eliminate unnecessary context** before trying to
|
|
170
|
-
4. **
|
|
167
|
+
1. **Avoid the call entirely** when the answer can be produced or reused safely.
|
|
168
|
+
2. **Avoid auxiliary calls** — the retrievals, tool hops and retries around it.
|
|
169
|
+
3. **Eliminate unnecessary context** before trying to shrink what remains.
|
|
170
|
+
4. **Shrink** the tokens that genuinely must be sent.
|
|
171
171
|
5. **Constrain generation** to emit only the structure and length required.
|
|
172
172
|
|
|
173
|
+
The order is the point: work removed at step 1 costs nothing to compress at step 4.
|
|
174
|
+
|
|
173
175
|
Provider prompt/KV caching is treated as a cost/latency saving — **never** as a
|
|
174
176
|
reduction of the logical prompt size.
|
|
175
177
|
|
|
@@ -195,15 +197,12 @@ Request
|
|
|
195
197
|
→ Canonicalize + Policy (risk & freshness)
|
|
196
198
|
→ Deterministic resolvers ─────────────► answer (0 calls)
|
|
197
199
|
→ Exact cache ─────────────────────────► answer (0 calls)
|
|
198
|
-
→
|
|
199
|
-
→
|
|
200
|
-
→ Execution
|
|
201
|
-
→
|
|
202
|
-
→
|
|
203
|
-
→
|
|
204
|
-
→ Compression (only when net-positive)
|
|
205
|
-
→ JSON / TOON / CSV selector (tokenizer-aware)
|
|
206
|
-
→ Output contract + constrained decoding
|
|
200
|
+
→ Concurrent duplicates coalesced ─────► answer (0 calls)
|
|
201
|
+
→ Near-duplicate reuse ────────────► answer (0 calls)
|
|
202
|
+
→ Execution planning (how much work this really needs)
|
|
203
|
+
→ Context reduction (only when it pays for itself)
|
|
204
|
+
→ Payload encoding chosen by measurement, not by default
|
|
205
|
+
→ Output contract enforced
|
|
207
206
|
→ ONE primary LLM call
|
|
208
207
|
→ Structural + semantic validation (+ ≤1 repair)
|
|
209
208
|
→ Local render / transcode / cache write
|
|
@@ -212,29 +211,26 @@ Request
|
|
|
212
211
|
|
|
213
212
|
## Why formats are chosen, not assumed
|
|
214
213
|
|
|
215
|
-
A compact encoding can be *larger* than plain JSON on nested
|
|
216
|
-
BV‑SALA
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
```
|
|
221
|
-
|
|
222
|
-
…and only moves away from JSON when a challenger clears both an absolute (`≥64`
|
|
223
|
-
tokens) and a relative (`≥8%`) threshold:
|
|
214
|
+
A compact encoding can be *larger* than plain JSON on nested or non‑uniform data,
|
|
215
|
+
so BV‑SALA never assumes one. It counts the candidates with the destination
|
|
216
|
+
model's own tokenizer — including the cost of explaining the encoding to the
|
|
217
|
+
model — and only moves away from JSON when the saving clears a margin worth the
|
|
218
|
+
change.
|
|
224
219
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
220
|
+
On a wide, uniform table that is a large reduction. On a small nested object it
|
|
221
|
+
leaves JSON alone. Whichever it picks, `report.serialization` tells you what was
|
|
222
|
+
chosen and what each candidate would have cost, so the decision is auditable
|
|
223
|
+
rather than promised.
|
|
229
224
|
|
|
230
225
|
## What's inside
|
|
231
226
|
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
227
|
+
Safe cache keys and a risk/freshness policy · several independent ways to answer
|
|
228
|
+
without spending a call · execution planning · context reduction that must pay for
|
|
229
|
+
itself · measured payload encoding · output contracts with validation · a
|
|
230
|
+
provider‑neutral adapter · and a trace collector that builds the `SavingsReport`.
|
|
231
|
+
|
|
232
|
+
Every piece is pluggable, and every saving it claims is reported per request with
|
|
233
|
+
the token counts behind it.
|
|
238
234
|
|
|
239
235
|
## Licensing & privacy
|
|
240
236
|
|