> ## Documentation Index
> Fetch the complete documentation index at: https://narev.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# CreateZaiV4Middleware

[**@ai-billing/zai**](../index)

***

[@ai-billing/zai](/docs/sdk/ai-billing/reference/zai/typedoc/index) / createZaiV4Middleware

# Function: createZaiV4Middleware()

> **createZaiV4Middleware**\<`TTags`>(`options`): `LanguageModelV4Middleware`

Defined in: [zai/src/ai-sdk/language-model-middleware/v4/language-model-v4-zai-billing-middleware.ts:87](https://github.com/narevai/ai-billing/blob/main/packages/zai/src/ai-sdk/language-model-middleware/v4/language-model-v4-zai-billing-middleware.ts#L87)

Creates a V4 billing middleware for the Z.ai provider (`@ai-sdk/zai`).
Deducts cache-read tokens from prompt tokens and reasoning tokens from completion tokens before
billing — reasoning tokens are a subset of `completion_tokens` (GLM has no separate reasoning rate), and
cached tokens are billed separately at the cache-read rate.

## Type Parameters

### TTags

`TTags` *extends* `JSONObject`

The shape of the tags object, extending DefaultTags.

## Parameters

### options

[`ZaiV4MiddlewareOptions`](/docs/sdk/ai-billing/reference/zai/typedoc/interfaces/ZaiV4MiddlewareOptions)\<`TTags`>

Billing options; see [ZaiV4MiddlewareOptions](/docs/sdk/ai-billing/reference/zai/typedoc/interfaces/ZaiV4MiddlewareOptions). A `priceResolver` is required.

## Returns

`LanguageModelV4Middleware`

A V4 billing middleware instance for Z.ai.

## Example

```ts theme={null}
import { createZai } from '@ai-sdk/zai';
import { wrapLanguageModel } from 'ai';
import { createZaiV4Middleware } from '@ai-billing/zai';
import {
  consoleDestination,
  createObjectPriceResolver,
  type ModelPricing,
} from '@ai-billing/core';

const zai = createZai({ apiKey: process.env.ZAI_API_KEY });

const customPricingMap: Record<string, ModelPricing> = {
  'glm-5.3': {
    promptTokens: 1.4 / 1_000_000,
    completionTokens: 4.4 / 1_000_000,
    inputCacheReadTokens: 0.26 / 1_000_000,
  },
};

const priceResolver = createObjectPriceResolver(customPricingMap);

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

const wrappedModel = wrapLanguageModel({
  model: zai('glm-5.3'),
  middleware: billingMiddleware,
});
```
