Installation
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 usingwrapLanguageModel 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.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.
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.4
Wrap the model
Use
wrapLanguageModel from the ai package to apply the billing middleware to your Alibaba 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 cached and reasoning tokens, and calculate costs.