Skip to main content

Installation

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

Initialize the Moonshot AI provider

First, set up the provider using your API key.
2

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).
3

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

Wrap the model

Use wrapLanguageModel from the ai package to apply the billing middleware to your Moonshot AI model.
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 metrics, and calculate costs.