@reaatech/llm-cost-telemetry-providers 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/LICENSE +21 -0
- package/README.md +205 -0
- package/dist/index.cjs +467 -0
- package/dist/index.d.cts +193 -0
- package/dist/index.d.ts +193 -0
- package/dist/index.js +434 -0
- package/package.json +63 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { CostSpan, TelemetryContext, Provider } from '@reaatech/llm-cost-telemetry';
|
|
2
|
+
import OpenAI from 'openai';
|
|
3
|
+
import Anthropic from '@anthropic-ai/sdk';
|
|
4
|
+
import { GoogleGenerativeAI, GenerativeModel } from '@google/generative-ai';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Base provider wrapper interface
|
|
8
|
+
* All provider wrappers extend this abstract class
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Request metadata captured by the wrapper
|
|
13
|
+
*/
|
|
14
|
+
interface RequestMetadata {
|
|
15
|
+
/** Model being used */
|
|
16
|
+
model: string;
|
|
17
|
+
/** Request parameters */
|
|
18
|
+
params: unknown;
|
|
19
|
+
/** Telemetry context if provided */
|
|
20
|
+
telemetry?: Partial<TelemetryContext>;
|
|
21
|
+
/** Request start time */
|
|
22
|
+
startTime: Date;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Response metadata captured by the wrapper
|
|
26
|
+
*/
|
|
27
|
+
interface ResponseMetadata {
|
|
28
|
+
/** Input tokens used */
|
|
29
|
+
inputTokens: number;
|
|
30
|
+
/** Output tokens used */
|
|
31
|
+
outputTokens: number;
|
|
32
|
+
/** Cache read tokens (if applicable) */
|
|
33
|
+
cacheReadTokens?: number;
|
|
34
|
+
/** Cache creation tokens (if applicable) */
|
|
35
|
+
cacheCreationTokens?: number;
|
|
36
|
+
/** Response end time */
|
|
37
|
+
endTime: Date;
|
|
38
|
+
/** Any error that occurred */
|
|
39
|
+
error?: Error;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Callback for when a cost span is recorded
|
|
43
|
+
*/
|
|
44
|
+
type SpanCallback = (span: CostSpan) => void;
|
|
45
|
+
/**
|
|
46
|
+
* Abstract base class for provider wrappers
|
|
47
|
+
*/
|
|
48
|
+
declare abstract class BaseProviderWrapper<TClient = unknown> {
|
|
49
|
+
/** The wrapped client */
|
|
50
|
+
protected client: TClient;
|
|
51
|
+
/** Callback for cost spans */
|
|
52
|
+
protected onSpanCallback: SpanCallback | null;
|
|
53
|
+
/** Default telemetry context */
|
|
54
|
+
protected defaultContext: Partial<TelemetryContext>;
|
|
55
|
+
/**
|
|
56
|
+
* Create a new provider wrapper
|
|
57
|
+
*/
|
|
58
|
+
constructor(client: TClient);
|
|
59
|
+
/**
|
|
60
|
+
* Get the provider name
|
|
61
|
+
*/
|
|
62
|
+
abstract get provider(): Provider;
|
|
63
|
+
/**
|
|
64
|
+
* Set the callback for cost spans
|
|
65
|
+
*/
|
|
66
|
+
onSpan(callback: SpanCallback): void;
|
|
67
|
+
/**
|
|
68
|
+
* Set default telemetry context
|
|
69
|
+
*/
|
|
70
|
+
setDefaultContext(context: Partial<TelemetryContext>): void;
|
|
71
|
+
/**
|
|
72
|
+
* Create a cost span from request and response metadata
|
|
73
|
+
*/
|
|
74
|
+
protected createSpan(request: RequestMetadata, response: ResponseMetadata): CostSpan;
|
|
75
|
+
/**
|
|
76
|
+
* Emit a cost span
|
|
77
|
+
*/
|
|
78
|
+
protected emitSpan(span: CostSpan): void;
|
|
79
|
+
/**
|
|
80
|
+
* Extract telemetry context from request options
|
|
81
|
+
*/
|
|
82
|
+
protected extractTelemetryContext(options: Record<string, unknown>): Partial<TelemetryContext> | undefined;
|
|
83
|
+
/**
|
|
84
|
+
* Dispose of the wrapper and release resources
|
|
85
|
+
*/
|
|
86
|
+
dispose(): void;
|
|
87
|
+
/**
|
|
88
|
+
* Get the underlying client
|
|
89
|
+
*/
|
|
90
|
+
unwrap(): TClient;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* OpenAI SDK wrapper for cost telemetry
|
|
95
|
+
*/
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Wrapped OpenAI client type
|
|
99
|
+
*/
|
|
100
|
+
type WrappedOpenAI = OpenAI & {
|
|
101
|
+
chat: {
|
|
102
|
+
completions: {
|
|
103
|
+
create: OpenAI['chat']['completions']['create'];
|
|
104
|
+
};
|
|
105
|
+
};
|
|
106
|
+
completions: {
|
|
107
|
+
create: OpenAI['completions']['create'];
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
/**
|
|
111
|
+
* OpenAI provider wrapper
|
|
112
|
+
*/
|
|
113
|
+
declare class OpenAIWrapper extends BaseProviderWrapper<OpenAI> {
|
|
114
|
+
/**
|
|
115
|
+
* Get the provider name
|
|
116
|
+
*/
|
|
117
|
+
get provider(): 'openai';
|
|
118
|
+
/**
|
|
119
|
+
* Wrap the OpenAI client to intercept chat completions
|
|
120
|
+
*/
|
|
121
|
+
wrap(): WrappedOpenAI;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Wrap an OpenAI client for cost telemetry
|
|
125
|
+
*/
|
|
126
|
+
declare function wrapOpenAI(client: OpenAI): WrappedOpenAI;
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Anthropic SDK wrapper for cost telemetry
|
|
130
|
+
*/
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Wrapped Anthropic client type
|
|
134
|
+
*/
|
|
135
|
+
type WrappedAnthropic = Anthropic & {
|
|
136
|
+
messages: {
|
|
137
|
+
create: Anthropic['messages']['create'];
|
|
138
|
+
};
|
|
139
|
+
};
|
|
140
|
+
/**
|
|
141
|
+
* Anthropic provider wrapper with cache-aware cost tracking
|
|
142
|
+
*/
|
|
143
|
+
declare class AnthropicWrapper extends BaseProviderWrapper<Anthropic> {
|
|
144
|
+
/**
|
|
145
|
+
* Get the provider name
|
|
146
|
+
*/
|
|
147
|
+
get provider(): 'anthropic';
|
|
148
|
+
/**
|
|
149
|
+
* Wrap the Anthropic client to intercept messages.create
|
|
150
|
+
*/
|
|
151
|
+
wrap(): WrappedAnthropic;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Wrap an Anthropic client for cost telemetry
|
|
155
|
+
*/
|
|
156
|
+
declare function wrapAnthropic(client: Anthropic): WrappedAnthropic;
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Google Generative AI SDK wrapper for cost telemetry
|
|
160
|
+
*/
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Wrapped Google Generative AI client type
|
|
164
|
+
*/
|
|
165
|
+
type WrappedGoogleGenerativeAI = GoogleGenerativeAI & {
|
|
166
|
+
getGenerativeModel: GoogleGenerativeAI['getGenerativeModel'];
|
|
167
|
+
};
|
|
168
|
+
/**
|
|
169
|
+
* Wrapped GenerativeModel with telemetry support
|
|
170
|
+
*/
|
|
171
|
+
interface WrappedGenerativeModel extends GenerativeModel {
|
|
172
|
+
generateContent: GenerativeModel['generateContent'];
|
|
173
|
+
generateContentStream: GenerativeModel['generateContentStream'];
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Google Generative AI provider wrapper
|
|
177
|
+
*/
|
|
178
|
+
declare class GoogleGenerativeAIWrapper extends BaseProviderWrapper<GoogleGenerativeAI> {
|
|
179
|
+
/**
|
|
180
|
+
* Get the provider name
|
|
181
|
+
*/
|
|
182
|
+
get provider(): 'google';
|
|
183
|
+
/**
|
|
184
|
+
* Wrap the GoogleGenerativeAI client to intercept generateContent
|
|
185
|
+
*/
|
|
186
|
+
wrap(): WrappedGoogleGenerativeAI;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Wrap a GoogleGenerativeAI client for cost telemetry
|
|
190
|
+
*/
|
|
191
|
+
declare function wrapGoogleGenerativeAI(client: GoogleGenerativeAI): WrappedGoogleGenerativeAI;
|
|
192
|
+
|
|
193
|
+
export { AnthropicWrapper, BaseProviderWrapper, GoogleGenerativeAIWrapper, OpenAIWrapper, type RequestMetadata, type ResponseMetadata, type SpanCallback, type WrappedAnthropic, type WrappedGenerativeModel, type WrappedGoogleGenerativeAI, type WrappedOpenAI, wrapAnthropic, wrapGoogleGenerativeAI, wrapOpenAI };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { CostSpan, TelemetryContext, Provider } from '@reaatech/llm-cost-telemetry';
|
|
2
|
+
import OpenAI from 'openai';
|
|
3
|
+
import Anthropic from '@anthropic-ai/sdk';
|
|
4
|
+
import { GoogleGenerativeAI, GenerativeModel } from '@google/generative-ai';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Base provider wrapper interface
|
|
8
|
+
* All provider wrappers extend this abstract class
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Request metadata captured by the wrapper
|
|
13
|
+
*/
|
|
14
|
+
interface RequestMetadata {
|
|
15
|
+
/** Model being used */
|
|
16
|
+
model: string;
|
|
17
|
+
/** Request parameters */
|
|
18
|
+
params: unknown;
|
|
19
|
+
/** Telemetry context if provided */
|
|
20
|
+
telemetry?: Partial<TelemetryContext>;
|
|
21
|
+
/** Request start time */
|
|
22
|
+
startTime: Date;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Response metadata captured by the wrapper
|
|
26
|
+
*/
|
|
27
|
+
interface ResponseMetadata {
|
|
28
|
+
/** Input tokens used */
|
|
29
|
+
inputTokens: number;
|
|
30
|
+
/** Output tokens used */
|
|
31
|
+
outputTokens: number;
|
|
32
|
+
/** Cache read tokens (if applicable) */
|
|
33
|
+
cacheReadTokens?: number;
|
|
34
|
+
/** Cache creation tokens (if applicable) */
|
|
35
|
+
cacheCreationTokens?: number;
|
|
36
|
+
/** Response end time */
|
|
37
|
+
endTime: Date;
|
|
38
|
+
/** Any error that occurred */
|
|
39
|
+
error?: Error;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Callback for when a cost span is recorded
|
|
43
|
+
*/
|
|
44
|
+
type SpanCallback = (span: CostSpan) => void;
|
|
45
|
+
/**
|
|
46
|
+
* Abstract base class for provider wrappers
|
|
47
|
+
*/
|
|
48
|
+
declare abstract class BaseProviderWrapper<TClient = unknown> {
|
|
49
|
+
/** The wrapped client */
|
|
50
|
+
protected client: TClient;
|
|
51
|
+
/** Callback for cost spans */
|
|
52
|
+
protected onSpanCallback: SpanCallback | null;
|
|
53
|
+
/** Default telemetry context */
|
|
54
|
+
protected defaultContext: Partial<TelemetryContext>;
|
|
55
|
+
/**
|
|
56
|
+
* Create a new provider wrapper
|
|
57
|
+
*/
|
|
58
|
+
constructor(client: TClient);
|
|
59
|
+
/**
|
|
60
|
+
* Get the provider name
|
|
61
|
+
*/
|
|
62
|
+
abstract get provider(): Provider;
|
|
63
|
+
/**
|
|
64
|
+
* Set the callback for cost spans
|
|
65
|
+
*/
|
|
66
|
+
onSpan(callback: SpanCallback): void;
|
|
67
|
+
/**
|
|
68
|
+
* Set default telemetry context
|
|
69
|
+
*/
|
|
70
|
+
setDefaultContext(context: Partial<TelemetryContext>): void;
|
|
71
|
+
/**
|
|
72
|
+
* Create a cost span from request and response metadata
|
|
73
|
+
*/
|
|
74
|
+
protected createSpan(request: RequestMetadata, response: ResponseMetadata): CostSpan;
|
|
75
|
+
/**
|
|
76
|
+
* Emit a cost span
|
|
77
|
+
*/
|
|
78
|
+
protected emitSpan(span: CostSpan): void;
|
|
79
|
+
/**
|
|
80
|
+
* Extract telemetry context from request options
|
|
81
|
+
*/
|
|
82
|
+
protected extractTelemetryContext(options: Record<string, unknown>): Partial<TelemetryContext> | undefined;
|
|
83
|
+
/**
|
|
84
|
+
* Dispose of the wrapper and release resources
|
|
85
|
+
*/
|
|
86
|
+
dispose(): void;
|
|
87
|
+
/**
|
|
88
|
+
* Get the underlying client
|
|
89
|
+
*/
|
|
90
|
+
unwrap(): TClient;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* OpenAI SDK wrapper for cost telemetry
|
|
95
|
+
*/
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Wrapped OpenAI client type
|
|
99
|
+
*/
|
|
100
|
+
type WrappedOpenAI = OpenAI & {
|
|
101
|
+
chat: {
|
|
102
|
+
completions: {
|
|
103
|
+
create: OpenAI['chat']['completions']['create'];
|
|
104
|
+
};
|
|
105
|
+
};
|
|
106
|
+
completions: {
|
|
107
|
+
create: OpenAI['completions']['create'];
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
/**
|
|
111
|
+
* OpenAI provider wrapper
|
|
112
|
+
*/
|
|
113
|
+
declare class OpenAIWrapper extends BaseProviderWrapper<OpenAI> {
|
|
114
|
+
/**
|
|
115
|
+
* Get the provider name
|
|
116
|
+
*/
|
|
117
|
+
get provider(): 'openai';
|
|
118
|
+
/**
|
|
119
|
+
* Wrap the OpenAI client to intercept chat completions
|
|
120
|
+
*/
|
|
121
|
+
wrap(): WrappedOpenAI;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Wrap an OpenAI client for cost telemetry
|
|
125
|
+
*/
|
|
126
|
+
declare function wrapOpenAI(client: OpenAI): WrappedOpenAI;
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Anthropic SDK wrapper for cost telemetry
|
|
130
|
+
*/
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Wrapped Anthropic client type
|
|
134
|
+
*/
|
|
135
|
+
type WrappedAnthropic = Anthropic & {
|
|
136
|
+
messages: {
|
|
137
|
+
create: Anthropic['messages']['create'];
|
|
138
|
+
};
|
|
139
|
+
};
|
|
140
|
+
/**
|
|
141
|
+
* Anthropic provider wrapper with cache-aware cost tracking
|
|
142
|
+
*/
|
|
143
|
+
declare class AnthropicWrapper extends BaseProviderWrapper<Anthropic> {
|
|
144
|
+
/**
|
|
145
|
+
* Get the provider name
|
|
146
|
+
*/
|
|
147
|
+
get provider(): 'anthropic';
|
|
148
|
+
/**
|
|
149
|
+
* Wrap the Anthropic client to intercept messages.create
|
|
150
|
+
*/
|
|
151
|
+
wrap(): WrappedAnthropic;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Wrap an Anthropic client for cost telemetry
|
|
155
|
+
*/
|
|
156
|
+
declare function wrapAnthropic(client: Anthropic): WrappedAnthropic;
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Google Generative AI SDK wrapper for cost telemetry
|
|
160
|
+
*/
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Wrapped Google Generative AI client type
|
|
164
|
+
*/
|
|
165
|
+
type WrappedGoogleGenerativeAI = GoogleGenerativeAI & {
|
|
166
|
+
getGenerativeModel: GoogleGenerativeAI['getGenerativeModel'];
|
|
167
|
+
};
|
|
168
|
+
/**
|
|
169
|
+
* Wrapped GenerativeModel with telemetry support
|
|
170
|
+
*/
|
|
171
|
+
interface WrappedGenerativeModel extends GenerativeModel {
|
|
172
|
+
generateContent: GenerativeModel['generateContent'];
|
|
173
|
+
generateContentStream: GenerativeModel['generateContentStream'];
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Google Generative AI provider wrapper
|
|
177
|
+
*/
|
|
178
|
+
declare class GoogleGenerativeAIWrapper extends BaseProviderWrapper<GoogleGenerativeAI> {
|
|
179
|
+
/**
|
|
180
|
+
* Get the provider name
|
|
181
|
+
*/
|
|
182
|
+
get provider(): 'google';
|
|
183
|
+
/**
|
|
184
|
+
* Wrap the GoogleGenerativeAI client to intercept generateContent
|
|
185
|
+
*/
|
|
186
|
+
wrap(): WrappedGoogleGenerativeAI;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Wrap a GoogleGenerativeAI client for cost telemetry
|
|
190
|
+
*/
|
|
191
|
+
declare function wrapGoogleGenerativeAI(client: GoogleGenerativeAI): WrappedGoogleGenerativeAI;
|
|
192
|
+
|
|
193
|
+
export { AnthropicWrapper, BaseProviderWrapper, GoogleGenerativeAIWrapper, OpenAIWrapper, type RequestMetadata, type ResponseMetadata, type SpanCallback, type WrappedAnthropic, type WrappedGenerativeModel, type WrappedGoogleGenerativeAI, type WrappedOpenAI, wrapAnthropic, wrapGoogleGenerativeAI, wrapOpenAI };
|