Skip to main content

Installation

Overview

The @ai-billing/huggingface package provides middleware for tracking token usage and calculating costs when using Hugging Face’s Inference Providers with the Vercel AI SDK. Hugging Face’s Inference Providers go through OpenAI’s Responses API shape (not Chat Completions), so usage is reported as input_tokens/output_tokens totals, with input_tokens_details.cached_tokens and output_tokens_details.reasoning_tokens as subsets of those totals. The middleware deducts cached and reasoning tokens from the base prompt/completion counts before billing, so you aren’t charged twice for the same tokens.

Usage

To use the middleware, wrap your Hugging Face model using wrapLanguageModel from the ai package and pass the createHuggingfaceMiddleware.
1

Initialize the Hugging Face provider

First, set up the provider using @ai-sdk/huggingface with your API key.
2

Define model pricing

Set up a price resolver to define the costs for the models you’ll be using.
3

Create the billing middleware

Initialize the Hugging Face 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 Hugging Face 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, deduct cached/reasoning tokens, and calculate costs.