Installation
Overview
The@ai-billing/gmicloud package provides middleware for tracking token usage and calculating costs when using GMI Cloud models with the Vercel AI SDK.
It captures GMI Cloud-specific metrics, such as completion_tokens_details.reasoning_tokens and prompt_tokens_details.cached_tokens, ensuring that reasoning and prompt-caching costs are accurately reflected.
Usage
To use the middleware, wrap your GMI Cloud model usingwrapLanguageModel from the ai package and pass the createGmicloudMiddleware.
1
Initialize the GMI Cloud provider
First, set up the GMI Cloud 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 GMI Cloud, you can specify costs for standard prompt/completion tokens and reasoning tokens.
3
Create the billing middleware
Initialize the GMI Cloud 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 GMI Cloud 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 and caching metrics, and calculate costs.