@bivelio/savings-layer 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 ADDED
@@ -0,0 +1,69 @@
1
+ BiVelio Inc. — BV-SALA Commercial Software License Agreement
2
+
3
+ Copyright (c) 2026 BiVelio Inc. All Rights Reserved.
4
+
5
+ IMPORTANT — READ BEFORE USE. This Commercial Software License Agreement (the
6
+ "Agreement") governs your use of the "@bivelio/savings-layer" software, also known
7
+ as "BV-SALA" (the "Software"), published by BiVelio Inc. ("BiVelio"). By installing,
8
+ copying, or using the Software, you agree to this Agreement. If you do not agree, do
9
+ not install or use the Software.
10
+
11
+ 1. LICENSE GRANT. Subject to your continuous compliance with this Agreement and to
12
+ an active, paid BiVelio subscription with a valid license key ("License Key"),
13
+ BiVelio grants you a limited, non-exclusive, non-transferable, non-sublicensable,
14
+ revocable license to install and use the Software, as published, solely for your
15
+ own internal business purposes, and only for the number of seats covered by your
16
+ subscription. The Software is licensed, not sold.
17
+
18
+ 2. LICENSE KEY; FAIL-CLOSED OPERATION. The Software requires a valid, active License
19
+ Key and does not operate without one. Expiry, non-payment, revocation, suspension,
20
+ or termination of your subscription immediately ends the license granted in
21
+ Section 1, and the Software will cease to function. You will not circumvent,
22
+ disable, remove, or tamper with the Software's licensing, metering, or revocation
23
+ mechanisms, nor use the Software after your license has ended.
24
+
25
+ 3. RESTRICTIONS. Except as expressly permitted above, you will not: (a) distribute,
26
+ publish, resell, rent, lease, lend, offer as a hosted or managed service, or
27
+ otherwise make the Software available to any third party; (b) sublicense, assign,
28
+ or transfer your rights; (c) use the Software for more seats than you have paid
29
+ for, or share a License Key across more machines or service identities than your
30
+ subscription covers; (d) remove, alter, or obscure any proprietary or copyright
31
+ notice; (e) reverse engineer, decompile, or disassemble the Software, except to
32
+ the limited extent applicable law expressly permits notwithstanding this
33
+ restriction; or (f) use the Software to develop, train, or improve a product or
34
+ service that competes with the Software.
35
+
36
+ 4. OWNERSHIP. The Software is the exclusive property of BiVelio and its licensors and
37
+ embodies valuable proprietary information and trade secrets. BiVelio retains all
38
+ right, title, and interest in and to the Software and all intellectual property
39
+ rights therein. No rights are granted to you except as expressly set out in this
40
+ Agreement, whether by implication, estoppel, or otherwise.
41
+
42
+ 5. YOUR DATA; PRIVACY. Your prompts, completions, and content are processed within
43
+ your own infrastructure and are not transmitted to BiVelio in order to serve a
44
+ request. BiVelio's control plane receives only usage counts and cost metadata, as
45
+ described in the BiVelio documentation and privacy terms.
46
+
47
+ 6. TERM AND TERMINATION. This Agreement remains in effect while your subscription is
48
+ active. BiVelio may terminate this Agreement immediately if you breach it. Upon
49
+ expiry or termination you must cease all use of the Software and destroy your
50
+ copies. Sections 2 through 9 survive termination.
51
+
52
+ 7. DISCLAIMER OF WARRANTY. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
53
+ KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
54
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT.
55
+
56
+ 8. LIMITATION OF LIABILITY. TO THE MAXIMUM EXTENT PERMITTED BY LAW, IN NO EVENT SHALL
57
+ BIVELIO INC. BE LIABLE FOR ANY INDIRECT, INCIDENTAL, SPECIAL, CONSEQUENTIAL, OR
58
+ EXEMPLARY DAMAGES, OR FOR ANY LOSS OF PROFITS, REVENUE, OR DATA, ARISING FROM OR
59
+ RELATED TO THE SOFTWARE OR THIS AGREEMENT. BIVELIO'S TOTAL LIABILITY SHALL NOT
60
+ EXCEED THE FEES YOU PAID FOR THE SOFTWARE IN THE TWELVE MONTHS PRECEDING THE CLAIM.
61
+
62
+ 9. GENERAL. This Agreement is governed by the laws of the State of Delaware, USA,
63
+ without regard to its conflict-of-laws rules. If any provision is held
64
+ unenforceable, the remaining provisions remain in effect. This Agreement, together
65
+ with the applicable BiVelio subscription terms, constitutes the entire agreement
66
+ between you and BiVelio regarding the Software. Unauthorized use, reproduction, or
67
+ distribution of the Software may result in civil and criminal penalties.
68
+
69
+ For licensing and compliance inquiries, contact: BiVelio Inc. — privacy@bivelio.com
package/README.md ADDED
@@ -0,0 +1,152 @@
1
+ # @bivelio/savings-layer — BV‑SALA
2
+
3
+ **A provider‑neutral LLM execution optimizer.** BV‑SALA sits in front of any LLM
4
+ provider and, for every request, picks the **cheapest execution that still meets
5
+ your quality bar** — then hands you a full, auditable `SavingsReport`.
6
+
7
+ It is **not** a prompt compressor. Compression is the *fourth* thing it tries, not
8
+ the first.
9
+
10
+ > **Commercial software — © 2026 BiVelio Inc.** Free to install, **paid to run**:
11
+ > BV‑SALA is **fail‑closed** and executes only with a valid, active BiVelio license
12
+ > key. Verification is **offline** (no phone‑home to answer a request) and **your
13
+ > prompts never leave your infrastructure** — only counts and cost are ever reported.
14
+ > Use is governed by the [LICENSE](./LICENSE) and the BiVelio Terms. Not affiliated
15
+ > with, and not a wrapper around, any third‑party library.
16
+
17
+ ## Install
18
+
19
+ ```bash
20
+ pnpm add @bivelio/savings-layer
21
+ # or: npm i @bivelio/savings-layer · yarn add @bivelio/savings-layer
22
+ ```
23
+
24
+ You also need a **license key**. Try the optimizer **free in your browser** — paste
25
+ your own data and watch the meter — at **[savings.bivelio.com](https://savings.bivelio.com)**,
26
+ then subscribe (**$4 / seat / month** or **$39 / seat / year**, USD) to get your key.
27
+
28
+ ## Quick start
29
+
30
+ ```ts
31
+ import { createSavingsLayer } from "@bivelio/savings-layer";
32
+
33
+ const layer = createSavingsLayer({
34
+ provider: yourProvider, // any OpenAI-compatible / self-hosted provider
35
+ licenseKey: process.env.BIVELIO_LICENSE_KEY, // required — from your savings.bivelio.com dashboard
36
+ licenseServerUrl: "https://savings.bivelio.com", // license check + kill-switch
37
+ });
38
+
39
+ const { data, text, report } = await layer.generate({
40
+ model: "openai/gpt-4o-mini",
41
+ messages: [{ role: "user", content: "What is 2 + 2 * 3?" }],
42
+ response: { contractId: "answer:v1", schema, maxOutputTokens: 220 },
43
+ });
44
+
45
+ console.log(text); // "8" (computed locally — no LLM call)
46
+ console.log(report.calls.avoidedLlm); // → whole calls you never paid for
47
+ console.log(report.cost.netSavings); // → verified savings, per request
48
+ ```
49
+
50
+ The hard requirements are a `provider` and a valid `licenseKey`: BV‑SALA is a paid
51
+ product and **refuses to run unlicensed, expired, or tampered** (it throws
52
+ `LicenseError` at the `generate()` entry). Get a key at
53
+ [savings.bivelio.com](https://savings.bivelio.com).
54
+
55
+ > The `devUnlicensed` flag and `BVSALA_DEV_OPEN=1` are development‑only, honored
56
+ > **only when building from source** (this repo's tests and benchmarks). They are
57
+ > **disabled in this published package** — a valid `licenseKey` is the only way to
58
+ > run it. They are not a way to use the product for free.
59
+
60
+ Every component ships with an in‑memory default and is independently swappable
61
+ (Redis exact cache, pgvector/Qdrant semantic cache, a real tokenizer, an LLMLingua
62
+ sidecar, XGrammar/llguidance backends, …).
63
+
64
+ ## The optimization order (cheapest first)
65
+
66
+ 1. **Avoid the call entirely** — resolvers, exact & semantic cache, singleflight.
67
+ 2. **Avoid auxiliary calls** — retrieval, tools, planners, validators, retries.
68
+ 3. **Eliminate unnecessary context** before trying to compress it.
69
+ 4. **Compress** the tokens that genuinely must be sent.
70
+ 5. **Constrain generation** to emit only the structure and length required.
71
+
72
+ Provider prompt/KV caching is treated as a cost/latency saving — **never** as a
73
+ reduction of the logical prompt size.
74
+
75
+ ## Four separate ledgers (never summed)
76
+
77
+ Mixing these produces misleading metrics, so BV‑SALA keeps them apart:
78
+
79
+ | Ledger | Meaning |
80
+ | --- | --- |
81
+ | `avoided_calls` | LLM / retrieval / tool calls that never executed |
82
+ | `eliminated_tokens` | information dropped because it was not needed |
83
+ | `compressed_tokens` | information kept, but encoded with fewer tokens |
84
+ | `provider_cached_tokens` | tokens still in the prompt, billed/processed more cheaply |
85
+
86
+ A cache hit removes a whole call. A re‑encode makes a payload smaller. Compression
87
+ drops redundant words. A prefix cache makes tokens cheaper without removing them.
88
+ Four different things — summing them would lie to you, so the report never does.
89
+
90
+ ## The decision cascade
91
+
92
+ ```
93
+ Request
94
+ → Canonicalize + Policy (risk & freshness)
95
+ → Deterministic resolvers ─────────────► answer (0 calls)
96
+ → Exact cache ─────────────────────────► answer (0 calls)
97
+ → Singleflight (coalesce in-flight)
98
+ → Safe semantic cache ─────────────► answer (0 calls)
99
+ → Execution router (NO / SINGLE / MULTI / TOOL)
100
+ → Tool selector (drop irrelevant, gate writes)
101
+ → Retrieval: retrieve → dedupe → rerank → adaptive-k
102
+ → Context projection (memory deltas)
103
+ → Compression (only when net-positive)
104
+ → JSON / TOON / CSV selector (tokenizer-aware)
105
+ → Output contract + constrained decoding
106
+ → ONE primary LLM call
107
+ → Structural + semantic validation (+ ≤1 repair)
108
+ → Local render / transcode / cache write
109
+ → SavingsReport + traces
110
+ ```
111
+
112
+ ## Why formats are chosen, not assumed
113
+
114
+ A compact encoding can be *larger* than plain JSON on nested/non‑uniform data, so
115
+ BV‑SALA counts — with the destination model's tokenizer — before switching:
116
+
117
+ ```
118
+ tokens(encoded payload) + tokens(instruction to understand it) + quality penalty
119
+ ```
120
+
121
+ …and only moves away from JSON when a challenger clears both an absolute (`≥64`
122
+ tokens) and a relative (`≥8%`) threshold:
123
+
124
+ ```
125
+ big uniform table → json 1716, toon 708 → chosen: toon (−58.7%)
126
+ small nested object → json 44, toon 116 → chosen: json-compact
127
+ ```
128
+
129
+ ## What's inside
130
+
131
+ Canonicalization & safe cache keys · risk/freshness policy · exact + singleflight +
132
+ safe semantic cache · deterministic resolvers · Adaptive‑RAG execution router & tool
133
+ selector · retrieval dedupe→rerank→adaptive‑k · conversation memory & context
134
+ projection · tokenizer‑aware JSON/TOON/CSV selection · JSON‑Schema validation &
135
+ constrained decoding · net‑positive compression · a provider‑neutral adapter · and a
136
+ trace collector that builds the `SavingsReport`. Every piece is pluggable.
137
+
138
+ ## Licensing & privacy
139
+
140
+ BV‑SALA is commercial, per‑seat software (**$4 / seat / month** or **$39 / seat /
141
+ year**, USD). The license is a signed token your SDK verifies **offline**; the
142
+ managed control plane (semantic cache, learned thresholds, dashboards) and the
143
+ revocation kill‑switch are served from BiVelio. Your LLM traffic goes **straight to
144
+ your provider** — BiVelio sees counts and cost, never prompts or completions.
145
+
146
+ Get a key and read the terms at **[savings.bivelio.com](https://savings.bivelio.com)**.
147
+ Onboarding & general questions: **support@bivelio.com** · licensing & compliance:
148
+ **privacy@bivelio.com** · technical: **team@bivelio.com**.
149
+
150
+ ---
151
+
152
+ © 2026 BiVelio Inc. All rights reserved. BV‑SALA is a trademark of BiVelio Inc.