Installation
Overview
The@ai-billing/groq package provides middleware for tracking token usage and calculating costs when using Groq models with the Vercel AI SDK.
Usage
To use the middleware, wrap your Groq model usingwrapLanguageModel from the ai package and pass the createGroqMiddleware.
1
Initialize the Groq provider
First, set up the Groq 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 Groq models.
3
Create the billing middleware
Initialize the Groq 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 Groq 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.