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

# Getting started

> First steps with Amazon Bedrock middleware for tracking AI costs.

## Installation

<CodeGroup>
  ```bash npm theme={null}
  npm install @ai-billing/amazon-bedrock @ai-billing/core @ai-sdk/amazon-bedrock ai
  ```

  ```bash pnpm theme={null}
  pnpm add @ai-billing/amazon-bedrock @ai-billing/core @ai-sdk/amazon-bedrock ai
  ```

  ```bash yarn theme={null}
  yarn add @ai-billing/amazon-bedrock @ai-billing/core @ai-sdk/amazon-bedrock ai
  ```
</CodeGroup>

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

<Steps>
  <Step title="Initialize the Amazon Bedrock provider">
    First, set up the provider using `@ai-sdk/amazon-bedrock` with your AWS credentials and region.

    ```typescript theme={null}
    import { createAmazonBedrock } from '@ai-sdk/amazon-bedrock';

    const amazonBedrock = createAmazonBedrock({
      region: process.env.AWS_REGION,
    });
    ```
  </Step>

  <Step title="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.

    ```typescript theme={null}
    import { createObjectPriceResolver } from '@ai-billing/core';

    const priceResolver = createObjectPriceResolver({
      'us.amazon.nova-lite-v1:0': {
        promptTokens: 0.06 / 1_000_000, // $0.06 per 1M tokens
        completionTokens: 0.24 / 1_000_000, // $0.24 per 1M tokens
      },
    });
    ```
  </Step>

  <Step title="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`.

    ```typescript theme={null}
    import { createAmazonBedrockMiddleware } from '@ai-billing/amazon-bedrock';
    import { consoleDestination } from '@ai-billing/core';

    const billingMiddleware = createAmazonBedrockMiddleware({
      destinations: [consoleDestination()],
      priceResolver: priceResolver,
    });
    ```
  </Step>

  <Step title="Wrap the model">
    Use `wrapLanguageModel` from the `ai` package to apply the billing middleware to your Amazon Bedrock model.

    ```typescript theme={null}
    import { wrapLanguageModel } from 'ai';

    const wrappedModel = wrapLanguageModel({
      model: amazonBedrock('us.amazon.nova-lite-v1:0'),
      middleware: billingMiddleware,
    });
    ```
  </Step>

  <Step title="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.

    ```typescript theme={null}
    import { generateText } from 'ai';

    const result = await generateText({
      model: wrappedModel,
      prompt: 'What is the capital of Sweden?',
    });
    ```
  </Step>
</Steps>
