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

# Getting started

> First steps with Z.ai (GLM) middleware for tracking AI costs.

## Installation

```bash npm theme={null}
npm install @ai-billing/zai @ai-billing/core @ai-sdk/zai ai
```

```bash pnpm theme={null}
pnpm add @ai-billing/zai @ai-billing/core @ai-sdk/zai ai
```

```bash yarn theme={null}
yarn add @ai-billing/zai @ai-billing/core @ai-sdk/zai ai
```

## Overview

The `@ai-billing/zai` package provides middleware for tracking token usage and calculating costs when using Z.ai (GLM) models with the Vercel AI SDK.

It captures GLM-specific metrics, such as `reasoningTokens` (from `completion_tokens_details.reasoning_tokens`) and cached input tokens (from `prompt_tokens_details.cached_tokens`), ensuring that reasoning and Prompt Caching costs are accurately reflected. GLM's reasoning tokens are a subset of the completion tokens count, so they are billed at the completion rate rather than a separate reasoning rate.

## Usage

To use the middleware, wrap your Z.ai model using `wrapLanguageModel` from the `ai` package and pass the `createZaiMiddleware`.

**1. Initialize the Z.ai provider**

First, set up the Z.ai provider using your API key.

```typescript theme={null}
import { createZai } from '@ai-sdk/zai';

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

**2. Define model pricing**

Set up a price resolver to define the costs for the models you'll be using. For Z.ai, you can specify costs for standard prompt/completion tokens and cached input tokens (`inputCacheReadTokens`).

```typescript theme={null}
import { createObjectPriceResolver } from '@ai-billing/core';

const priceResolver = createObjectPriceResolver({
  'glm-5.3': {
    promptTokens: 1.4 / 1_000_000, // $1.40 per 1M tokens
    completionTokens: 4.4 / 1_000_000, // $4.40 per 1M tokens
    inputCacheReadTokens: 0.26 / 1_000_000, // $0.26 per 1M tokens
  },
});
```

**3. Create the billing middleware**

Initialize the Z.ai billing middleware. You need to provide a destination (such as `consoleDestination`) where billing events will be sent, along with your `priceResolver`.

```typescript theme={null}
import { createZaiMiddleware } from '@ai-billing/zai';
import { consoleDestination } from '@ai-billing/core';

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

**4. Wrap the model**

Use `wrapLanguageModel` from the `ai` package to apply the billing middleware to your Z.ai model.

```typescript theme={null}
import { wrapLanguageModel } from 'ai';

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

**5. Use the wrapped model**

Finally, use the wrapped model with AI SDK functions like `generateText` or `streamText`. The billing middleware will automatically track tokens, handle reasoning and caching metrics, and calculate costs.

```typescript theme={null}
import { generateText } from 'ai';

const result = await generateText({
  model: wrappedModel,
  prompt: 'What is the capital of Sweden?',
});
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.