UNPKG

@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/)

60 lines (54 loc) 1.63 kB
import type { LanguageModelV3Usage } from '@ai-sdk/provider'; export type GoogleGenerativeAITokenDetail = { modality: string; tokenCount: number; }; export type GoogleGenerativeAIUsageMetadata = { promptTokenCount?: number | null; candidatesTokenCount?: number | null; totalTokenCount?: number | null; cachedContentTokenCount?: number | null; thoughtsTokenCount?: number | null; trafficType?: string | null; serviceTier?: string | null; promptTokensDetails?: GoogleGenerativeAITokenDetail[] | null; candidatesTokensDetails?: GoogleGenerativeAITokenDetail[] | null; }; export function convertGoogleGenerativeAIUsage( usage: GoogleGenerativeAIUsageMetadata | 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 promptTokens = usage.promptTokenCount ?? 0; const candidatesTokens = usage.candidatesTokenCount ?? 0; const cachedContentTokens = usage.cachedContentTokenCount ?? 0; const thoughtsTokens = usage.thoughtsTokenCount ?? 0; return { inputTokens: { total: promptTokens, noCache: promptTokens - cachedContentTokens, cacheRead: cachedContentTokens, cacheWrite: undefined, }, outputTokens: { total: candidatesTokens + thoughtsTokens, text: candidatesTokens, reasoning: thoughtsTokens, }, raw: usage, }; }