@ai-sdk/google
Version:
The **[Google Generative AI provider](https://ai-sdk.dev/providers/ai-sdk-providers/google-generative-ai)** for the [AI SDK](https://ai-sdk.dev/docs) contains language model support for the [Google Generative AI](https://ai.google/discover/generativeai/)
48 lines (44 loc) • 1.43 kB
text/typescript
import type { JSONObject, LanguageModelV3Usage } from '@ai-sdk/provider';
import type { GoogleInteractionsUsage } from './google-interactions-api';
export function convertGoogleInteractionsUsage(
usage: GoogleInteractionsUsage | undefined | null,
): LanguageModelV3Usage {
if (usage == null) {
return {
inputTokens: {
total: undefined,
noCache: undefined,
cacheRead: undefined,
cacheWrite: undefined,
},
outputTokens: {
total: undefined,
text: undefined,
reasoning: undefined,
},
raw: undefined,
};
}
const totalInput = usage.total_input_tokens ?? 0;
const totalOutput = usage.total_output_tokens ?? 0;
const totalThought = usage.total_thought_tokens ?? 0;
const totalCached = usage.total_cached_tokens ?? 0;
return {
inputTokens: {
total: usage.total_input_tokens ?? undefined,
noCache:
usage.total_input_tokens == null ? undefined : totalInput - totalCached,
cacheRead: usage.total_cached_tokens ?? undefined,
cacheWrite: undefined,
},
outputTokens: {
total:
usage.total_output_tokens == null && usage.total_thought_tokens == null
? undefined
: totalOutput + totalThought,
text: usage.total_output_tokens ?? undefined,
reasoning: usage.total_thought_tokens ?? undefined,
},
raw: usage as unknown as JSONObject,
};
}