Skip to content

Opt-in genai-prices pricing for non-Anthropic models#9

Open
dsfaccini wants to merge 1 commit into
mainfrom
feat/genai-prices-fallback
Open

Opt-in genai-prices pricing for non-Anthropic models#9
dsfaccini wants to merge 1 commit into
mainfrom
feat/genai-prices-fallback

Conversation

@dsfaccini

Copy link
Copy Markdown

David's AICA here:

Problem

The cost table (MODEL_PRICING) only covers Anthropic models (opus/sonnet/haiku, matched by substring). When Claude Code is pointed at an Anthropic-compatible endpoint via ANTHROPIC_BASE_URL — z.ai/GLM, Moonshot/Kimi, MiniMax, etc. — the model name matches nothing, get_model_prices returns None, and operation.cost is omitted. Token usage is recorded, but there's no cost.

Change

Adds an opt-in fallback to genai-prices for any non-Anthropic model:

  • Off by default. Enabled with LOGFIRE_GENAI_PRICES=true.
  • Lazily imported — users install genai-prices themselves into the Python that runs the hooks. The default install stays stdlib-only and offline; hooks.json, pyproject.toml, and the invocation are unchanged.
  • If the flag is set but the package isn't importable, cost is omitted with a single diagnostic — never errors.
  • Provider label (gen_ai.provider.name/gen_ai.system) reflects the resolved provider (e.g. moonshotai, minimax) instead of hardcoded anthropic.

Token mapping note

genai-prices treats Usage.input_tokens as the inclusive total, with cache_write_tokens/cache_read_tokens as subsets billed at their own rates — the inverse of Anthropic's convention (where input_tokens is uncached-only and cache tokens are additional). The patch passes the combined input (raw_input + cache_creation + cache_read) and lets genai-prices split it, so cached tokens are priced correctly.

Testing

Verified the full matrix via the actual span-builder (build_child_spans_from_calls):

state opus (built-in table) glm-5.2 / kimi-k2.7-code
default (flag unset) priced omitted (stdlib, offline)
flag set, genai-prices not installed priced omitted, graceful diagnostic
flag set + installed priced $0.003034 / $0.00295
installed but flag unset priced omitted (opt-in respected)

Anthropic pricing path is byte-for-byte unchanged. ruff check clean (no new findings).

The built-in cost table only covers Anthropic models (opus/sonnet/haiku), so
sessions run against an Anthropic-compatible endpoint via ANTHROPIC_BASE_URL
(z.ai/GLM, Moonshot/Kimi, MiniMax, ...) get no operation.cost.

Setting LOGFIRE_GENAI_PRICES=true prices any model genai-prices knows, matched
by model name. It is opt-in and lazily imported: users install genai-prices
themselves into the hooks' Python, so the default install stays stdlib-only and
offline. If the flag is set but the package is missing, cost is simply omitted
(with a diagnostic) rather than erroring.

genai-prices' Usage.input_tokens is the inclusive total (cache tokens are
subsets billed at their own rates), the inverse of Anthropic's convention, so
the combined input is passed and genai-prices splits it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant