Installation
Overview
The@ai-billing/cohere package provides middleware for tracking token usage and calculating costs when using Cohere models with the Vercel AI SDK.
Cohere’s usage payload reports three separate counters that do not reconcile with each other by
subtraction: a raw tokens.{input_tokens,output_tokens} total (which includes cache hits and
Cohere-injected framework tokens), the actually-charged billed_units.{input_tokens,output_tokens}, and an
informational cached_tokens counter. This middleware always computes cost from billed_units (falling
back to tokens.* only if billed_units is entirely absent from the response), so a heavily-cached
request is never over-billed. As a result, usage.inputTokens/usage.outputTokens on the emitted billing
event reflect Cohere’s billed token counts, which can be smaller than the AI SDK’s own normalized usage
totals. cacheReadTokens is still populated (from cached_tokens) for observability, even though Cohere
does not publish a discounted cached-input rate and this field does not affect cost by default.
Usage
To use the middleware, wrap your Cohere model usingwrapLanguageModel from the ai package and pass the createCohereMiddleware.
1
Initialize the Cohere provider
First, set up the provider using the Cohere SDK 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 Cohere, you can specify costs for standard prompt/completion tokens; cache-read and reasoning rates are optional and default to zero.
3
Create the billing middleware
Initialize the Cohere 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 Cohere model.5
Use the wrapped model
Finally, use the wrapped model with AI SDK functions like
generateText or streamText. The billing middleware will automatically bill off Cohere’s billed_units and calculate costs.