@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 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.