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

Function: createCohereV4Middleware()

createCohereV4Middleware<TTags>(options): LanguageModelV4Middleware
Defined in: cohere/src/ai-sdk/language-model-middleware/v4/language-model-v4-cohere-billing-middleware.ts:107 Creates a V4 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

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

Returns

LanguageModelV4Middleware A V4 billing middleware instance for Cohere.

Example