@usagetap/sdk 1.1.0 → 1.3.1
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/README.md +200 -201
- package/dist/adapters/anthropic.cjs +1676 -6
- package/dist/adapters/anthropic.cjs.map +1 -1
- package/dist/adapters/anthropic.d.cts +39 -2
- package/dist/adapters/anthropic.d.ts +39 -2
- package/dist/adapters/anthropic.mjs +1675 -7
- package/dist/adapters/anthropic.mjs.map +1 -1
- package/dist/adapters/openai.cjs +1726 -6
- package/dist/adapters/openai.cjs.map +1 -1
- package/dist/adapters/openai.d.cts +46 -2
- package/dist/adapters/openai.d.ts +46 -2
- package/dist/adapters/openai.mjs +1725 -7
- 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 +1676 -6
- package/dist/anthropic/index.cjs.map +1 -1
- package/dist/anthropic/index.d.cts +2 -2
- package/dist/anthropic/index.d.ts +2 -2
- package/dist/anthropic/index.mjs +1675 -7
- package/dist/anthropic/index.mjs.map +1 -1
- package/dist/{client-BA-QlnRq.d.cts → client-BD8O2J8Z.d.cts} +106 -11
- package/dist/{client-BA-QlnRq.d.ts → client-BD8O2J8Z.d.ts} +106 -11
- package/dist/express/index.cjs +86 -1
- 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 +86 -1
- package/dist/express/index.mjs.map +1 -1
- package/dist/index.cjs +263 -39
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.mjs +262 -40
- package/dist/index.mjs.map +1 -1
- package/dist/openai/index.cjs +1726 -6
- 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 +1725 -7
- package/dist/openai/index.mjs.map +1 -1
- package/dist/openrouter/index.cjs +3024 -0
- package/dist/openrouter/index.cjs.map +1 -0
- package/dist/openrouter/index.d.cts +4 -0
- package/dist/openrouter/index.d.ts +4 -0
- package/dist/openrouter/index.mjs +3019 -0
- package/dist/openrouter/index.mjs.map +1 -0
- package/package.json +102 -44
package/README.md
CHANGED
|
@@ -8,181 +8,180 @@ 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/anthropic` – Anthropic helper (`wrapAnthropic`)
|
|
13
|
-
- `@usagetap/sdk/
|
|
14
|
-
- `@usagetap/sdk/
|
|
11
|
+
- `@usagetap/sdk/openai` – OpenAI/OpenRouter helpers (`wrapOpenAI`, `streamOpenAIRoute`, etc.)
|
|
12
|
+
- `@usagetap/sdk/anthropic` – Anthropic helper (`wrapAnthropic`)
|
|
13
|
+
- `@usagetap/sdk/openrouter` – discoverable OpenRouter aliases for the OpenAI-compatible wrappers
|
|
14
|
+
- `@usagetap/sdk/express` – Express middleware
|
|
15
|
+
- `@usagetap/sdk/react` – React chat hook
|
|
15
16
|
|
|
16
17
|
Install only the peer dependencies for the adapters you actually use.
|
|
17
18
|
|
|
18
19
|
## Quick start
|
|
19
20
|
|
|
20
|
-
Install the peer dependency for your vendor (e.g. `openai` or `@anthropic-ai/sdk`) and the UsageTap SDK in your server runtime.
|
|
21
|
+
Install the peer dependency for your vendor (e.g. `openai` or `@anthropic-ai/sdk`) and the UsageTap SDK in your server runtime.
|
|
21
22
|
|
|
22
23
|
```bash
|
|
23
24
|
npm install @usagetap/sdk openai
|
|
24
25
|
```
|
|
25
26
|
|
|
26
|
-
|
|
27
|
+
Wrap the provider client you already use. `USAGETAP_API_KEY` and the production
|
|
28
|
+
UsageTap URL are read automatically:
|
|
27
29
|
|
|
28
30
|
```ts
|
|
29
31
|
import OpenAI from "openai";
|
|
30
|
-
import {
|
|
32
|
+
import { withMetering } from "@usagetap/sdk/openai";
|
|
31
33
|
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
34
|
+
const openai = withMetering(new OpenAI(), "cust_123");
|
|
35
|
+
const completion = await openai.responses.create({
|
|
36
|
+
model: "gpt-5.5-mini",
|
|
37
|
+
input: "Draft a welcome email for our Pro plan",
|
|
35
38
|
});
|
|
36
39
|
|
|
37
|
-
|
|
40
|
+
console.log(completion.output_text);
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Only the customer ID is required for metering. Pass an object instead of the
|
|
44
|
+
string when you want optional feature, tag, entitlement, or prompt-compression
|
|
45
|
+
settings. Existing `wrapOpenAI`, `wrapAnthropic`, and manual `withUsage` flows
|
|
46
|
+
remain supported for advanced control.
|
|
38
47
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
48
|
+
For standalone compression, wrap the client without changing any downstream
|
|
49
|
+
calls:
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
import OpenAI from "openai";
|
|
53
|
+
import { withCompression } from "@usagetap/sdk/openai";
|
|
54
|
+
|
|
55
|
+
const openai = withCompression(new OpenAI(), {
|
|
56
|
+
// Defaults to 1,000. Use 0 to always attempt compression.
|
|
57
|
+
minContextTokens: 2_000,
|
|
58
|
+
});
|
|
59
|
+
const completion = await openai.responses.create({
|
|
60
|
+
model: "gpt-5.5-mini",
|
|
61
|
+
input: longPrompt,
|
|
43
62
|
});
|
|
63
|
+
```
|
|
44
64
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
? "high"
|
|
55
|
-
: allowed.reasoningLevel === "MEDIUM"
|
|
56
|
-
? "medium"
|
|
57
|
-
: allowed.reasoningLevel === "LOW"
|
|
58
|
-
? "low"
|
|
59
|
-
: undefined;
|
|
60
|
-
|
|
61
|
-
return {
|
|
62
|
-
model,
|
|
63
|
-
reasoning: reasoningEffort ? { effort: reasoningEffort } : undefined,
|
|
64
|
-
tools: allowed.search ? [{ type: "web_search" as const }] : undefined,
|
|
65
|
-
};
|
|
66
|
-
}
|
|
65
|
+
The same `withMetering` and `withCompression` APIs are exported from
|
|
66
|
+
`@usagetap/sdk/anthropic` and `@usagetap/sdk/openrouter`. Remove the wrapper or
|
|
67
|
+
call `.unwrap()` to recover the original provider client.
|
|
68
|
+
|
|
69
|
+
`withCompression` uses a fast token estimate over the combined request context
|
|
70
|
+
and skips the compression step below 1,000 estimated tokens by default. This
|
|
71
|
+
avoids an extra network round trip when likely savings are small. Override the
|
|
72
|
+
cutoff with `minContextTokens`; set it to `0` to always attempt compression.
|
|
73
|
+
The separate `minTokens` option remains a per-text-segment cutoff.
|
|
67
74
|
|
|
68
|
-
|
|
69
|
-
|
|
75
|
+
Wrappers compose. Put metering outside compression so the metered operation
|
|
76
|
+
includes compression and the provider call:
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
const openai = withMetering(
|
|
80
|
+
withCompression(new OpenAI()),
|
|
81
|
+
"cust_123",
|
|
82
|
+
);
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Each `.unwrap()` removes one layer. Do not also set `promptCompression: true` on
|
|
86
|
+
`withMetering` when using a separate `withCompression` layer.
|
|
87
|
+
|
|
88
|
+
For advanced entitlement control, `wrapOpenAI` exposes the full UsageTap context
|
|
89
|
+
and applies entitlement-aware defaults when you omit `model`.
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
import { wrapOpenAI } from "@usagetap/sdk/openai";
|
|
93
|
+
|
|
94
|
+
const ai = wrapOpenAI(openai, usageTap, {
|
|
95
|
+
defaultContext: {
|
|
70
96
|
customerId: "cust_123",
|
|
71
97
|
feature: "chat.send",
|
|
72
98
|
requested: { standard: true, premium: true, search: true, reasoningLevel: "HIGH" },
|
|
73
99
|
},
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
model,
|
|
79
|
-
input: "Draft a welcome email for our Pro plan",
|
|
80
|
-
reasoning,
|
|
81
|
-
tools,
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
setUsage({
|
|
85
|
-
modelUsed: model,
|
|
86
|
-
inputTokens: response.usage?.input_tokens ?? response.usage?.prompt_tokens ?? 0,
|
|
87
|
-
cachedInputTokens:
|
|
88
|
-
response.usage?.prompt_tokens_details?.cached_tokens ??
|
|
89
|
-
response.usage?.cache_read_input_tokens ??
|
|
90
|
-
response.usage?.cached_tokens ??
|
|
91
|
-
0,
|
|
92
|
-
responseTokens: response.usage?.output_tokens ?? response.usage?.completion_tokens ?? 0,
|
|
93
|
-
reasoningTokens: reasoning ? response.usage?.reasoning_tokens ?? 0 : 0,
|
|
94
|
-
searches: tools?.length ? response.usage?.web_search_queries ?? 0 : 0,
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
return response;
|
|
100
|
+
promptCompression: {
|
|
101
|
+
provider: "heuristic",
|
|
102
|
+
roles: { user: true, tool: true },
|
|
103
|
+
minTokens: 500,
|
|
98
104
|
},
|
|
99
|
-
);
|
|
100
|
-
|
|
101
|
-
console.log(completion.output_text);
|
|
105
|
+
});
|
|
102
106
|
```
|
|
103
107
|
|
|
104
|
-
|
|
108
|
+
### Prompt compression
|
|
109
|
+
|
|
110
|
+
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.
|
|
105
111
|
|
|
106
112
|
```ts
|
|
113
|
+
import { protectPromptText } from "@usagetap/sdk";
|
|
114
|
+
|
|
115
|
+
const begin = await usageTap.beginCall({
|
|
116
|
+
customerId: "cust_123",
|
|
117
|
+
feature: "chat.send",
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
const compressed = await usageTap.promptCompress({
|
|
121
|
+
callId: begin.data.callId,
|
|
122
|
+
input: `Please summarize this long prompt but keep ${protectPromptText("PLAN_ID_PRO_2026")} exact.`,
|
|
123
|
+
});
|
|
124
|
+
|
|
107
125
|
const response = await openai.responses.create({
|
|
108
|
-
model: "gpt5",
|
|
109
|
-
|
|
110
|
-
input: "What was a positive news story from today?",
|
|
126
|
+
model: "gpt5-mini",
|
|
127
|
+
input: compressed.compressedInput as string,
|
|
111
128
|
});
|
|
112
129
|
```
|
|
113
130
|
|
|
114
|
-
|
|
131
|
+
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.
|
|
132
|
+
|
|
133
|
+
`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`.
|
|
115
134
|
|
|
116
135
|
```ts
|
|
117
|
-
import
|
|
136
|
+
import Anthropic from "@anthropic-ai/sdk";
|
|
137
|
+
import { wrapAnthropic } from "@usagetap/sdk/anthropic";
|
|
118
138
|
|
|
119
|
-
const
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
`
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
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.
|
|
139
|
+
const anthropic = wrapAnthropic(
|
|
140
|
+
new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY! }),
|
|
141
|
+
usageTap,
|
|
142
|
+
{
|
|
143
|
+
defaultContext: { customerId: "cust_123", feature: "chat.send" },
|
|
144
|
+
promptCompression: { roles: { system: true, user: true, tool: true } },
|
|
145
|
+
},
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
await anthropic.messages.create({
|
|
149
|
+
model: "claude-3-5-haiku-latest",
|
|
150
|
+
max_tokens: 512,
|
|
151
|
+
system: "Long system prompt",
|
|
152
|
+
messages: [{ role: "user", content: "Long user prompt" }],
|
|
153
|
+
});
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Use `provider: "usagetap"` to compress with UsageTap's hosted endpoints. Manual `promptCompress()` and `compressPromptInput()` use the single-text The Token Company-compatible endpoint at `https://compress.usagetap.com/v1/compress`, where `aggressiveness` is a single number from `0.0` to `1.0`. `wrapOpenAI()` and `wrapAnthropic()` use the message/request endpoint at `https://compress.usagetap.com/v1/messages/compress`, where `aggressiveness` may be a per-role object:
|
|
157
|
+
|
|
158
|
+
```ts
|
|
159
|
+
const result = await usageTap.promptCompress({
|
|
160
|
+
callId: begin.data.callId,
|
|
161
|
+
text: "Your text here",
|
|
162
|
+
provider: "usagetap",
|
|
163
|
+
model: "bear-2",
|
|
164
|
+
aggressiveness: 0.5,
|
|
165
|
+
});
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
```ts
|
|
169
|
+
const ai = wrapOpenAI(openai, usageTap, {
|
|
170
|
+
defaultContext: { customerId: "cust_123", feature: "chat.send" },
|
|
171
|
+
promptCompression: {
|
|
172
|
+
provider: "usagetap",
|
|
173
|
+
aggressiveness: { user: 0.5, system: 0.5, tool: 0.5 },
|
|
174
|
+
},
|
|
175
|
+
});
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
`UsageTapClient` sends your UsageTap API key by default. Override `usageTapCompressionEndpoint` for single-text compression or `usageTapCompressionMessagesEndpoint` for wrapper message compression.
|
|
179
|
+
|
|
180
|
+
When using The Token Company, configure `tokenCompanyApiKey` on `UsageTapClient` and set `provider: "thetokencompany"`. Optional `tokenCompanyModel`, `aggressiveness`, 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.
|
|
181
|
+
|
|
182
|
+
For advanced custom flows, `compressPromptInput(input, options?)` returns compression results without recording telemetry, and `recordPromptCompression({ callId, promptCompression })` records precomputed savings metadata against a call.
|
|
183
|
+
|
|
184
|
+
> **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.
|
|
186
185
|
|
|
187
186
|
### Streaming helpers
|
|
188
187
|
|
|
@@ -472,19 +471,19 @@ Key exports from `@usagetap/sdk`:
|
|
|
472
471
|
- `createCustomer` – idempotently ensure a customer subscription exists before starting a call.
|
|
473
472
|
- `changePlan` – switch a customer to a different usage plan with configurable strategy (immediate reset, prorated, or scheduled).
|
|
474
473
|
- `incrementCustomMeter` – track custom usage metrics beyond standard LLM counters (agent actions, documents, API calls, etc.).
|
|
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).
|
|
474
|
+
- `checkUsage` – lightweight method to query current usage status without creating a call session.
|
|
475
|
+
- `promptCompress` / `compressPromptToon` – compress prompt input after `call_begin`, return the compressed payload, and record savings metadata for the call.
|
|
476
|
+
- `protectPromptText` / `protect` – mark exact text spans that compatible compressors should not rewrite.
|
|
477
|
+
- `wrapFetch` – wraps a fetch function to automatically instrument OpenAI API calls (minimal integration).
|
|
479
478
|
- `createIdempotencyKey` – helper for generating UsageTap-compatible idempotency keys.
|
|
480
479
|
- Type definitions for canonical UsageTap request/response payloads.
|
|
481
480
|
|
|
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.
|
|
481
|
+
Optional subpaths:
|
|
482
|
+
|
|
483
|
+
- `@usagetap/sdk/openai` – `wrapOpenAI`, `createOpenAIAdapter`, `streamOpenAIRoute`, `toNextResponse`, `pipeToResponse`, and related types.
|
|
484
|
+
- `@usagetap/sdk/anthropic` – `wrapAnthropic` and related prompt compression types.
|
|
485
|
+
- `@usagetap/sdk/express` – `withUsage`, `withUsageMiddleware`, and corresponding Express request types.
|
|
486
|
+
- `@usagetap/sdk/react` – `useChatWithUsage` and supporting types for building chat interfaces.
|
|
488
487
|
|
|
489
488
|
All helpers are designed for server runtimes. Use `UsageTapClient` with `allowBrowser: true` only for sandbox/test scenarios.
|
|
490
489
|
|
|
@@ -645,18 +644,18 @@ UsageTap responds exclusively with the canonical `{ result, data, correlationId
|
|
|
645
644
|
"search": true,
|
|
646
645
|
"reasoningLevel": "MEDIUM"
|
|
647
646
|
},
|
|
648
|
-
"entitlementHints": {
|
|
649
|
-
"suggestedModelTier": "standard",
|
|
650
|
-
"reasoningLevel": "MEDIUM",
|
|
651
|
-
"policy": "DOWNGRADE",
|
|
652
|
-
"downgrade": {
|
|
653
|
-
"reason": "PREMIUM_QUOTA_EXHAUSTED",
|
|
654
|
-
"fallbackTier": "standard"
|
|
655
|
-
}
|
|
656
|
-
},
|
|
657
|
-
"meters": {
|
|
658
|
-
"standardCalls": {
|
|
659
|
-
"remaining": 12,
|
|
647
|
+
"entitlementHints": {
|
|
648
|
+
"suggestedModelTier": "standard",
|
|
649
|
+
"reasoningLevel": "MEDIUM",
|
|
650
|
+
"policy": "DOWNGRADE",
|
|
651
|
+
"downgrade": {
|
|
652
|
+
"reason": "PREMIUM_QUOTA_EXHAUSTED",
|
|
653
|
+
"fallbackTier": "standard"
|
|
654
|
+
}
|
|
655
|
+
},
|
|
656
|
+
"meters": {
|
|
657
|
+
"standardCalls": {
|
|
658
|
+
"remaining": 12,
|
|
660
659
|
"limit": 20,
|
|
661
660
|
"used": 8,
|
|
662
661
|
"unlimited": false,
|
|
@@ -707,8 +706,8 @@ UsageTap responds exclusively with the canonical `{ result, data, correlationId
|
|
|
707
706
|
|
|
708
707
|
`UsageTapClient` exposes the normalized structure via `UsageTapSuccessResponse<BeginCallResponseBody>`. In addition to the flattened `allowed` map, the begin response now ships richer metadata:
|
|
709
708
|
|
|
710
|
-
- `entitlementHints` summarises the recommended model tier and downgrade rationale based on the active policy.
|
|
711
|
-
- `meters` is a per-counter snapshot including remaining quotas, total limits, usage to date, and convenience ratios. `remainingRatios` mirrors the same information in a compact map for quick lookups.
|
|
709
|
+
- `entitlementHints` summarises the recommended model tier and downgrade rationale based on the active policy.
|
|
710
|
+
- `meters` is a per-counter snapshot including remaining quotas, total limits, usage to date, and convenience ratios. `remainingRatios` mirrors the same information in a compact map for quick lookups.
|
|
712
711
|
- `subscription` contains the active plan identity, versioning, and upcoming replenishment timestamps so you can render customer-facing UI without querying Dynamo yourself.
|
|
713
712
|
- `models` surfaces per-organization vendor hints (e.g. standard vs. premium model shortlists).
|
|
714
713
|
- `idempotency` reveals the actual key that was persisted (`callId` mirrors this value). When you omit `idempotency` in the request, the backend derives a deterministic hash from organization, customer, feature, and requested entitlements.
|
|
@@ -733,48 +732,48 @@ UsageTap responds exclusively with the canonical `{ result, data, correlationId
|
|
|
733
732
|
"responseTokens": 288,
|
|
734
733
|
"reasoningTokens": 0
|
|
735
734
|
},
|
|
736
|
-
"metered": {
|
|
737
|
-
"tokens": 768,
|
|
738
|
-
"calls": 1,
|
|
739
|
-
"searches": 1
|
|
740
|
-
},
|
|
741
|
-
"spendVelocity": {
|
|
742
|
-
"currency": "USD",
|
|
743
|
-
"source": "usage_aggregate",
|
|
744
|
-
"generatedAt": "2025-10-04T18:21:52.103Z",
|
|
745
|
-
"customerId": "cust_123",
|
|
746
|
-
"currentCallCostUsd": 0,
|
|
747
|
-
"windows": {
|
|
748
|
-
"hour": {
|
|
749
|
-
"bucket": "2025-10-04T18",
|
|
750
|
-
"windowMinutes": 60,
|
|
751
|
-
"startedAt": "2025-10-04T18:00:00.000Z",
|
|
752
|
-
"endedAt": "2025-10-04T18:21:52.103Z",
|
|
753
|
-
"completedCostUsd": 8.75,
|
|
754
|
-
"completedCalls": 24
|
|
755
|
-
},
|
|
756
|
-
"day": {
|
|
757
|
-
"bucket": "2025-10-04",
|
|
758
|
-
"windowMinutes": 1440,
|
|
759
|
-
"startedAt": "2025-10-04T00:00:00.000Z",
|
|
760
|
-
"endedAt": "2025-10-04T18:21:52.103Z",
|
|
761
|
-
"completedCostUsd": 42.1,
|
|
762
|
-
"completedCalls": 140
|
|
763
|
-
}
|
|
764
|
-
}
|
|
765
|
-
}
|
|
766
|
-
},
|
|
767
|
-
"correlationId": "corr_abc123"
|
|
768
|
-
}
|
|
735
|
+
"metered": {
|
|
736
|
+
"tokens": 768,
|
|
737
|
+
"calls": 1,
|
|
738
|
+
"searches": 1
|
|
739
|
+
},
|
|
740
|
+
"spendVelocity": {
|
|
741
|
+
"currency": "USD",
|
|
742
|
+
"source": "usage_aggregate",
|
|
743
|
+
"generatedAt": "2025-10-04T18:21:52.103Z",
|
|
744
|
+
"customerId": "cust_123",
|
|
745
|
+
"currentCallCostUsd": 0,
|
|
746
|
+
"windows": {
|
|
747
|
+
"hour": {
|
|
748
|
+
"bucket": "2025-10-04T18",
|
|
749
|
+
"windowMinutes": 60,
|
|
750
|
+
"startedAt": "2025-10-04T18:00:00.000Z",
|
|
751
|
+
"endedAt": "2025-10-04T18:21:52.103Z",
|
|
752
|
+
"completedCostUsd": 8.75,
|
|
753
|
+
"completedCalls": 24
|
|
754
|
+
},
|
|
755
|
+
"day": {
|
|
756
|
+
"bucket": "2025-10-04",
|
|
757
|
+
"windowMinutes": 1440,
|
|
758
|
+
"startedAt": "2025-10-04T00:00:00.000Z",
|
|
759
|
+
"endedAt": "2025-10-04T18:21:52.103Z",
|
|
760
|
+
"completedCostUsd": 42.1,
|
|
761
|
+
"completedCalls": 140
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
},
|
|
766
|
+
"correlationId": "corr_abc123"
|
|
767
|
+
}
|
|
769
768
|
```
|
|
770
769
|
|
|
771
770
|
Send `cachedInputTokens` when available so UsageTap can apply provider cache-read pricing correctly.
|
|
772
771
|
|
|
773
|
-
`metered` is derived from the raw Dynamo deltas. Additional meters (audio seconds, reasoning tokens, balances) will populate in later phases without breaking the contract.
|
|
774
|
-
|
|
775
|
-
`spendVelocity` is aggregate-backed current UTC hour/day telemetry. UsageTap does not enforce limits from this section; `currentCallCostUsd` is included separately because aggregate updates are asynchronous.
|
|
776
|
-
|
|
777
|
-
### Premium detection and override
|
|
772
|
+
`metered` is derived from the raw Dynamo deltas. Additional meters (audio seconds, reasoning tokens, balances) will populate in later phases without breaking the contract.
|
|
773
|
+
|
|
774
|
+
`spendVelocity` is aggregate-backed current UTC hour/day telemetry. UsageTap does not enforce limits from this section; `currentCallCostUsd` is included separately because aggregate updates are asynchronous.
|
|
775
|
+
|
|
776
|
+
### Premium detection and override
|
|
778
777
|
|
|
779
778
|
UsageTap automatically determines whether a call is premium based on the model's output token pricing:
|
|
780
779
|
- If the output token price exceeds **$4.00 per million tokens**, the call is classified as premium
|