Skip to main content
@ai-billing/google
@ai-billing/google / createGoogleV3Middleware

Function: createGoogleV3Middleware()

createGoogleV3Middleware<TTags>(options): LanguageModelV3Middleware
Defined in: google/src/ai-sdk/language-model-middleware/v3/language-model-v3-google-billing-middleware.ts:97 Creates a V3 billing middleware for the Google provider (@ai-sdk/google). Maps AI SDK usage into billing fields and resolves cost from pricing plus usage.

Type Parameters

TTags

TTags extends JSONObject The shape of the tags object, extending DefaultTags.

Parameters

options

GoogleV3MiddlewareOptions<TTags> Billing options; see GoogleV3MiddlewareOptions.

Returns

LanguageModelV3Middleware A V3 billing middleware instance for Google.

Example

Same wiring as examples/dev-sandbox/app/api/google (createGoogleV3Middleware is this function’s export alias from @ai-billing/google).
import { createGoogleGenerativeAI } from '@ai-sdk/google';
import { wrapLanguageModel } from 'ai';
import { createGoogleV3Middleware } from '@ai-billing/google';
import {
  consoleDestination,
  createObjectPriceResolver,
  type ModelPricing,
} from '@ai-billing/core';

const google = createGoogleGenerativeAI({ apiKey: process.env.GOOGLE_AI_STUDIO_KEY });

const customPricingMap: Record<string, ModelPricing> = {
 'models/gemini-3.1-flash-lite-preview': {
   promptTokens: 0.00000025, // $0.25 per 1M tokens
   completionTokens: 0.0000015, // $1.50 per 1M tokens
   inputCacheReadTokens: 0.000000025, // $0.025 per 1M tokens
   internalReasoningTokens: 0.0000015, // $1.50 per 1M tokens
 },
};

const priceResolver = createObjectPriceResolver(customPricingMap);

const billingMiddleware = createGoogleV3Middleware({
  destinations: [consoleDestination()],
  priceResolver,
});

const wrappedModel = wrapLanguageModel({
  model: google('models/gemini-3.1-flash-lite-preview'),
  middleware: billingMiddleware,
});