1+ import type { Provider as UsageProvider } from '@pydantic/genai-prices'
12import { extractUsage , findProvider , type Usage } from '@pydantic/genai-prices'
23import type { GenAIAttributes , GenAIAttributesExtractor } from '../otel/attributes'
34import type { InputMessages , OutputMessages , TextPart } from '../otel/genai'
@@ -48,6 +49,8 @@ export interface SafeExtractor<RequestBody, ResponseBody, StreamChunk> {
4849export abstract class BaseAPI < RequestBody , ResponseBody , StreamChunk = JsonData >
4950 implements GenAIAttributesExtractor < RequestBody , ResponseBody > , SafeExtractor < RequestBody , ResponseBody , StreamChunk >
5051{
52+ private usageProvider : UsageProvider | undefined
53+
5154 /** @apiFlavor : the flavor of the API, used to determine the response model and usage */
5255 apiFlavor : string | undefined = undefined
5356
@@ -57,9 +60,10 @@ export abstract class BaseAPI<RequestBody, ResponseBody, StreamChunk = JsonData>
5760 extractedRequest : ExtractedRequest = { }
5861 extractedResponse : Partial < ExtractedResponse > = { }
5962
60- constructor ( providerId : ProviderID , requestModel ?: string ) {
63+ constructor ( providerId : ProviderID , requestModel ?: string , options ?: { usageProvider ?: UsageProvider } ) {
6164 this . providerId = providerId
6265 this . requestModel = requestModel
66+ this . usageProvider = options ?. usageProvider
6367 }
6468
6569 requestExtractors : ExtractorConfig < RequestBody , ExtractedRequest > = { }
@@ -86,7 +90,7 @@ export abstract class BaseAPI<RequestBody, ResponseBody, StreamChunk = JsonData>
8690 }
8791
8892 extractUsage ( responseBody : ResponseBody | StreamChunk ) : Usage | undefined {
89- const provider = findProvider ( { providerId : this . providerId } )
93+ const provider = this . usageProvider ?? findProvider ( { providerId : this . providerId } )
9094 // This should never happen because we know the provider ID is valid, but we will throw an error to be safe.
9195 if ( ! provider ) throw new Error ( `Provider not found for provider ID: ${ this . providerId } ` )
9296 const { usage } = extractUsage ( provider , responseBody , this . apiFlavor )
0 commit comments