@viveka10/cap-ai-token-optimizer 0.0.0-stage → 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/CHANGELOG.md +71 -0
- package/LICENSE +21 -0
- package/README.md +746 -2
- package/SECURITY.md +39 -0
- package/cds-plugin.js +50 -0
- package/db/token-usage-log.cds +44 -0
- package/docs/integration-guide.md +140 -0
- package/index.d.ts +291 -0
- package/index.js +36 -0
- package/lib/cache/index.js +67 -0
- package/lib/client/adapters.js +263 -0
- package/lib/client/instrument.js +124 -0
- package/lib/config.js +128 -0
- package/lib/exporters/console.js +18 -0
- package/lib/exporters/db.js +169 -0
- package/lib/exporters/http.js +143 -0
- package/lib/exporters/index.js +42 -0
- package/lib/exporters/otel.js +126 -0
- package/lib/metrics/classifier.js +175 -0
- package/lib/metrics/collector.js +206 -0
- package/lib/metrics/events.js +124 -0
- package/lib/optimizer.js +467 -0
- package/lib/pipeline/index.js +90 -0
- package/lib/pipeline/messages.js +62 -0
- package/lib/pipeline/steps/dedupe.js +103 -0
- package/lib/pipeline/steps/history.js +63 -0
- package/lib/pipeline/steps/normalize.js +88 -0
- package/lib/pipeline/steps/outputCap.js +28 -0
- package/lib/pipeline/steps/tools.js +157 -0
- package/lib/pipeline/text.js +166 -0
- package/lib/tokens/estimator.js +163 -0
- package/lib/util/cds.js +13 -0
- package/lib/util/hash.js +40 -0
- package/lib/util/log.js +37 -0
- package/lib/util/peer.js +55 -0
- package/package.json +131 -4
- package/srv/admin-service.cds +83 -0
- package/srv/admin-service.js +41 -0
- package/srv/admin-usage-log.cds +8 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project are documented in this file.
|
|
4
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
5
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
6
|
+
|
|
7
|
+
## [Unreleased]
|
|
8
|
+
|
|
9
|
+
## [0.1.0] - 2026-10-10
|
|
10
|
+
|
|
11
|
+
First public release.
|
|
12
|
+
|
|
13
|
+
### Added
|
|
14
|
+
|
|
15
|
+
- **Client wrapper:** `optimizer.wrap()` for `OrchestrationClient` (`chatCompletion`, `stream`)
|
|
16
|
+
and `AzureOpenAiChatClient` (`run`, `stream`) from `@sap-ai-sdk/*` 2.x. It keeps the client's
|
|
17
|
+
API and never modifies the caller's client or request objects.
|
|
18
|
+
- **Explicit calls:** `optimizer.chat()` for orchestration or foundation models.
|
|
19
|
+
- **CAP plugin:** configuration from `cds.requires.tokenOptimizer`, exporter flush on shutdown,
|
|
20
|
+
and tokenizer warm-up after the server starts.
|
|
21
|
+
- **Optimization pipeline:** `normalize`, `dedupe`, `history` (pluggable strategy, summarizer
|
|
22
|
+
hook), `tools` (pluggable selector, pinned tools, description shortening) and `outputCap`.
|
|
23
|
+
Each step can be switched off and reports its estimated savings. Per-use-case configuration.
|
|
24
|
+
- **Exact-match response cache:** in-memory LRU with TTL, a pluggable `CacheStore`, and tenant,
|
|
25
|
+
user or global scope. Only temperature ≤ 0.2 by default.
|
|
26
|
+
- **Modes:** `active`, `shadow` (measures savings, sends the original request) and `off`.
|
|
27
|
+
- **Prompt-caching advisory:** a warning, event field, stat and metric when the system prompt or
|
|
28
|
+
tools change between calls of the same use case and model, including whether the optimizer
|
|
29
|
+
caused it.
|
|
30
|
+
- **Metrics:** one event per call with real and estimated tokens, savings per step, cost
|
|
31
|
+
estimate, latency, retries, cache hit, hashed user, tenant and service from `cds.context`, and
|
|
32
|
+
a classified error. Prompt and completion text only with `captureContent: true`.
|
|
33
|
+
- **Failure classifier:** `RATE_LIMIT`, `CONTENT_FILTER`, `CONTEXT_LENGTH`, `TIMEOUT`, `AUTH`,
|
|
34
|
+
`DEPLOYMENT_NOT_FOUND`, `SERVER`, `NETWORK`, `UNKNOWN`.
|
|
35
|
+
- **Opt-in retries** with backoff and `Retry-After`, and a default request timeout.
|
|
36
|
+
- **Exporters:**
|
|
37
|
+
- `console`
|
|
38
|
+
- `http` (URL or BTP destination, batching, retries, bounded queue)
|
|
39
|
+
- `otel` (OpenTelemetry GenAI metrics and spans through `@cap-js/telemetry` / SAP Cloud ALM)
|
|
40
|
+
- `db` / `hana` (`TokenUsageLog` entity, multitenant, retention)
|
|
41
|
+
- **Admin service:** read-only `TokenOptimizerService` at `/odata/v4/token-optimizer` with a
|
|
42
|
+
configurable role.
|
|
43
|
+
- **Testing and benchmark:**
|
|
44
|
+
- token-savings benchmark (`npm run bench`) with 12 agent conversations, and `--live`
|
|
45
|
+
off/active answer comparison
|
|
46
|
+
- mock AI Core server for tests and local development
|
|
47
|
+
- sample CAP app `sample-cap-agent`
|
|
48
|
+
- **Instrumenting existing code:** `autoInstrument` hooks the `@sap-ai-sdk` client classes so
|
|
49
|
+
existing apps need no code changes. `optimizer.withUseCase()` labels the calls made inside a
|
|
50
|
+
function.
|
|
51
|
+
- **LangChain:** `optimizer.wrapLangChain()` for `@sap-ai-sdk/langchain` chat models.
|
|
52
|
+
- **Compatibility:**
|
|
53
|
+
- tested on Node 22.12+ and 24, `@sap/cds` 9 and 10, `@sap-ai-sdk` 2.8 to 2.16;
|
|
54
|
+
- CI matrix (`.github/workflows/ci.yml`);
|
|
55
|
+
- smoke test with `node:test` for Node 22.
|
|
56
|
+
- **Integration guide:** "Add to an existing CAP app in 5 minutes" (`docs/integration-guide.md`).
|
|
57
|
+
- **TypeScript definitions** (`index.d.ts`).
|
|
58
|
+
|
|
59
|
+
### Known limitations
|
|
60
|
+
|
|
61
|
+
- `stream()` calls are passed through without optimization or token usage.
|
|
62
|
+
- `autoInstrument` only hooks the SDK copy resolved from the app root, and is process-wide.
|
|
63
|
+
- The Jest suite needs Node ≥ 24.9; the runtime supports Node ≥ 22.12.
|
|
64
|
+
- Clients built from orchestration fallback lists, JSON strings or stored config references
|
|
65
|
+
keep their tools and model parameters (the `tools` and `outputCap` steps are skipped).
|
|
66
|
+
- Token estimates use tiktoken for OpenAI model families and characters ÷ 4 for others.
|
|
67
|
+
- The `http` exporter supports Internet destinations only.
|
|
68
|
+
- Not yet verified against SAP Cloud ALM; live AI Core tests are opt-in.
|
|
69
|
+
|
|
70
|
+
[Unreleased]: https://github.com/viveka10/npm_token_optimizer/compare/v0.1.0...HEAD
|
|
71
|
+
[0.1.0]: https://github.com/viveka10/npm_token_optimizer/releases/tag/v0.1.0
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Vivekanandhan Srinivasan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|