Repository navigation
v0.1.0
First public release.
Added
- Client wrapper:
optimizer.wrap()forOrchestrationClient(chatCompletion,stream)
andAzureOpenAiChatClient(run,stream) from@sap-ai-sdk/*2.x. It keeps the client's
API and never modifies the caller's client or request objects. - Explicit calls:
optimizer.chat()for orchestration or foundation models. - CAP plugin: configuration from
cds.requires.tokenOptimizer, exporter flush on shutdown,
and tokenizer warm-up after the server starts. - Optimization pipeline:
normalize,dedupe,history(pluggable strategy, summarizer
hook),tools(pluggable selector, pinned tools, description shortening) andoutputCap.
Each step can be switched off and reports its estimated savings. Per-use-case configuration. - Exact-match response cache: in-memory LRU with TTL, a pluggable
CacheStore, and tenant,
user or global scope. Only temperature ≤ 0.2 by default. - Modes:
active,shadow(measures savings, sends the original request) andoff. - Prompt-caching advisory: a warning, event field, stat and metric when the system prompt or
tools change between calls of the same use case and model, including whether the optimizer
caused it. - Metrics: one event per call with real and estimated tokens, savings per step, cost
estimate, latency, retries, cache hit, hashed user, tenant and service fromcds.context, and
a classified error. Prompt and completion text only withcaptureContent: true. - Failure classifier:
RATE_LIMIT,CONTENT_FILTER,CONTEXT_LENGTH,TIMEOUT,AUTH,
DEPLOYMENT_NOT_FOUND,SERVER,NETWORK,UNKNOWN. - Opt-in retries with backoff and
Retry-After, and a default request timeout. - Exporters:
consolehttp(URL or BTP destination, batching, retries, bounded queue)otel(OpenTelemetry GenAI metrics and spans through@cap-js/telemetry/ SAP Cloud ALM)db/hana(TokenUsageLogentity, multitenant, retention)
- Admin service: read-only
TokenOptimizerServiceat/odata/v4/token-optimizerwith a
configurable role. - Testing and benchmark:
- token-savings benchmark (
npm run bench) with 12 agent conversations, and--live
off/active answer comparison - mock AI Core server for tests and local development
- sample CAP app
sample-cap-agent
- token-savings benchmark (
- Instrumenting existing code:
autoInstrumenthooks the@sap-ai-sdkclient classes so
existing apps need no code changes.optimizer.withUseCase()labels the calls made inside a
function. - LangChain:
optimizer.wrapLangChain()for@sap-ai-sdk/langchainchat models. - Compatibility:
- tested on Node 22.12+ and 24,
@sap/cds9 and 10,@sap-ai-sdk2.8 to 2.16; - CI matrix (
.github/workflows/ci.yml); - smoke test with
node:testfor Node 22.
- tested on Node 22.12+ and 24,
- Integration guide: "Add to an existing CAP app in 5 minutes" (
docs/integration-guide.md). - TypeScript definitions (
index.d.ts).
Known limitations
stream()calls are passed through without optimization or token usage.autoInstrumentonly hooks the SDK copy resolved from the app root, and is process-wide.- The Jest suite needs Node ≥ 24.9; the runtime supports Node ≥ 22.12.
- Clients built from orchestration fallback lists, JSON strings or stored config references
keep their tools and model parameters (thetoolsandoutputCapsteps are skipped). - Token estimates use tiktoken for OpenAI model families and characters ÷ 4 for others.
- The
httpexporter supports Internet destinations only. - Not yet verified against SAP Cloud ALM; live AI Core tests are opt-in.