@bivelio/savings-layer 0.2.2 → 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 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 }>({
@@ -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
- (Redis exact cache, pgvector/Qdrant semantic cache, a real tokenizer, an LLMLingua
163
- sidecar, XGrammar/llguidance backends, …).
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** — 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.
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
- → 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
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/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:
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
- big uniform table → json 1716, toon 708 → chosen: toon (−58.7%)
227
- small nested object → json 44, toon 116 → chosen: json-compact
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
- 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.
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