Skip to main content

Installation

npm install @ai-billing/anthropic @ai-billing/core @ai-sdk/anthropic ai

Overview

The @ai-billing/anthropic package provides middleware for tracking token usage and calculating costs when using Anthropic models with the Vercel AI SDK. It captures Anthropic-specific metrics, such as inputCacheReadTokens and inputCacheWriteTokens, ensuring that Prompt Caching costs are accurately reflected.

Usage

To use the middleware, wrap your Anthropic model using wrapLanguageModel from the ai package and pass the createAnthropicMiddleware.
1

Initialize the Anthropic provider

First, set up the Anthropic provider using your API key.
import { createAnthropic } from '@ai-sdk/anthropic';

const anthropic = createAnthropic({
  apiKey: process.env.ANTHROPIC_API_KEY,
});
2

Define model pricing

Set up a price resolver to define the costs for the models you’ll be using. For Anthropic, you can specify costs for both standard prompt/completion tokens and cached tokens (inputCacheReadTokens and inputCacheWriteTokens).
import { createObjectPriceResolver } from '@ai-billing/core';

const priceResolver = createObjectPriceResolver({
  'claude-sonnet-4-6': {
    promptTokens: 0.000003,
    completionTokens: 0.000015,
    inputCacheReadTokens: 0.0000003,
    inputCacheWriteTokens: 0.00000375,
  },
});
3

Create the billing middleware

Initialize the Anthropic billing middleware. You need to provide a destination (such as consoleDestination) where billing events will be sent, along with your priceResolver.
import { createAnthropicMiddleware } from '@ai-billing/anthropic';
import { consoleDestination } from '@ai-billing/core';

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

Wrap the model

Use wrapLanguageModel from the ai package to apply the billing middleware to your Anthropic model.
import { wrapLanguageModel } from 'ai';

const wrappedModel = wrapLanguageModel({
  model: anthropic('claude-sonnet-4-6'),
  middleware: billingMiddleware,
});
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, handle caching metrics, and calculate costs.
import { generateText } from 'ai';

const result = await generateText({
  model: wrappedModel,
  prompt: 'What is the capital of Sweden?',
});