Skip to main content

Installation

Overview

The @ai-billing/togetherai package provides middleware for tracking token usage and calculating costs when using Together AI models with the Vercel AI SDK. Together AI’s usage payload is a flat, OpenAI-compatible block (prompt_tokens, completion_tokens, total_tokens, reasoning_tokens) with no cache fields, so cache-read and cache-write tokens are always billed as zero.

Usage

To use the middleware, wrap your Together AI model using wrapLanguageModel from the ai package and pass the createTogetheraiMiddleware.
1

Initialize the Together AI provider

First, set up the Together AI provider using 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 Together AI 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 Together AI 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 and calculate costs.