Skip to main content

Installation

Overview

The @ai-billing/perplexity package provides middleware for tracking token usage and calculating costs when using Perplexity models (the sonar family) with the Vercel AI SDK. All Perplexity sonar models are web-search-grounded and bill a flat per-request search fee (request_cost) on top of token costs. In reality, that fee also varies by search_context_size (low/medium/high) — this middleware models it as a single flat request rate per resolved model, the same simplification used for other “flat request fee” providers. If you need to bill different search_context_size tiers differently, resolve a distinct modelId per tier in your priceResolver. Perplexity has no prompt-caching feature today, so cache-read/cache-write tokens are always reported as 0. Reasoning models (sonar-reasoning, sonar-reasoning-pro, sonar-deep-research) report reasoning_tokens separately, which you can price with internalReasoningTokens.

Usage

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

Initialize the Perplexity provider

First, set up the provider using @ai-sdk/perplexity and your API key.
2

Define model pricing

Set up a price resolver to define the costs for the models you’ll be using. For Perplexity, you can specify a flat per-request search fee (request) in addition to prompt/completion token rates, and optionally internalReasoningTokens for reasoning models.
3

Create the billing middleware

Initialize the Perplexity 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 Perplexity 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, count returned sources/citations as webSearchCount, and calculate costs.