Skip to main content

Module pricing

Module pricing 

Source
Expand description

Per-token price table keyed on each provider’s name()

§Model Pricing

Compile-time pricing data for the providers this crate runs, keyed on the exact string each one’s LlmProvider::name reports. MetricsProvider prices every call through it; a consumer that layers operator overrides on top builds its own lookup from lookup_pricing, cost_from_pricing and zero_cost_for_unpriced, which are public for that purpose.

§Cache accounting

A prompt token can bill at three rates, so the pricing entry carries the multipliers rather than a single global discount: fresh input at 1.0×, a cache read at ModelPricing::cache_read_multiplier (Anthropic 0.10×, OpenAI 0.50×, Gemini 0.25×), and a cache write at ModelPricing::cache_write_multiplier — which is a premium on Anthropic (1.25×), not a discount. Reasoning tokens bill at the output rate and are additive to the completion count, because every provider that reports them separately excludes them from it.

Counts arrive on TokenUsage, are carried through TokenCounts, and are priced by calculate_cost_for.

Structs§

LongPromptRates
The rate card a model switches to when a request’s prompt is long.
ModelPricing
Per-model pricing rates in USD per million tokens, plus the two cache multipliers that turn a raw token count into a billed one.
TokenCounts
The token counts a single LLM call reports, in the shape they bill.

Constants§

DEFAULT_CACHE_READ_RATE
Default fraction of the input rate at which a context-cache read bills.
DEFAULT_CACHE_WRITE_RATE
Default fraction of the input rate at which a context-cache write bills.
NOT_PER_TOKEN_METERED_PROVIDERS
Providers whose per-token cost is genuinely $0 by design, so a $0 billed cost is correct rather than a missing-price undercount.
PRICING_TABLE
Compile-time pricing table: (provider, model_prefix, pricing)

Functions§

calculate_cost
Calculate the cost of an LLM request using compile-time pricing.
calculate_cost_for
Calculate the cost of an LLM request from a full TokenCounts.
calculate_cost_with_cache
Calculate the cost of an LLM request, breaking the prompt into cached + fresh tokens.
cost_from_pricing
Compute the USD cost from a resolved ModelPricing + token counts. Shared by the compile-time lookup and any override path a consumer layers on top, so both sources price a call identically.
is_not_per_token_metered
True when a provider bills via flat-rate subscription or self-hosting, so a $0 per-token cost is correct rather than a missing-price undercount.
lookup_pricing
Look up pricing for a (provider, model) pair using prefix matching.
pool_family
The provider a pooled tier prices as: claude-code#2 is a second Claude account, billed like the first. See crate::pool.
zero_cost_for_unpriced
Log the resolution of a (provider, model) pair that has no PRICING_TABLE or override entry, at a severity matching why it is unpriced, and return the $0 cost all three paths fall back to.