Skip to main content

Installation

npm install @ai-billing/groq @ai-billing/core @ai-sdk/groq ai

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 using wrapLanguageModel from the ai package and pass the createGroqMiddleware.
1

Initialize the Groq provider

First, set up the Groq provider using your API key.
import { createGroq } from '@ai-sdk/groq';

const groq = createGroq({
  apiKey: process.env.GROQ_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.
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.
import { createGroqMiddleware } from '@ai-billing/groq';
import { consoleDestination } from '@ai-billing/core';

const billingMiddleware = createGroqMiddleware({
  destinations: [consoleDestination()],
  priceResolver: priceResolver,
});
4

Wrap the model

Use wrapLanguageModel from the ai package to apply the billing middleware to your Groq model.
import { wrapLanguageModel } from 'ai';

const wrappedModel = wrapLanguageModel({
  model: groq('llama-3.3-70b-versatile'), // or 'openai/gpt-oss-120b'
  middleware: billingMiddleware,
});
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.
import { generateText } from 'ai';

const result = await generateText({
  model: wrappedModel,
  prompt: 'What is the capital of Sweden?',
});