Skip to main content

Installation

Overview

The @ai-billing/amazon-bedrock package provides middleware for tracking token usage and calculating costs when using Amazon Bedrock models with the Vercel AI SDK. Amazon Bedrock’s Converse API reports usage with inputTokens/outputTokens field names (camelCase, no underscore) instead of the prompt_tokens/completion_tokens naming used by OpenAI-style providers. Bedrock’s inputTokens also excludes cache-read and cache-write tokens — they are reported separately as cacheReadInputTokens/cacheWriteInputTokens — so billing adds cache costs on top of the full prompt cost instead of deducting them, the same accounting style used by the Anthropic middleware.

Usage

To use the middleware, wrap your Amazon Bedrock model using wrapLanguageModel from the ai package and pass the createAmazonBedrockMiddleware.
1

Initialize the Amazon Bedrock provider

First, set up the provider using @ai-sdk/amazon-bedrock with your AWS credentials and region.
2

Define model pricing

Set up a price resolver to define the costs for the models you’ll be using. Note that a cross-region inference profile id (e.g. us.amazon.nova-lite-v1:0) is billed at its own rate, distinct from the bare model id, so key your pricing map by the exact id you invoke.
3

Create the billing middleware

Initialize the Amazon Bedrock 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 Amazon Bedrock 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.