> ## 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.

# CreateTogetheraiV4Middleware

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

***

[@ai-billing/togetherai](/docs/sdk/ai-billing/reference/togetherai/typedoc/index) / createTogetheraiV4Middleware

# Function: createTogetheraiV4Middleware()

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

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

Creates a V4 billing middleware for the Together AI provider (`@ai-sdk/togetherai`).
Together AI's usage payload does not report any cache fields, so cache-read/cache-write tokens are
always billed as zero; `reasoning_tokens` is read as a flat top-level field (not nested, unlike some
other OpenAI-compatible providers in this repo) and deducted from completion tokens before billing.

## Type Parameters

### TTags

`TTags` *extends* `JSONObject`

The shape of the tags object, extending DefaultTags.

## Parameters

### options

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

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

## Returns

`LanguageModelV4Middleware`

A V4 billing middleware instance for Together AI.

## Example

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

const togetherai = createTogetherAI({ apiKey: process.env.TOGETHER_API_KEY });

const customPricingMap: Record<string, ModelPricing> = {
  'openai/gpt-oss-20b': {
    promptTokens: 0.05 / 1_000_000,
    completionTokens: 0.2 / 1_000_000,
  },
};

const priceResolver = createObjectPriceResolver(customPricingMap);

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

const wrappedModel = wrapLanguageModel({
  model: togetherai('openai/gpt-oss-20b'),
  middleware: billingMiddleware,
});
```
