@bivelio/savings-layer 0.2.2 → 0.3.1

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 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 constrained-decoding levers switch on here:
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 }>({
@@ -78,8 +78,10 @@ console.log(data.answer); // 8
78
78
 
79
79
  The hard requirements are a `provider` and a valid `licenseKey`: BV‑SALA is a paid
80
80
  product and **refuses to run unlicensed, expired, or tampered** (it throws
81
- `LicenseError` at the `generate()` entry). Get a key at
82
- [savings.bivelio.com](https://savings.bivelio.com).
81
+ `LicenseError` at the `generate()` entry, carrying a `status` of `none`,
82
+ `invalid`, `expired`, `grace`, `revoked` or `stale`). Your key — and this snippet
83
+ with the key already filled in — is at
84
+ [savings.bivelio.com/dashboard/install](https://savings.bivelio.com/dashboard/install).
83
85
 
84
86
  > **Set `serviceAccountKey` on every long‑running deployment.** Your licence token
85
87
  > expires at the end of the billing period; each renewal mints a **new** token. With
@@ -158,18 +160,20 @@ an action that has not run yet cannot be a saving.
158
160
  > **disabled in this published package** — a valid `licenseKey` is the only way to
159
161
  > run it. They are not a way to use the product for free.
160
162
 
161
- Every component ships with an in‑memory default and is independently swappable
162
- (Redis exact cache, pgvector/Qdrant semantic cache, a real tokenizer, an LLMLingua
163
- sidecar, XGrammar/llguidance backends, …).
163
+ Every component ships with an in‑memory default and is independently swappable:
164
+ plug in your own cache, vector store, tokenizer, compressor or constraint backend
165
+ behind the same interfaces.
164
166
 
165
167
  ## The optimization order (cheapest first)
166
168
 
167
- 1. **Avoid the call entirely** — resolvers, exact & semantic cache, singleflight.
168
- 2. **Avoid auxiliary calls** — retrieval, tools, planners, validators, retries.
169
- 3. **Eliminate unnecessary context** before trying to compress it.
170
- 4. **Compress** the tokens that genuinely must be sent.
169
+ 1. **Avoid the call entirely** when the answer can be produced or reused safely.
170
+ 2. **Avoid auxiliary calls** — the retrievals, tool hops and retries around it.
171
+ 3. **Eliminate unnecessary context** before trying to shrink what remains.
172
+ 4. **Shrink** the tokens that genuinely must be sent.
171
173
  5. **Constrain generation** to emit only the structure and length required.
172
174
 
175
+ The order is the point: work removed at step 1 costs nothing to compress at step 4.
176
+
173
177
  Provider prompt/KV caching is treated as a cost/latency saving — **never** as a
174
178
  reduction of the logical prompt size.
175
179
 
@@ -195,15 +199,12 @@ Request
195
199
  → Canonicalize + Policy (risk & freshness)
196
200
  → Deterministic resolvers ─────────────► answer (0 calls)
197
201
  → Exact cache ─────────────────────────► answer (0 calls)
198
- → Singleflight (coalesce in-flight)
199
- → Safe semantic cache ─────────────► answer (0 calls)
200
- → Execution router (NO / SINGLE / MULTI / TOOL)
201
- → Tool selector (drop irrelevant, gate writes)
202
- → Retrieval: retrieve → dedupe → rerank → adaptive-k
203
- → Context projection (memory deltas)
204
- → Compression (only when net-positive)
205
- → JSON / TOON / CSV selector (tokenizer-aware)
206
- → Output contract + constrained decoding
202
+ → Concurrent duplicates coalesced ─────► answer (0 calls)
203
+ → Near-duplicate reuse ────────────► answer (0 calls)
204
+ → Execution planning (how much work this really needs)
205
+ → Context reduction (only when it pays for itself)
206
+ → Payload encoding chosen by measurement, not by default
207
+ → Output contract enforced
207
208
  → ONE primary LLM call
208
209
  → Structural + semantic validation (+ ≤1 repair)
209
210
  → Local render / transcode / cache write
@@ -212,29 +213,26 @@ Request
212
213
 
213
214
  ## Why formats are chosen, not assumed
214
215
 
215
- A compact encoding can be *larger* than plain JSON on nested/non‑uniform data, so
216
- BV‑SALA counts — with the destination model's tokenizer — before switching:
217
-
218
- ```
219
- tokens(encoded payload) + tokens(instruction to understand it) + quality penalty
220
- ```
221
-
222
- …and only moves away from JSON when a challenger clears both an absolute (`≥64`
223
- tokens) and a relative (`≥8%`) threshold:
216
+ A compact encoding can be *larger* than plain JSON on nested or non‑uniform data,
217
+ so BV‑SALA never assumes one. It counts the candidates with the destination
218
+ model's own tokenizer — including the cost of explaining the encoding to the
219
+ model — and only moves away from JSON when the saving clears a margin worth the
220
+ change.
224
221
 
225
- ```
226
- big uniform table → json 1716, toon 708 → chosen: toon (−58.7%)
227
- small nested object → json 44, toon 116 → chosen: json-compact
228
- ```
222
+ On a wide, uniform table that is a large reduction. On a small nested object it
223
+ leaves JSON alone. Whichever it picks, `report.serialization` tells you what was
224
+ chosen and what each candidate would have cost, so the decision is auditable
225
+ rather than promised.
229
226
 
230
227
  ## What's inside
231
228
 
232
- Canonicalization & safe cache keys · risk/freshness policy · exact + singleflight +
233
- safe semantic cache · deterministic resolvers · Adaptive‑RAG execution router & tool
234
- selector · retrieval dedupe→rerank→adaptive‑k · conversation memory & context
235
- projection · tokenizer‑aware JSON/TOON/CSV selection · JSON‑Schema validation &
236
- constrained decoding · net‑positive compression · a provider‑neutral adapter · and a
237
- trace collector that builds the `SavingsReport`. Every piece is pluggable.
229
+ Safe cache keys and a risk/freshness policy · several independent ways to answer
230
+ without spending a call · execution planning · context reduction that must pay for
231
+ itself · measured payload encoding · output contracts with validation · a
232
+ provider‑neutral adapter · and a trace collector that builds the `SavingsReport`.
233
+
234
+ Every piece is pluggable, and every saving it claims is reported per request with
235
+ the token counts behind it.
238
236
 
239
237
  ## Licensing & privacy
240
238