> ## 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.

# Getting started

> First steps with Moonshot AI (Kimi) middleware for tracking AI costs.

## Installation

<CodeGroup>
  ```bash npm theme={null}
  npm install @ai-billing/moonshotai @ai-billing/core @ai-sdk/moonshotai ai
  ```

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

  ```bash yarn theme={null}
  yarn add @ai-billing/moonshotai @ai-billing/core @ai-sdk/moonshotai ai
  ```
</CodeGroup>

## Overview

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

It captures Moonshot-specific metrics, such as `reasoningTokens`, ensuring that reasoning costs are accurately reflected. Reasoning models such as `kimi-k3` always reason before answering, and Moonshot's usage payload reports reasoning tokens as a subset of the completion tokens — the middleware splits reasoning tokens out of the completion total before billing, so reasoning is never double-counted.

## Usage

To use the middleware, wrap your Moonshot AI model using `wrapLanguageModel` from the `ai` package and pass the `createMoonshotaiMiddleware`.

<Steps>
  <Step title="Initialize the Moonshot AI provider">
    First, set up the provider using your API key.

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

    const moonshotai = createMoonshotAI({
      apiKey: process.env.MOONSHOT_API_KEY,
    });
    ```
  </Step>

  <Step title="Define model pricing">
    Set up a price resolver to define the costs for the models you'll be using. For Moonshot AI's `kimi-k3`, you can specify costs for prompt/completion tokens, cache-read tokens (`inputCacheReadTokens`), and reasoning tokens (`internalReasoningTokens`).

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

    const priceResolver = createObjectPriceResolver({
      'kimi-k3': {
        promptTokens: 3.0 / 1_000_000, // $3.00 per 1M tokens
        completionTokens: 15.0 / 1_000_000, // $15.00 per 1M tokens
        inputCacheReadTokens: 0.3 / 1_000_000, // $0.30 per 1M tokens
        internalReasoningTokens: 15.0 / 1_000_000, // billed at the same flat rate as completion tokens
      },
    });
    ```
  </Step>

  <Step title="Create the billing middleware">
    Initialize the Moonshot 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 { createMoonshotaiMiddleware } from '@ai-billing/moonshotai';
    import { consoleDestination } from '@ai-billing/core';

    const billingMiddleware = createMoonshotaiMiddleware({
      destinations: [consoleDestination()],
      priceResolver: priceResolver,
    });
    ```
  </Step>

  <Step title="Wrap the model">
    Use `wrapLanguageModel` from the `ai` package to apply the billing middleware to your Moonshot AI model.

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

    const wrappedModel = wrapLanguageModel({
      model: moonshotai('kimi-k3'),
      middleware: billingMiddleware,
    });
    ```
  </Step>

  <Step title="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 metrics, and calculate costs.

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

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