cadrumo.adapters.outbound.llm.pricing module

Provider pricing helpers for cost estimation.

Holds the public per-million-token pricing table for each supported llm.LLMProvider and exposes estimate_cost_usd() so llm.LLMClient can stamp every llm.LLMResponse with a deterministic USD cost estimate. The local provider always reports zero.

estimate_cost_usd(provider, model, input_tokens, output_tokens)[source]

Estimate request cost in USD from known public pricing, or refuse.

Looks up the first pricing entry whose model field is a prefix of the lowercased model argument so model variants (versioned suffixes, dated minor releases) inherit their family’s rates.

A model this table does not price returns ``None``, never zero. It used to return zero so every usage record carried a well-typed Decimal, and that is exactly how a cost surface reports an absence as a positive answer: a caller budgeting from $0.0000 concludes the call was free rather than unpriced. The table is a snapshot of public rates and goes stale by construction, so the models it does not carry are a permanent, moving population – widening it cannot fix this and re-introduces the silent zero for whatever is added next.

Free and unpriceable stay distinct. The local provider genuinely costs nothing and keeps returning Decimal('0'). Collapsing the two would put the defect back under a different name.

Parameters:
  • provider (LLMProvider) – The llm.LLMProvider that produced the response.

  • model (str) – Provider-resolved model identifier (e.g. "claude-sonnet-4-6-20260101").

  • input_tokens (int) – Prompt-side token count.

  • output_tokens (int) – Completion-side token count.

Return type:

Decimal | None

Returns:

Estimated cost in USD, quantised to 6 decimal places with decimal.ROUND_HALF_UP – half away from zero, matching how this tree rounds money everywhere else, and distinct from banker’s rounding (decimal.ROUND_HALF_EVEN), which resolves a tie to the even digit instead; decimal.Decimal('0') for the local provider, which is free rather than unpriced; and None when no table entry prices this provider and model.