Skip to main content

Installation

Overview

The @ai-billing/azure package provides middleware for tracking token usage and calculating costs when using Azure-hosted models with the Vercel AI SDK, via @ai-sdk/azure. This package targets Azure AI Foundry (*.services.ai.azure.com), not classic Azure OpenAI (*.openai.azure.com). The AI SDK routes Foundry deployments through the Responses API (provider: "azure.responses"), whose raw usage payload uses input_tokens/output_tokens field names (and input_tokens_details.{cached_tokens,cache_write_tokens} / output_tokens_details.reasoning_tokens) rather than classic Chat Completions’ prompt_tokens/completion_tokens naming. The middleware reads those Responses-API field names when present, and falls back to the AI SDK’s normalized usage fields otherwise, so it also works against classic Azure OpenAI resources. It captures Foundry-specific metrics, such as inputCacheReadTokens, inputCacheWriteTokens, and internalReasoningTokens, ensuring that prompt-caching and reasoning costs are accurately reflected.

Usage

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

Initialize the Azure provider

Set up the Azure provider against your AI Foundry resource. Foundry resources are reached with a baseURL + apiVersion pair (not the classic resourceName option), and models are addressed by their account-specific deployment name.
2

Define model pricing

Set up a price resolver to define the costs for your deployments. Because Foundry deployment names are account-specific (not a stable public model id), key the pricing map dynamically off the same deployment name rather than a hardcoded literal.
3

Create the billing middleware

Initialize the Azure 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 Azure 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, handle caching and reasoning metrics, and calculate costs.