> ## Documentation Index
> Fetch the complete documentation index at: https://narev.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# CreateHuggingfaceV4Middleware

[**@ai-billing/huggingface**](../index)

***

[@ai-billing/huggingface](/docs/sdk/ai-billing/reference/huggingface/typedoc/index) / createHuggingfaceV4Middleware

# Function: createHuggingfaceV4Middleware()

> **createHuggingfaceV4Middleware**\<`TTags`>(`options`): `LanguageModelV4Middleware`

Defined in: [huggingface/src/ai-sdk/language-model-middleware/v4/language-model-v4-huggingface-billing-middleware.ts:92](https://github.com/narevai/ai-billing/blob/main/packages/huggingface/src/ai-sdk/language-model-middleware/v4/language-model-v4-huggingface-billing-middleware.ts#L92)

Creates a V4 billing middleware for the Hugging Face provider (`@ai-sdk/huggingface`).
Deducts cache-read tokens from prompt tokens before billing — Hugging Face charges only non-cached input
at the prompt rate, and cached tokens separately at the cache-read rate.

## Type Parameters

### TTags

`TTags` *extends* `JSONObject`

The shape of the tags object, extending DefaultTags.

## Parameters

### options

[`HuggingfaceV4MiddlewareOptions`](/docs/sdk/ai-billing/reference/huggingface/typedoc/interfaces/HuggingfaceV4MiddlewareOptions)\<`TTags`>

Billing options; see [HuggingfaceV4MiddlewareOptions](/docs/sdk/ai-billing/reference/huggingface/typedoc/interfaces/HuggingfaceV4MiddlewareOptions). A `priceResolver` is required.

## Returns

`LanguageModelV4Middleware`

A V4 billing middleware instance for Hugging Face.

## Example

```ts theme={null}
import { createHuggingFace } from '@ai-sdk/huggingface';
import { wrapLanguageModel } from 'ai';
import { createHuggingfaceV4Middleware } from '@ai-billing/huggingface';
import {
  consoleDestination,
  createObjectPriceResolver,
  type ModelPricing,
} from '@ai-billing/core';

const huggingFace = createHuggingFace({ apiKey: process.env.HUGGINGFACE_API_KEY });

const customPricingMap: Record<string, ModelPricing> = {
  'meta-llama/Llama-3.1-8B-Instruct': {
    promptTokens: 0.05 / 1_000_000,
    completionTokens: 0.15 / 1_000_000,
  },
};

const priceResolver = createObjectPriceResolver(customPricingMap);

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

const wrappedModel = wrapLanguageModel({
  model: huggingFace('meta-llama/Llama-3.1-8B-Instruct'),
  middleware: billingMiddleware,
});
```
