Installation
Overview
The@ai-billing/cerebras package provides middleware for tracking token usage and calculating costs when using Cerebras models with the Vercel AI SDK.
Usage
To use the middleware, wrap your Cerebras model usingwrapLanguageModel from the ai package and pass the createCerebrasMiddleware.
1
Initialize the Cerebras provider
First, set up the Cerebras 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. You can use either the dynamic Narev price resolver or a manual object price resolver.
- Using Narev Pricing (Recommended)
- Using Manual Pricing
The Narev price resolver automatically fetches the latest pricing for Cerebras models.
3
Create the billing middleware
Initialize the Cerebras 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 Cerebras 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 and calculate costs.