Skip to main content

Installation

Overview

The @ai-billing/gateway package provides middleware for tracking token usage and capturing events when using the Vercel AI Gateway with the Vercel AI SDK. It seamlessly integrates with the createGateway provider from the ai package, allowing you to monitor and route requests across different language models while capturing their metrics.

Usage

To use the middleware, wrap your gateway model using wrapLanguageModel from the ai package and pass the createGatewayMiddleware.
1

Initialize the Gateway provider

First, set up the Vercel AI Gateway provider using your Gateway ID and API key.
2

Create the billing middleware

Initialize the Gateway billing middleware. You need to provide a destination (such as consoleDestination) where billing events will be sent.
3

Wrap the model

Use wrapLanguageModel from the ai package to apply the billing middleware to your chosen model routed through the gateway.
4

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 usage across the gateway models.