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 usingwrapLanguageModel from the ai package and pass the createGatewayMiddleware.
Initialize the Gateway provider
First, set up the Vercel AI Gateway provider using your Gateway ID and API key.
Create the billing middleware
Initialize the Gateway billing middleware. You need to provide a destination (such as
consoleDestination) where billing events will be sent.Wrap the model
Use
wrapLanguageModel from the ai package to apply the billing middleware to your chosen model routed through the gateway.