PhoenixKitAI.AnthropicPricing (PhoenixKitAI v0.26.1)

Copy Markdown View Source

What a chat call to Anthropic cost, from its token counts and a rate table.

Anthropic's OpenAI-compatible layer returns prompt_tokens and completion_tokens but no cost (OpenRouter puts usage.cost in its answer), and its GET /v1/models lists no prices. So the cost comes from the per-model rates of Anthropic's pricing page, in US dollars per million tokens. Thinking is billed as output and is already in completion_tokens. That layer does no prompt caching, so the cache rates do not apply.

Rates as of 2026-10-22, from https://platform.claude.com/docs/en/about-claude/pricing — they drift. A host can add or correct rates without a release:

config :phoenix_kit_ai, :anthropic_prices, %{
  "claude-sonnet-5-5" => {2.0, 10.0}
}

A model is looked up by its id without a trailing date (claude-sonnet-4-5-20250929 → claude-sonnet-4-5). A model that is in neither table has no price: its calls are logged without a cost, as before.

The table is the last source of a call's cost (PhoenixKitAI.CallCost): after the provider's own cost, a catalogue price and the endpoint's manual price — so a manual price corrects a stale rate. It is never shown as a catalogue price. Ported from BeamLabEU/phoenix_kit_ai#47 (8e8c65e). Not covered: US-only inference (inference_geo, ×1.1), which this module never asks for, and the Batch API.

Summary

Functions

The cost of a call in US dollars, or nil when the model has no price here or the token counts are not numbers.

The rate for a call of prompt_tokens input tokens in US dollars per 1M tokens (%{"input" => Decimal, "output" => Decimal}, the shape of a manual price), or nil when the model has no price here.

The rate of a model as {input, output} US dollars per token, or nil when the model has no price here — the shape AIModel.pricing and the capability cache keep. A tiered model gives its base rate.

Functions

cost(model, prompt_tokens, completion_tokens)

@spec cost(String.t() | nil, term(), term()) :: float() | nil

The cost of a call in US dollars, or nil when the model has no price here or the token counts are not numbers.

per_million(model, prompt_tokens)

@spec per_million(String.t() | nil, non_neg_integer()) ::
  %{required(String.t()) => Decimal.t()} | nil

The rate for a call of prompt_tokens input tokens in US dollars per 1M tokens (%{"input" => Decimal, "output" => Decimal}, the shape of a manual price), or nil when the model has no price here.

per_token(model)

@spec per_token(String.t() | nil) :: {float(), float()} | nil

The rate of a model as {input, output} US dollars per token, or nil when the model has no price here — the shape AIModel.pricing and the capability cache keep. A tiered model gives its base rate.