Skip to main content
@ai-billing/cohere
@ai-billing/cohere / createCohereV3Middleware

Function: createCohereV3Middleware()

createCohereV3Middleware<TTags>(options): LanguageModelV3Middleware
Defined in: cohere/src/ai-sdk/language-model-middleware/v3/language-model-v3-cohere-billing-middleware.ts:107 Creates a V3 billing middleware for the Cohere provider (@ai-sdk/cohere). Bills off billed_units.input_tokens/billed_units.output_tokens (falling back to tokens.input_tokens/ tokens.output_tokens only when billed_units is entirely absent from the payload) — not the raw tokens.* totals — since Cohere only charges for billed_units. cached_tokens is surfaced on the emitted event’s usage.cacheReadTokens for observability, but is not subtracted from the billed prompt total (unlike Mistral) because billed_units already nets out cache hits in a way that isn’t reproducible by subtracting cached_tokens from tokens.input_tokens. As a result, usage.inputTokens/ usage.outputTokens on the emitted BillingEvent reflect Cohere’s billed token counts, which can be substantially smaller than the AI SDK’s own normalized inputTokens/outputTokens totals when a request hits Cohere’s inference cache.

Type Parameters

TTags

TTags extends JSONObject The shape of the tags object, extending DefaultTags.

Parameters

options

CohereV3MiddlewareOptions<TTags> Billing options; see CohereV3MiddlewareOptions. A priceResolver is required.

Returns

LanguageModelV3Middleware A V3 billing middleware instance for Cohere.

Example