@usagetap/sdk 0.7.1 → 0.8.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 +75 -2
- package/dist/adapters/openai.d.cts +1 -1
- package/dist/adapters/openai.d.ts +1 -1
- package/dist/adapters/openrouter.d.cts +1 -1
- package/dist/adapters/openrouter.d.ts +1 -1
- package/dist/{client-nU5xk2pN.d.cts → client-DokYK2Gv.d.cts} +88 -1
- package/dist/{client-nU5xk2pN.d.ts → client-DokYK2Gv.d.ts} +88 -1
- package/dist/express/index.cjs.map +1 -1
- package/dist/express/index.d.cts +4 -6
- package/dist/express/index.d.ts +4 -6
- package/dist/express/index.mjs.map +1 -1
- package/dist/index.cjs +101 -6
- 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 +101 -6
- package/dist/index.mjs.map +1 -1
- package/dist/openai/index.d.cts +1 -1
- package/dist/openai/index.d.ts +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -405,9 +405,10 @@ const { begin, end, vendor, endUsage } = envelope.data;
|
|
|
405
405
|
|
|
406
406
|
Key exports from `@usagetap/sdk`:
|
|
407
407
|
|
|
408
|
-
- `UsageTapClient` – minimal HTTP client for `createCustomer`, `changePlan`, `call_begin`, `call_end`, and `checkUsage`.
|
|
408
|
+
- `UsageTapClient` – minimal HTTP client for `createCustomer`, `changePlan`, `incrementCustomMeter`, `call_begin`, `call_end`, and `checkUsage`.
|
|
409
409
|
- `createCustomer` – idempotently ensure a customer subscription exists before starting a call.
|
|
410
410
|
- `changePlan` – switch a customer to a different usage plan with configurable strategy (immediate reset, prorated, or scheduled).
|
|
411
|
+
- `incrementCustomMeter` – track custom usage metrics beyond standard LLM counters (agent actions, documents, API calls, etc.).
|
|
411
412
|
- `checkUsage` – lightweight method to query current usage status without creating a call session.
|
|
412
413
|
- `wrapFetch` – wraps a fetch function to automatically instrument OpenAI API calls (minimal integration).
|
|
413
414
|
- `createIdempotencyKey` – helper for generating UsageTap-compatible idempotency keys.
|
|
@@ -438,7 +439,7 @@ console.log("Plan:", snapshot.data.plan);
|
|
|
438
439
|
console.log("Allowed entitlements:", snapshot.data.allowed);
|
|
439
440
|
```
|
|
440
441
|
|
|
441
|
-
This returns the same rich subscription snapshot surfaces by `call_begin` and `checkUsage`, making it safe to cache the response for onboarding flows. Pass
|
|
442
|
+
This returns the same rich subscription snapshot surfaces by `call_begin` and `checkUsage`, making it safe to cache the response for onboarding flows. Pass `idempotencyKey` in `CreateCustomerOptions` when you need deterministic keys across services; otherwise the client auto-generates one by default. Both `idempotencyKey` (preferred) and `idempotency` (deprecated) are supported.
|
|
442
443
|
|
|
443
444
|
### Change a customer's plan
|
|
444
445
|
|
|
@@ -477,6 +478,78 @@ console.log("Balances:", usageStatus.data.balances);
|
|
|
477
478
|
|
|
478
479
|
This returns the same rich usage snapshot as `call_begin` (meters, entitlements, subscription details, plan info, balances) but without creating a call record. Use this for dashboard widgets, pre-flight checks, or displaying quota status to users.
|
|
479
480
|
|
|
481
|
+
### Increment custom meters
|
|
482
|
+
|
|
483
|
+
Custom meters allow you to track usage beyond standard LLM metrics—ideal for agent actions, document processing, API calls, or any custom usage you need to meter.
|
|
484
|
+
|
|
485
|
+
```ts
|
|
486
|
+
const result = await usageTap.incrementCustomMeter({
|
|
487
|
+
customerId: "cust_123",
|
|
488
|
+
meterSlot: "CUSTOM1", // or "CUSTOM2"
|
|
489
|
+
amount: 5,
|
|
490
|
+
feature: "agent_actions",
|
|
491
|
+
tags: ["workflow_automation"],
|
|
492
|
+
metadata: {
|
|
493
|
+
workflowId: "wf_abc123",
|
|
494
|
+
actionType: "email_send",
|
|
495
|
+
},
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
console.log("Event recorded:", result.data.eventId);
|
|
499
|
+
console.log("Remaining quota:", result.data.meter.remaining);
|
|
500
|
+
console.log("Blocked:", result.data.blocked);
|
|
501
|
+
```
|
|
502
|
+
|
|
503
|
+
**Parameters:**
|
|
504
|
+
|
|
505
|
+
- `customerId` (string, required): Customer identifier
|
|
506
|
+
- `meterSlot` ("CUSTOM1" | "CUSTOM2", required): Which custom meter to increment
|
|
507
|
+
- `amount` (number, required): Positive number to decrement from quota
|
|
508
|
+
- `feature` (string, optional): Feature identifier for tracking
|
|
509
|
+
- `tags` (string[], optional): Tags for categorization
|
|
510
|
+
- `metadata` (object, optional): Additional metadata
|
|
511
|
+
|
|
512
|
+
The method returns the updated meter snapshot showing remaining quota, limits, and usage. If the customer's plan has `limitType: "BLOCK"` and quota is exceeded, a `UsageTapError` is thrown with code `USAGETAP_AUTH_ERROR`.
|
|
513
|
+
|
|
514
|
+
**Use cases:**
|
|
515
|
+
|
|
516
|
+
```ts
|
|
517
|
+
// Track agent tool invocations
|
|
518
|
+
await usageTap.incrementCustomMeter({
|
|
519
|
+
customerId: "cust_123",
|
|
520
|
+
meterSlot: "CUSTOM1",
|
|
521
|
+
amount: 1,
|
|
522
|
+
feature: "agent.tool_call",
|
|
523
|
+
tags: ["web_search"],
|
|
524
|
+
});
|
|
525
|
+
|
|
526
|
+
// Track document processing (10 pages)
|
|
527
|
+
await usageTap.incrementCustomMeter({
|
|
528
|
+
customerId: "cust_456",
|
|
529
|
+
meterSlot: "CUSTOM2",
|
|
530
|
+
amount: 10,
|
|
531
|
+
feature: "document.ocr",
|
|
532
|
+
metadata: { documentId: "doc_789", pages: 10 },
|
|
533
|
+
});
|
|
534
|
+
|
|
535
|
+
// Track external API calls
|
|
536
|
+
await usageTap.incrementCustomMeter({
|
|
537
|
+
customerId: "cust_789",
|
|
538
|
+
meterSlot: "CUSTOM1",
|
|
539
|
+
amount: 1,
|
|
540
|
+
feature: "external_api.maps",
|
|
541
|
+
tags: ["geocoding"],
|
|
542
|
+
});
|
|
543
|
+
```
|
|
544
|
+
|
|
545
|
+
**Important notes:**
|
|
546
|
+
|
|
547
|
+
1. Custom meters must be enabled in the customer's usage plan
|
|
548
|
+
2. The `amount` decrements the remaining quota (like token usage)
|
|
549
|
+
3. With `BLOCK` policy, exceeding quota throws an error
|
|
550
|
+
4. With `DOWNGRADE` policy, usage continues but quota can go negative
|
|
551
|
+
5. Unlimited meters don't track usage but still record events for analytics
|
|
552
|
+
|
|
480
553
|
## Response envelope (canonical only)
|
|
481
554
|
|
|
482
555
|
UsageTap responds exclusively with the canonical `{ result, data, correlationId }` envelope for every endpoint. The SDK automatically sends `Accept: application/vnd.usagetap.v1+json`, parses the envelope, and returns strongly typed data structures. Transitional `raw` payloads and the `normalize*` helpers have been removed—`response.data` already contains the canonical shape you should persist or render.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import OpenAI from 'openai';
|
|
2
|
-
import { B as BeginCallRequest, U as UsageTapClient, W as WithUsageOptions, V as VendorHints, a as UsageTapSuccessResponse, b as BeginCallResponseBody, E as EndCallRequest } from '../client-
|
|
2
|
+
import { B as BeginCallRequest, U as UsageTapClient, W as WithUsageOptions, V as VendorHints, a as UsageTapSuccessResponse, b as BeginCallResponseBody, E as EndCallRequest } from '../client-DokYK2Gv.cjs';
|
|
3
3
|
|
|
4
4
|
interface OpenAIAdapterInit {
|
|
5
5
|
client: OpenAI;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import OpenAI from 'openai';
|
|
2
|
-
import { B as BeginCallRequest, U as UsageTapClient, W as WithUsageOptions, V as VendorHints, a as UsageTapSuccessResponse, b as BeginCallResponseBody, E as EndCallRequest } from '../client-
|
|
2
|
+
import { B as BeginCallRequest, U as UsageTapClient, W as WithUsageOptions, V as VendorHints, a as UsageTapSuccessResponse, b as BeginCallResponseBody, E as EndCallRequest } from '../client-DokYK2Gv.js';
|
|
3
3
|
|
|
4
4
|
interface OpenAIAdapterInit {
|
|
5
5
|
client: OpenAI;
|
|
@@ -26,6 +26,7 @@ interface MeterSummary {
|
|
|
26
26
|
used: number | null;
|
|
27
27
|
unlimited: boolean;
|
|
28
28
|
ratio: number | null;
|
|
29
|
+
label?: string;
|
|
29
30
|
}
|
|
30
31
|
type MeterSnapshot = Record<string, MeterSummary>;
|
|
31
32
|
type RemainingRatios = Record<string, number | null | undefined>;
|
|
@@ -64,7 +65,20 @@ interface BeginCallRequest {
|
|
|
64
65
|
requested?: RequestedEntitlements;
|
|
65
66
|
feature?: string;
|
|
66
67
|
tags?: string[];
|
|
68
|
+
/**
|
|
69
|
+
* Optional PAYG hold hint (USD) to authorize upfront during call_begin.
|
|
70
|
+
* When provided, overrides the plan's default hold for this call only.
|
|
71
|
+
*/
|
|
72
|
+
holdUsd?: number;
|
|
73
|
+
/**
|
|
74
|
+
* Idempotency key for safe retries. When omitted, UsageTap derives a key deterministically.
|
|
75
|
+
* @deprecated Use idempotencyKey instead
|
|
76
|
+
*/
|
|
67
77
|
idempotency?: string;
|
|
78
|
+
/**
|
|
79
|
+
* Idempotency key for safe retries. When omitted, UsageTap derives a key deterministically.
|
|
80
|
+
*/
|
|
81
|
+
idempotencyKey?: string;
|
|
68
82
|
customerName?: string;
|
|
69
83
|
customerEmail?: string;
|
|
70
84
|
stripeCustomerId?: string;
|
|
@@ -75,6 +89,8 @@ interface BalanceSummary {
|
|
|
75
89
|
tokensRemaining?: number;
|
|
76
90
|
searchesRemaining?: number;
|
|
77
91
|
audioSecondsRemaining?: number;
|
|
92
|
+
customMeter1Remaining?: number;
|
|
93
|
+
customMeter2Remaining?: number;
|
|
78
94
|
}
|
|
79
95
|
interface PlanSummary {
|
|
80
96
|
id: string | null;
|
|
@@ -103,6 +119,12 @@ interface BeginCallResponseBody {
|
|
|
103
119
|
}
|
|
104
120
|
interface EndCallRequest {
|
|
105
121
|
callId: string;
|
|
122
|
+
/** Optional customer ID for metric tracking. Not sent to API, used for onUsageMetric callback. */
|
|
123
|
+
customerId?: string;
|
|
124
|
+
/** Optional feature for metric tracking. Not sent to API, used for onUsageMetric callback. */
|
|
125
|
+
feature?: string;
|
|
126
|
+
/** Optional tags for metric tracking. Not sent to API, used for onUsageMetric callback. */
|
|
127
|
+
tags?: string[];
|
|
106
128
|
modelUsed?: string;
|
|
107
129
|
inputTokens?: number;
|
|
108
130
|
responseTokens?: number;
|
|
@@ -200,6 +222,11 @@ interface UsageTapClientOptions {
|
|
|
200
222
|
*/
|
|
201
223
|
autoIdempotency?: boolean;
|
|
202
224
|
onLog?: (entry: UsageTapLogEntry) => void;
|
|
225
|
+
/**
|
|
226
|
+
* Callback invoked when usage metrics are recorded (call_end, custom_meter).
|
|
227
|
+
* Use this to export metrics to OpenTelemetry or other observability systems.
|
|
228
|
+
*/
|
|
229
|
+
onUsageMetric?: (event: UsageMetricEvent) => void;
|
|
203
230
|
/**
|
|
204
231
|
* Override the default Authorization header with x-api-key when true.
|
|
205
232
|
*/
|
|
@@ -283,6 +310,63 @@ interface ChangePlanResponseBody {
|
|
|
283
310
|
success: boolean;
|
|
284
311
|
subscription: SubscriptionSnapshot;
|
|
285
312
|
}
|
|
313
|
+
type CustomMeterSlot = "CUSTOM1" | "CUSTOM2";
|
|
314
|
+
interface IncrementCustomMeterRequest {
|
|
315
|
+
customerId: string;
|
|
316
|
+
meterSlot: CustomMeterSlot;
|
|
317
|
+
amount: number;
|
|
318
|
+
feature?: string;
|
|
319
|
+
tags?: string[];
|
|
320
|
+
metadata?: Record<string, unknown>;
|
|
321
|
+
}
|
|
322
|
+
interface IncrementCustomMeterOptions extends RequestOptions {
|
|
323
|
+
correlationId?: string;
|
|
324
|
+
idempotencyKey?: string;
|
|
325
|
+
}
|
|
326
|
+
interface IncrementCustomMeterResponseBody {
|
|
327
|
+
success: boolean;
|
|
328
|
+
eventId: string;
|
|
329
|
+
meterSlot: CustomMeterSlot;
|
|
330
|
+
amount: number;
|
|
331
|
+
meter: MeterSummary;
|
|
332
|
+
blocked: boolean;
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Event emitted when usage metrics are recorded.
|
|
336
|
+
* Use with `onUsageMetric` callback to export to OpenTelemetry or other observability systems.
|
|
337
|
+
*/
|
|
338
|
+
interface UsageMetricEvent {
|
|
339
|
+
/** Type of metric event */
|
|
340
|
+
type: "call_end" | "custom_meter";
|
|
341
|
+
/** Timestamp in ISO 8601 format */
|
|
342
|
+
timestamp: string;
|
|
343
|
+
/** Customer identifier */
|
|
344
|
+
customerId: string;
|
|
345
|
+
/** Unique call identifier (for call_end events) */
|
|
346
|
+
callId?: string;
|
|
347
|
+
/** Feature tag */
|
|
348
|
+
feature?: string;
|
|
349
|
+
/** Additional tags */
|
|
350
|
+
tags?: string[];
|
|
351
|
+
/** Model used for the call */
|
|
352
|
+
modelUsed?: string;
|
|
353
|
+
/** Usage metrics */
|
|
354
|
+
metrics: {
|
|
355
|
+
inputTokens?: number;
|
|
356
|
+
responseTokens?: number;
|
|
357
|
+
cachedTokens?: number;
|
|
358
|
+
reasoningTokens?: number;
|
|
359
|
+
searches?: number;
|
|
360
|
+
audioSeconds?: number;
|
|
361
|
+
costUsd?: number;
|
|
362
|
+
/** Custom meter slot (for custom_meter events) */
|
|
363
|
+
customMeterSlot?: CustomMeterSlot;
|
|
364
|
+
/** Custom meter amount (for custom_meter events) */
|
|
365
|
+
customMeterAmount?: number;
|
|
366
|
+
};
|
|
367
|
+
/** Correlation ID for tracing */
|
|
368
|
+
correlationId?: string;
|
|
369
|
+
}
|
|
286
370
|
|
|
287
371
|
declare class UsageTapClient {
|
|
288
372
|
private readonly apiKey;
|
|
@@ -294,6 +378,7 @@ declare class UsageTapClient {
|
|
|
294
378
|
private readonly retryDefaults;
|
|
295
379
|
private readonly idempotencyGenerator;
|
|
296
380
|
private readonly logFn?;
|
|
381
|
+
private readonly metricFn?;
|
|
297
382
|
private readonly authHeader;
|
|
298
383
|
private readonly autoIdempotency;
|
|
299
384
|
constructor(options: UsageTapClientOptions);
|
|
@@ -302,16 +387,18 @@ declare class UsageTapClient {
|
|
|
302
387
|
checkUsage(request: CheckUsageRequest, options?: CheckUsageOptions): Promise<UsageTapSuccessResponse<CheckUsageResponseBody>>;
|
|
303
388
|
createCustomer(request: CreateCustomerRequest, options?: CreateCustomerOptions): Promise<UsageTapSuccessResponse<CreateCustomerResponseBody>>;
|
|
304
389
|
changePlan(request: ChangePlanRequest, options?: ChangePlanOptions): Promise<UsageTapSuccessResponse<ChangePlanResponseBody>>;
|
|
390
|
+
incrementCustomMeter(request: IncrementCustomMeterRequest, options?: IncrementCustomMeterOptions): Promise<UsageTapSuccessResponse<IncrementCustomMeterResponseBody>>;
|
|
305
391
|
withUsage<T>(beginRequest: BeginCallRequest, handler: (context: WithUsageContext) => Promise<T>, options?: WithUsageOptions): Promise<T>;
|
|
306
392
|
private request;
|
|
307
393
|
private requestGet;
|
|
308
394
|
private performFetch;
|
|
309
395
|
private composeHeaders;
|
|
310
396
|
private log;
|
|
397
|
+
private emitUsageMetric;
|
|
311
398
|
private mergeTags;
|
|
312
399
|
private shouldRetry;
|
|
313
400
|
private toHttpError;
|
|
314
401
|
private toApiError;
|
|
315
402
|
}
|
|
316
403
|
|
|
317
|
-
export { type AllowedEntitlements as A, type BeginCallRequest as B, type CreateCustomerOptions as C, type
|
|
404
|
+
export { type AllowedEntitlements as A, type BeginCallRequest as B, type CreateCustomerOptions as C, type UsageTapResultStatus as D, type EndCallRequest as E, type UsageTapLogEntry as F, type WithUsageContext as G, type ModelHints as H, type IncrementCustomMeterOptions as I, type IdempotencyMetadata as J, type UsageMetricEvent as K, type LimitType as L, type MeterSummary as M, type PlanSummary as P, type RemainingRatios as R, type SubscriptionSnapshot as S, UsageTapClient as U, type VendorHints as V, type WithUsageOptions as W, type UsageTapSuccessResponse as a, type BeginCallResponseBody as b, type BeginCallOptions as c, type CreateCustomerRequest as d, type CreateCustomerResponseBody as e, type CheckUsageOptions as f, type CheckUsageRequest as g, type CheckUsageResponseBody as h, type ChangePlanOptions as i, type ChangePlanRequest as j, type ChangePlanResponseBody as k, type ChangePlanStrategy as l, type IncrementCustomMeterRequest as m, type IncrementCustomMeterResponseBody as n, type CustomMeterSlot as o, type EndCallOptions as p, type EndCallResponseBody as q, type BalanceSummary as r, type EntitlementHints as s, type MeterSnapshot as t, type MeteredUsage as u, type RequestedEntitlements as v, type RetryOptions as w, type UsageTapClientOptions as x, type UsageTapErrorResponse as y, type UsageTapResultEnvelope as z };
|
|
@@ -26,6 +26,7 @@ interface MeterSummary {
|
|
|
26
26
|
used: number | null;
|
|
27
27
|
unlimited: boolean;
|
|
28
28
|
ratio: number | null;
|
|
29
|
+
label?: string;
|
|
29
30
|
}
|
|
30
31
|
type MeterSnapshot = Record<string, MeterSummary>;
|
|
31
32
|
type RemainingRatios = Record<string, number | null | undefined>;
|
|
@@ -64,7 +65,20 @@ interface BeginCallRequest {
|
|
|
64
65
|
requested?: RequestedEntitlements;
|
|
65
66
|
feature?: string;
|
|
66
67
|
tags?: string[];
|
|
68
|
+
/**
|
|
69
|
+
* Optional PAYG hold hint (USD) to authorize upfront during call_begin.
|
|
70
|
+
* When provided, overrides the plan's default hold for this call only.
|
|
71
|
+
*/
|
|
72
|
+
holdUsd?: number;
|
|
73
|
+
/**
|
|
74
|
+
* Idempotency key for safe retries. When omitted, UsageTap derives a key deterministically.
|
|
75
|
+
* @deprecated Use idempotencyKey instead
|
|
76
|
+
*/
|
|
67
77
|
idempotency?: string;
|
|
78
|
+
/**
|
|
79
|
+
* Idempotency key for safe retries. When omitted, UsageTap derives a key deterministically.
|
|
80
|
+
*/
|
|
81
|
+
idempotencyKey?: string;
|
|
68
82
|
customerName?: string;
|
|
69
83
|
customerEmail?: string;
|
|
70
84
|
stripeCustomerId?: string;
|
|
@@ -75,6 +89,8 @@ interface BalanceSummary {
|
|
|
75
89
|
tokensRemaining?: number;
|
|
76
90
|
searchesRemaining?: number;
|
|
77
91
|
audioSecondsRemaining?: number;
|
|
92
|
+
customMeter1Remaining?: number;
|
|
93
|
+
customMeter2Remaining?: number;
|
|
78
94
|
}
|
|
79
95
|
interface PlanSummary {
|
|
80
96
|
id: string | null;
|
|
@@ -103,6 +119,12 @@ interface BeginCallResponseBody {
|
|
|
103
119
|
}
|
|
104
120
|
interface EndCallRequest {
|
|
105
121
|
callId: string;
|
|
122
|
+
/** Optional customer ID for metric tracking. Not sent to API, used for onUsageMetric callback. */
|
|
123
|
+
customerId?: string;
|
|
124
|
+
/** Optional feature for metric tracking. Not sent to API, used for onUsageMetric callback. */
|
|
125
|
+
feature?: string;
|
|
126
|
+
/** Optional tags for metric tracking. Not sent to API, used for onUsageMetric callback. */
|
|
127
|
+
tags?: string[];
|
|
106
128
|
modelUsed?: string;
|
|
107
129
|
inputTokens?: number;
|
|
108
130
|
responseTokens?: number;
|
|
@@ -200,6 +222,11 @@ interface UsageTapClientOptions {
|
|
|
200
222
|
*/
|
|
201
223
|
autoIdempotency?: boolean;
|
|
202
224
|
onLog?: (entry: UsageTapLogEntry) => void;
|
|
225
|
+
/**
|
|
226
|
+
* Callback invoked when usage metrics are recorded (call_end, custom_meter).
|
|
227
|
+
* Use this to export metrics to OpenTelemetry or other observability systems.
|
|
228
|
+
*/
|
|
229
|
+
onUsageMetric?: (event: UsageMetricEvent) => void;
|
|
203
230
|
/**
|
|
204
231
|
* Override the default Authorization header with x-api-key when true.
|
|
205
232
|
*/
|
|
@@ -283,6 +310,63 @@ interface ChangePlanResponseBody {
|
|
|
283
310
|
success: boolean;
|
|
284
311
|
subscription: SubscriptionSnapshot;
|
|
285
312
|
}
|
|
313
|
+
type CustomMeterSlot = "CUSTOM1" | "CUSTOM2";
|
|
314
|
+
interface IncrementCustomMeterRequest {
|
|
315
|
+
customerId: string;
|
|
316
|
+
meterSlot: CustomMeterSlot;
|
|
317
|
+
amount: number;
|
|
318
|
+
feature?: string;
|
|
319
|
+
tags?: string[];
|
|
320
|
+
metadata?: Record<string, unknown>;
|
|
321
|
+
}
|
|
322
|
+
interface IncrementCustomMeterOptions extends RequestOptions {
|
|
323
|
+
correlationId?: string;
|
|
324
|
+
idempotencyKey?: string;
|
|
325
|
+
}
|
|
326
|
+
interface IncrementCustomMeterResponseBody {
|
|
327
|
+
success: boolean;
|
|
328
|
+
eventId: string;
|
|
329
|
+
meterSlot: CustomMeterSlot;
|
|
330
|
+
amount: number;
|
|
331
|
+
meter: MeterSummary;
|
|
332
|
+
blocked: boolean;
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Event emitted when usage metrics are recorded.
|
|
336
|
+
* Use with `onUsageMetric` callback to export to OpenTelemetry or other observability systems.
|
|
337
|
+
*/
|
|
338
|
+
interface UsageMetricEvent {
|
|
339
|
+
/** Type of metric event */
|
|
340
|
+
type: "call_end" | "custom_meter";
|
|
341
|
+
/** Timestamp in ISO 8601 format */
|
|
342
|
+
timestamp: string;
|
|
343
|
+
/** Customer identifier */
|
|
344
|
+
customerId: string;
|
|
345
|
+
/** Unique call identifier (for call_end events) */
|
|
346
|
+
callId?: string;
|
|
347
|
+
/** Feature tag */
|
|
348
|
+
feature?: string;
|
|
349
|
+
/** Additional tags */
|
|
350
|
+
tags?: string[];
|
|
351
|
+
/** Model used for the call */
|
|
352
|
+
modelUsed?: string;
|
|
353
|
+
/** Usage metrics */
|
|
354
|
+
metrics: {
|
|
355
|
+
inputTokens?: number;
|
|
356
|
+
responseTokens?: number;
|
|
357
|
+
cachedTokens?: number;
|
|
358
|
+
reasoningTokens?: number;
|
|
359
|
+
searches?: number;
|
|
360
|
+
audioSeconds?: number;
|
|
361
|
+
costUsd?: number;
|
|
362
|
+
/** Custom meter slot (for custom_meter events) */
|
|
363
|
+
customMeterSlot?: CustomMeterSlot;
|
|
364
|
+
/** Custom meter amount (for custom_meter events) */
|
|
365
|
+
customMeterAmount?: number;
|
|
366
|
+
};
|
|
367
|
+
/** Correlation ID for tracing */
|
|
368
|
+
correlationId?: string;
|
|
369
|
+
}
|
|
286
370
|
|
|
287
371
|
declare class UsageTapClient {
|
|
288
372
|
private readonly apiKey;
|
|
@@ -294,6 +378,7 @@ declare class UsageTapClient {
|
|
|
294
378
|
private readonly retryDefaults;
|
|
295
379
|
private readonly idempotencyGenerator;
|
|
296
380
|
private readonly logFn?;
|
|
381
|
+
private readonly metricFn?;
|
|
297
382
|
private readonly authHeader;
|
|
298
383
|
private readonly autoIdempotency;
|
|
299
384
|
constructor(options: UsageTapClientOptions);
|
|
@@ -302,16 +387,18 @@ declare class UsageTapClient {
|
|
|
302
387
|
checkUsage(request: CheckUsageRequest, options?: CheckUsageOptions): Promise<UsageTapSuccessResponse<CheckUsageResponseBody>>;
|
|
303
388
|
createCustomer(request: CreateCustomerRequest, options?: CreateCustomerOptions): Promise<UsageTapSuccessResponse<CreateCustomerResponseBody>>;
|
|
304
389
|
changePlan(request: ChangePlanRequest, options?: ChangePlanOptions): Promise<UsageTapSuccessResponse<ChangePlanResponseBody>>;
|
|
390
|
+
incrementCustomMeter(request: IncrementCustomMeterRequest, options?: IncrementCustomMeterOptions): Promise<UsageTapSuccessResponse<IncrementCustomMeterResponseBody>>;
|
|
305
391
|
withUsage<T>(beginRequest: BeginCallRequest, handler: (context: WithUsageContext) => Promise<T>, options?: WithUsageOptions): Promise<T>;
|
|
306
392
|
private request;
|
|
307
393
|
private requestGet;
|
|
308
394
|
private performFetch;
|
|
309
395
|
private composeHeaders;
|
|
310
396
|
private log;
|
|
397
|
+
private emitUsageMetric;
|
|
311
398
|
private mergeTags;
|
|
312
399
|
private shouldRetry;
|
|
313
400
|
private toHttpError;
|
|
314
401
|
private toApiError;
|
|
315
402
|
}
|
|
316
403
|
|
|
317
|
-
export { type AllowedEntitlements as A, type BeginCallRequest as B, type CreateCustomerOptions as C, type
|
|
404
|
+
export { type AllowedEntitlements as A, type BeginCallRequest as B, type CreateCustomerOptions as C, type UsageTapResultStatus as D, type EndCallRequest as E, type UsageTapLogEntry as F, type WithUsageContext as G, type ModelHints as H, type IncrementCustomMeterOptions as I, type IdempotencyMetadata as J, type UsageMetricEvent as K, type LimitType as L, type MeterSummary as M, type PlanSummary as P, type RemainingRatios as R, type SubscriptionSnapshot as S, UsageTapClient as U, type VendorHints as V, type WithUsageOptions as W, type UsageTapSuccessResponse as a, type BeginCallResponseBody as b, type BeginCallOptions as c, type CreateCustomerRequest as d, type CreateCustomerResponseBody as e, type CheckUsageOptions as f, type CheckUsageRequest as g, type CheckUsageResponseBody as h, type ChangePlanOptions as i, type ChangePlanRequest as j, type ChangePlanResponseBody as k, type ChangePlanStrategy as l, type IncrementCustomMeterRequest as m, type IncrementCustomMeterResponseBody as n, type CustomMeterSlot as o, type EndCallOptions as p, type EndCallResponseBody as q, type BalanceSummary as r, type EntitlementHints as s, type MeterSnapshot as t, type MeteredUsage as u, type RequestedEntitlements as v, type RetryOptions as w, type UsageTapClientOptions as x, type UsageTapErrorResponse as y, type UsageTapResultEnvelope as z };
|