@usagetap/sdk 0.10.0 → 1.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 +21 -0
- package/README.md +83 -22
- package/dist/adapters/anthropic.cjs +943 -0
- package/dist/adapters/anthropic.cjs.map +1 -0
- package/dist/adapters/anthropic.d.cts +81 -0
- package/dist/adapters/anthropic.d.ts +81 -0
- package/dist/adapters/anthropic.mjs +940 -0
- package/dist/adapters/anthropic.mjs.map +1 -0
- package/dist/adapters/openai.cjs +601 -17
- package/dist/adapters/openai.cjs.map +1 -1
- package/dist/adapters/openai.d.cts +57 -2
- package/dist/adapters/openai.d.ts +57 -2
- package/dist/adapters/openai.mjs +601 -18
- package/dist/adapters/openai.mjs.map +1 -1
- package/dist/adapters/openrouter.cjs.map +1 -1
- package/dist/adapters/openrouter.d.cts +1 -1
- package/dist/adapters/openrouter.d.ts +1 -1
- package/dist/adapters/openrouter.mjs.map +1 -1
- package/dist/anthropic/index.cjs +943 -0
- package/dist/anthropic/index.cjs.map +1 -0
- package/dist/anthropic/index.d.cts +2 -0
- package/dist/anthropic/index.d.ts +2 -0
- package/dist/anthropic/index.mjs +940 -0
- package/dist/anthropic/index.mjs.map +1 -0
- package/dist/{client-DEbk0Q2l.d.cts → client-BA-QlnRq.d.cts} +95 -1
- package/dist/{client-DEbk0Q2l.d.ts → client-BA-QlnRq.d.ts} +95 -1
- package/dist/express/index.cjs +597 -17
- package/dist/express/index.cjs.map +1 -1
- package/dist/express/index.d.cts +1 -1
- package/dist/express/index.d.ts +1 -1
- package/dist/express/index.mjs +597 -17
- package/dist/express/index.mjs.map +1 -1
- package/dist/index.cjs +586 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.mjs +581 -2
- package/dist/index.mjs.map +1 -1
- package/dist/openai/index.cjs +601 -17
- package/dist/openai/index.cjs.map +1 -1
- package/dist/openai/index.d.cts +2 -2
- package/dist/openai/index.d.ts +2 -2
- package/dist/openai/index.mjs +601 -18
- package/dist/openai/index.mjs.map +1 -1
- package/package.json +22 -2
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 UsageTap
|
|
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.
|
package/README.md
CHANGED
|
@@ -8,15 +8,16 @@ Server-only JavaScript/TypeScript client for UsageTap. The SDK helps you instrum
|
|
|
8
8
|
|
|
9
9
|
Optional adapters live behind subpath exports so their peer dependencies stay out of the core bundle:
|
|
10
10
|
|
|
11
|
-
- `@usagetap/sdk/openai` – OpenAI/OpenRouter helpers (`wrapOpenAI`, `streamOpenAIRoute`, etc.)
|
|
12
|
-
- `@usagetap/sdk/
|
|
13
|
-
- `@usagetap/sdk/
|
|
11
|
+
- `@usagetap/sdk/openai` – OpenAI/OpenRouter helpers (`wrapOpenAI`, `streamOpenAIRoute`, etc.)
|
|
12
|
+
- `@usagetap/sdk/anthropic` – Anthropic helper (`wrapAnthropic`)
|
|
13
|
+
- `@usagetap/sdk/express` – Express middleware
|
|
14
|
+
- `@usagetap/sdk/react` – React chat hook
|
|
14
15
|
|
|
15
16
|
Install only the peer dependencies for the adapters you actually use.
|
|
16
17
|
|
|
17
18
|
## Quick start
|
|
18
19
|
|
|
19
|
-
Install the peer dependency for your vendor (e.g. `openai`) and the UsageTap SDK in your server runtime.
|
|
20
|
+
Install the peer dependency for your vendor (e.g. `openai` or `@anthropic-ai/sdk`) and the UsageTap SDK in your server runtime.
|
|
20
21
|
|
|
21
22
|
```bash
|
|
22
23
|
npm install @usagetap/sdk openai
|
|
@@ -110,21 +111,78 @@ const response = await openai.responses.create({
|
|
|
110
111
|
});
|
|
111
112
|
```
|
|
112
113
|
|
|
113
|
-
Prefer a zero-boilerplate integration? Keep scrolling—`wrapOpenAI` applies the same entitlement-aware defaults if you omit `model` from your request.
|
|
114
|
+
Prefer a zero-boilerplate integration? Keep scrolling—`wrapOpenAI` applies the same entitlement-aware defaults if you omit `model` from your request.
|
|
114
115
|
|
|
115
116
|
```ts
|
|
116
117
|
import { wrapOpenAI } from "@usagetap/sdk/openai";
|
|
117
118
|
|
|
118
|
-
const ai = wrapOpenAI(openai, usageTap, {
|
|
119
|
-
defaultContext: {
|
|
120
|
-
customerId: "cust_123",
|
|
121
|
-
feature: "chat.send",
|
|
122
|
-
requested: { standard: true, premium: true, search: true, reasoningLevel: "HIGH" },
|
|
123
|
-
},
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
119
|
+
const ai = wrapOpenAI(openai, usageTap, {
|
|
120
|
+
defaultContext: {
|
|
121
|
+
customerId: "cust_123",
|
|
122
|
+
feature: "chat.send",
|
|
123
|
+
requested: { standard: true, premium: true, search: true, reasoningLevel: "HIGH" },
|
|
124
|
+
},
|
|
125
|
+
promptCompression: {
|
|
126
|
+
provider: "heuristic",
|
|
127
|
+
roles: { user: true, tool: true },
|
|
128
|
+
minTokens: 500,
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### Prompt compression
|
|
134
|
+
|
|
135
|
+
Prompt compression is an explicit step after `call_begin`. `beginCall` only starts the call and returns the `callId`; `promptCompress` compresses locally, records savings metadata against that call, and returns the compressed prompt for your vendor request. Raw prompt content is not sent to UsageTap.
|
|
136
|
+
|
|
137
|
+
```ts
|
|
138
|
+
import { protectPromptText } from "@usagetap/sdk";
|
|
139
|
+
|
|
140
|
+
const begin = await usageTap.beginCall({
|
|
141
|
+
customerId: "cust_123",
|
|
142
|
+
feature: "chat.send",
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
const compressed = await usageTap.promptCompress({
|
|
146
|
+
callId: begin.data.callId,
|
|
147
|
+
input: `Please summarize this long prompt but keep ${protectPromptText("PLAN_ID_PRO_2026")} exact.`,
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
const response = await openai.responses.create({
|
|
151
|
+
model: "gpt5-mini",
|
|
152
|
+
input: compressed.compressedInput as string,
|
|
153
|
+
});
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
The default heuristic is conservative: it normalizes whitespace, preserves fenced code indentation, minifies valid embedded JSON, and converts eligible JSON data blocks to TOON when that is smaller. Pass `provider: "toon"` to force local TOON-style encoding for structured data. Savings include both character counts and approximate token counts using lightweight regex tokenization (`[\p{L}\p{N}]+|[^\s]`), not a model-specific BPE tokenizer. If compression or savings reporting fails, the SDK returns the original input with zero savings so the vendor call can continue.
|
|
157
|
+
|
|
158
|
+
`wrapOpenAI()` and `wrapAnthropic()` can also compress prompts automatically after `call_begin` and before the vendor request. This is opt-in via `promptCompression`; assistant messages are skipped by default so historical assistant turns are not rewritten. Compression telemetry is aggregated once per UsageTap call, and stats are available on `ai.promptCompression.totalTokensSaved`.
|
|
159
|
+
|
|
160
|
+
```ts
|
|
161
|
+
import Anthropic from "@anthropic-ai/sdk";
|
|
162
|
+
import { wrapAnthropic } from "@usagetap/sdk/anthropic";
|
|
163
|
+
|
|
164
|
+
const anthropic = wrapAnthropic(
|
|
165
|
+
new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY! }),
|
|
166
|
+
usageTap,
|
|
167
|
+
{
|
|
168
|
+
defaultContext: { customerId: "cust_123", feature: "chat.send" },
|
|
169
|
+
promptCompression: { roles: { system: true, user: true, tool: true } },
|
|
170
|
+
},
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
await anthropic.messages.create({
|
|
174
|
+
model: "claude-3-5-haiku-latest",
|
|
175
|
+
max_tokens: 512,
|
|
176
|
+
system: "Long system prompt",
|
|
177
|
+
messages: [{ role: "user", content: "Long user prompt" }],
|
|
178
|
+
});
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
When using The Token Company, configure `tokenCompanyApiKey` on `UsageTapClient` and set `provider: "thetokencompany"`. Optional `tokenCompanyModel`, `tokenCompanyAggressiveness`, and `tokenCompanyAppId` are supported at the client, manual `promptCompress`, and wrapper levels. Use `protectPromptText()` for text that must be passed through unchanged by compression-compatible providers.
|
|
182
|
+
|
|
183
|
+
For advanced custom flows, `compressPromptInput(input, options?)` returns compression results without recording telemetry, and `recordPromptCompression({ callId, promptCompression })` records precomputed savings metadata against a call.
|
|
184
|
+
|
|
185
|
+
> **Heads up:** `UsageTapClient` always negotiates the canonical UsageTap media type by sending `Accept: application/vnd.usagetap.v1+json`. Every response now uses the `{ result, data, correlationId }` envelope exclusively and the begin payload includes `data.idempotency.key` (always matching `callId`), per-meter snapshots, and subscription metadata. Set `autoIdempotency: false` (or pass your own `idempotency`) to skip the SDK's auto-generated key and rely on the server's deterministic fallback when retriable semantics are acceptable.
|
|
128
186
|
|
|
129
187
|
### Streaming helpers
|
|
130
188
|
|
|
@@ -414,16 +472,19 @@ Key exports from `@usagetap/sdk`:
|
|
|
414
472
|
- `createCustomer` – idempotently ensure a customer subscription exists before starting a call.
|
|
415
473
|
- `changePlan` – switch a customer to a different usage plan with configurable strategy (immediate reset, prorated, or scheduled).
|
|
416
474
|
- `incrementCustomMeter` – track custom usage metrics beyond standard LLM counters (agent actions, documents, API calls, etc.).
|
|
417
|
-
- `checkUsage` – lightweight method to query current usage status without creating a call session.
|
|
418
|
-
- `
|
|
475
|
+
- `checkUsage` – lightweight method to query current usage status without creating a call session.
|
|
476
|
+
- `promptCompress` / `compressPromptToon` – compress prompt input after `call_begin`, return the compressed payload, and record savings metadata for the call.
|
|
477
|
+
- `protectPromptText` / `protect` – mark exact text spans that compatible compressors should not rewrite.
|
|
478
|
+
- `wrapFetch` – wraps a fetch function to automatically instrument OpenAI API calls (minimal integration).
|
|
419
479
|
- `createIdempotencyKey` – helper for generating UsageTap-compatible idempotency keys.
|
|
420
480
|
- Type definitions for canonical UsageTap request/response payloads.
|
|
421
481
|
|
|
422
|
-
Optional subpaths:
|
|
423
|
-
|
|
424
|
-
- `@usagetap/sdk/openai` – `wrapOpenAI`, `createOpenAIAdapter`, `streamOpenAIRoute`, `toNextResponse`, `pipeToResponse`, and related types.
|
|
425
|
-
- `@usagetap/sdk/
|
|
426
|
-
- `@usagetap/sdk/
|
|
482
|
+
Optional subpaths:
|
|
483
|
+
|
|
484
|
+
- `@usagetap/sdk/openai` – `wrapOpenAI`, `createOpenAIAdapter`, `streamOpenAIRoute`, `toNextResponse`, `pipeToResponse`, and related types.
|
|
485
|
+
- `@usagetap/sdk/anthropic` – `wrapAnthropic` and related prompt compression types.
|
|
486
|
+
- `@usagetap/sdk/express` – `withUsage`, `withUsageMiddleware`, and corresponding Express request types.
|
|
487
|
+
- `@usagetap/sdk/react` – `useChatWithUsage` and supporting types for building chat interfaces.
|
|
427
488
|
|
|
428
489
|
All helpers are designed for server runtimes. Use `UsageTapClient` with `allowBrowser: true` only for sandbox/test scenarios.
|
|
429
490
|
|