> ## 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 Alibaba middleware for tracking AI costs.

## Installation

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

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

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

## Overview

The `@ai-billing/alibaba` package provides middleware for tracking token usage and calculating costs when using Alibaba Cloud DashScope (Qwen) models with the Vercel AI SDK.

It captures usage straight from DashScope's OpenAI-compatible chat-completions payload, including cached prompt tokens (`prompt_tokens_details.cached_tokens`) and, for Qwen models running in thinking mode, reasoning tokens (`completion_tokens_details.reasoning_tokens`).

## Usage

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

**1. Initialize the Alibaba provider**

First, set up the provider using the `@ai-sdk/alibaba` package and your API key.

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

const alibaba = createAlibaba({
  apiKey: process.env.ALIBABA_API_KEY,
});
```

**2. Define model pricing**

Set up a price resolver to define the costs for the models you'll be using. For Alibaba, you can specify costs for standard prompt/completion tokens, cached prompt tokens, and reasoning tokens.

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

const priceResolver = createObjectPriceResolver({
  'qwen-plus': {
    promptTokens: 0.8 / 1_000_000, // $0.80 per 1M tokens
    completionTokens: 2.0 / 1_000_000, // $2.00 per 1M tokens
    inputCacheReadTokens: 0.4 / 1_000_000, // discounted cache-read rate
  },
});
```

**3. Create the billing middleware**

Initialize the Alibaba 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 { createAlibabaMiddleware } from '@ai-billing/alibaba';
import { consoleDestination } from '@ai-billing/core';

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

**4. Wrap the model**

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

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

const wrappedModel = wrapLanguageModel({
  model: alibaba('qwen-plus'),
  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 cached and reasoning tokens, 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.