The personal finance app for everyone (by everyone)
Ruby
Move LLM prices to a config file refreshed on release (#4666)
Artem Danilov70647cb11c47
diff --git a/.github/workflows/llm-pricing.yml b/.github/workflows/llm-pricing.ymlnew file mode 100644index 0000000000..e66b9a38fc--- /dev/null+++ b/.github/workflows/llm-pricing.yml@@ -0,0 +1,71 @@+name: Refresh LLM pricing++# Opens a pull request that refreshes config/llm_pricing.yml from LiteLLM's+# price list, unless one is still open. A maintainer checks the changes against+# the providers' pricing pages before merging.+on:+ push:+ tags:+ - 'v*'+ workflow_dispatch:++concurrency:+ group: llm-pricing+ cancel-in-progress: false++jobs:+ refresh:+ if: github.repository == 'we-promise/sure'+ permissions:+ actions: write+ contents: write+ pull-requests: write+ runs-on: ubuntu-24.04+ timeout-minutes: 10+ env:+ BRANCH: automation/refresh-llm-pricing+ steps:+ - name: Check out main+ uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5.0.1+ with:+ ref: main++ - name: Set up Ruby+ uses: ruby/setup-ruby@97ecb7b512899eb71ab1bf2310a624c6f1589ac6 # v1.308.0+ with:+ ruby-version: .ruby-version++ - name: Refresh prices+ run: bin/update_llm_pricing > "$RUNNER_TEMP/llm-pricing.md"++ - name: Open the pull request+ env:+ GH_TOKEN: ${{ github.token }}+ run: |+ if git diff --quiet -- config/llm_pricing.yml; then+ echo "LLM pricing already matches LiteLLM."+ exit 0+ fi++ # One refresh PR at a time: never rewrite a branch that may be under review.+ # An API error fails the step here rather than looking like "no pull request".+ open_pr=$(gh pr list --repo "$GITHUB_REPOSITORY" --base main --head "$BRANCH" --state open \+ --json url,headRepositoryOwner \+ --jq "[.[] | select(.headRepositoryOwner.login == \"$GITHUB_REPOSITORY_OWNER\") | .url][0] // empty")+ if [[ -n "$open_pr" ]]; then+ echo "::warning::A pricing refresh pull request is still open ($open_pr). Merge or close it, then rerun this workflow."+ exit 0+ fi++ git config user.name "github-actions[bot]"+ git config user.email "github-actions[bot]@users.noreply.github.com"+ git switch -c "$BRANCH"+ git commit -m "Refresh LLM pricing from LiteLLM" -- config/llm_pricing.yml+ git push --force origin "$BRANCH"+ gh pr create --repo "$GITHUB_REPOSITORY" --base main --head "$BRANCH" \+ --title "Refresh LLM pricing from LiteLLM" --body-file "$RUNNER_TEMP/llm-pricing.md"++ # Pull requests opened with GITHUB_TOKEN do not trigger pull_request workflows.+ if ! gh workflow run pr.yml --repo "$GITHUB_REPOSITORY" --ref "$BRANCH"; then+ echo "::warning::Could not dispatch pr.yml for $BRANCH"+ fidiff --git a/app/models/llm_usage.rb b/app/models/llm_usage.rbindex a5efa10d59..c43c82135e 100644--- a/app/models/llm_usage.rb+++ b/app/models/llm_usage.rb@@ -10,194 +10,16 @@ class LlmUsage < ApplicationRecordscope :recent, -> { order(created_at: :desc) }scope :for_date_range, ->(start_date, end_date) { where(created_at: start_date..end_date) }+ # Standard USD pricing per 1M tokens by provider and model, refreshed by+ # bin/update_llm_pricing. Sources and estimation limits: docs/hosting/ai.md#cost-considerations+ pricing_config = YAML.load_file(Rails.root.join("config/llm_pricing.yml"), permitted_classes: [ Date ], aliases: true)+# Last complete review of every listed model, not a provider price-change date.- # Sources, dated changes and estimation limits: docs/hosting/ai.md#cost-considerations- PRICING_VERIFIED_ON = Date.new(2026, 10, 17).freeze-- # Standard USD pricing per 1M tokens, excluding regional and service-tier modifiers.- # Source: https://developers.openai.com/api/docs/pricing- PRICING = {- "openai" => {- # GPT-6 Astra, GPT-6.1 Terra and GPT-6 Luna Standard pricing, November 2026.- # Source: https://developers.openai.com/api/docs/models/gpt-6-astra (and -6.1-terra, -6-luna)- "gpt-6-astra" => {- prompt: 10.00, completion: 50.00,- long_context: { threshold: 272_000, prompt: 20.00, completion: 75.00 }- },- "gpt-6.1-terra" => {- prompt: 1.00, completion: 5.00,- long_context: { threshold: 272_000, prompt: 2.00, completion: 7.50 }- },- "gpt-6-luna" => {- prompt: 0.10, completion: 0.50,- long_context: { threshold: 272_000, prompt: 0.20, completion: 0.75 }- },- # GPT-6.1 Sol Standard pricing, September 2026.- # Source: https://developers.openai.com/api/docs/models/gpt-6.1-sol- "gpt-6.1-sol" => {- prompt: 2.00, completion: 10.00,- long_context: { threshold: 272_000, prompt: 4.00, completion: 15.00 }- },- # GPT-6 Sol Standard pricing, September 2026.- # Source: https://developers.openai.com/api/docs/models/gpt-6-sol- "gpt-6-sol" => {- prompt: 2.00, completion: 10.00,- long_context: { threshold: 272_000, prompt: 4.00, completion: 15.00 }- },- # GPT-4.1 and similar models- "gpt-4.1" => { prompt: 2.00, completion: 8.00 },- "gpt-4.1-mini" => { prompt: 0.40, completion: 1.60 },- "gpt-4.1-nano" => { prompt: 0.10, completion: 0.40 },- # 4o- "gpt-4o" => { prompt: 2.50, completion: 10.00 },- "gpt-4o-2024-05-13" => { prompt: 5.00, completion: 15.00 },- "gpt-4o-mini" => { prompt: 0.15, completion: 0.60 },- # Specialized models- "chat-latest" => { prompt: 5.00, completion: 30.00 },- "gpt-5.3-codex" => { prompt: 1.75, completion: 14.00 },- # GPT-5 models- # Sol promotion effective 2026-08-21, available at least through 2026-11-21.- "gpt-5.6-sol" => {- prompt: 4.00, completion: 20.00,- long_context: { threshold: 272_000, prompt: 8.00, completion: 30.00 }- },- # OpenAI routes the gpt-5.6 alias to GPT-5.6 Sol.- "gpt-5.6" => {- prompt: 4.00, completion: 20.00,- long_context: { threshold: 272_000, prompt: 8.00, completion: 30.00 }- },- # Trusted-access cyber models; listed so they don't match the rows above.- "gpt-5.6-cyber" => { prompt: 12.50, completion: 75.00 },- "gpt-5.5-cyber" => { prompt: 12.50, completion: 75.00 },- # Terra and Luna price reductions effective 2026-07-30.- "gpt-5.6-terra" => {- prompt: 2.00, completion: 12.00,- long_context: { threshold: 272_000, prompt: 4.00, completion: 18.00 }- },- "gpt-5.6-luna" => {- prompt: 0.20, completion: 1.20,- long_context: { threshold: 272_000, prompt: 0.40, completion: 1.80 }- },- "gpt-5.5-pro" => {- prompt: 30.00, completion: 180.00,- long_context: { threshold: 272_000, prompt: 60.00, completion: 270.00 }- },- "gpt-5.5" => {- prompt: 5.00, completion: 30.00,- long_context: { threshold: 272_000, prompt: 10.00, completion: 45.00 }- },- "gpt-5.4" => {- prompt: 2.50, completion: 15.00,- long_context: { threshold: 272_000, prompt: 5.00, completion: 22.50 }- },- "gpt-5.4-mini" => { prompt: 0.75, completion: 4.50 },- "gpt-5.4-nano" => { prompt: 0.20, completion: 1.25 },- "gpt-5.4-pro" => {- prompt: 30.00, completion: 180.00,- long_context: { threshold: 272_000, prompt: 60.00, completion: 270.00 }- },- # GPT-5.2 Pro is published at 12x the GPT-5.2 Thinking rate.- "gpt-5.2-pro" => { prompt: 21.00, completion: 168.00 },- "gpt-5.2-chat-latest" => { prompt: 1.75, completion: 14.00 },- "gpt-5.2" => { prompt: 1.75, completion: 14.00 },- "gpt-5.1-chat-latest" => { prompt: 1.25, completion: 10.00 },- "gpt-5.1" => { prompt: 1.25, completion: 10.00 },- "gpt-5" => { prompt: 1.25, completion: 10.00 },- "gpt-5-mini" => { prompt: 0.25, completion: 2.00 },- "gpt-5-nano" => { prompt: 0.05, completion: 0.40 },- "gpt-5-pro" => { prompt: 15.00, completion: 120.00 },- # o1 models- "o1-mini" => { prompt: 1.10, completion: 4.40 },- "o1-pro" => { prompt: 150.00, completion: 600.00 },- "o1" => { prompt: 15.00, completion: 60.00 },- # o-series models- "o3" => { prompt: 2.00, completion: 8.00 },- "o4-mini" => { prompt: 1.10, completion: 4.40 },- "o3-mini" => { prompt: 1.10, completion: 4.40 },- "o3-pro" => { prompt: 20.00, completion: 80.00 },- # Tier fallbacks: an unlisted release of a tier (e.g. "gpt-6.2-sol",- # "gpt-7-astra") is estimated at that tier's newest listed rate.- "gpt-astra" => {- prompt: 10.00, completion: 50.00,- long_context: { threshold: 272_000, prompt: 20.00, completion: 75.00 }- },- "gpt-sol" => {- prompt: 2.00, completion: 10.00,- long_context: { threshold: 272_000, prompt: 4.00, completion: 15.00 }- },- "gpt-terra" => {- prompt: 1.00, completion: 5.00,- long_context: { threshold: 272_000, prompt: 2.00, completion: 7.50 }- },- "gpt-luna" => {- prompt: 0.10, completion: 0.50,- long_context: { threshold: 272_000, prompt: 0.20, completion: 0.75 }- }- },- # Source: https://ai.google.dev/gemini-api/docs/pricing- "google" => {- # Gemini 4 models- # Gemini 4 Argon promotional pricing through 2026-11-30: $2.00 / $10.00 ($4.00 / $20.00 starting December 2026)- "gemini-4-argon" => { prompt: 2.00, completion: 10.00 },- # Gemini 3 models- # Flash series promotion through 2026-12-31: $0.75 / $3.75 ($1.50 / $7.50 starting 2027)- "gemini-3.8-flash" => { prompt: 0.75, completion: 3.75 },- "gemini-3.7-flash" => { prompt: 0.75, completion: 3.75 },- "gemini-3.6-flash" => { prompt: 0.75, completion: 3.75 },- "gemini-3.5-flash" => { prompt: 1.50, completion: 9.00 },- "gemini-3.5-flash-lite" => { prompt: 0.30, completion: 2.50 },- "gemini-3.1-flash-lite" => { prompt: 0.25, completion: 1.50 },- "gemini-3.1-pro" => {- prompt: 2.00, completion: 12.00,- long_context: { threshold: 200_000, prompt: 4.00, completion: 18.00 }- },- "gemini-3.1-pro-preview" => {- prompt: 2.00, completion: 12.00,- long_context: { threshold: 200_000, prompt: 4.00, completion: 18.00 }- },- "gemini-3-flash" => { prompt: 0.50, completion: 3.00 },- "gemini-3-flash-preview" => { prompt: 0.50, completion: 3.00 },- # Gemini 2.5 models- "gemini-2.5-pro" => {- prompt: 1.25, completion: 10.00,- long_context: { threshold: 200_000, prompt: 2.50, completion: 15.00 }- },- "gemini-2.5-flash" => { prompt: 0.30, completion: 2.50 }- },- # Source: https://platform.claude.com/docs/en/about-claude/pricing- # cache_read is set only where Anthropic lists a rate other than 0.1x input.- "anthropic" => {- "claude-fable-5-1" => { prompt: 10.00, completion: 50.00, cache_read: 0.25 },- "claude-fable-5" => { prompt: 10.00, completion: 50.00 },- "claude-opus-5-5" => { prompt: 4.00, completion: 20.00, cache_read: 0.20 },- "claude-opus-5" => { prompt: 5.00, completion: 25.00 },- "claude-opus-4-8" => { prompt: 5.00, completion: 25.00 },- "claude-opus-4-7" => { prompt: 5.00, completion: 25.00 },- "claude-opus-4-6" => { prompt: 5.00, completion: 25.00 },- "claude-opus-4-5" => { prompt: 5.00, completion: 25.00 },- "claude-opus-4-1" => { prompt: 15.00, completion: 75.00 },- "claude-opus-4" => { prompt: 15.00, completion: 75.00 },- "claude-sonnet-5-5" => { prompt: 2.00, completion: 10.00, cache_read: 0.10 },- "claude-sonnet-5" => { prompt: 2.00, completion: 10.00 },- "claude-sonnet-4-6" => { prompt: 3.00, completion: 15.00 },- "claude-sonnet-4-5" => { prompt: 3.00, completion: 15.00 },- "claude-sonnet-4" => { prompt: 3.00, completion: 15.00 },- "claude-haiku-5-5" => {- prompt: 0.10, completion: 0.50,- long_context: { threshold: 100_000, prompt: 0.50, completion: 2.50 }- },- "claude-haiku-4-5" => { prompt: 1.00, completion: 5.00 },- # Family fallbacks: a Claude model released after this review (e.g.- # "claude-opus-6") is estimated at its family's newest listed rate.- "claude-fable" => { prompt: 10.00, completion: 50.00, cache_read: 0.25 },- "claude-opus" => { prompt: 4.00, completion: 20.00, cache_read: 0.20 },- "claude-sonnet" => { prompt: 2.00, completion: 10.00, cache_read: 0.10 },- "claude-haiku" => {- prompt: 0.10, completion: 0.50,- long_context: { threshold: 100_000, prompt: 0.50, completion: 2.50 }- }- }- }.freeze+ PRICING_VERIFIED_ON = pricing_config.fetch("verified_on").freeze++ PRICING = pricing_config.fetch("providers").transform_values do |models|+ models.transform_values { |pricing| pricing.deep_symbolize_keys.freeze }.freeze+ end.freeze# Calculate cost for a model and token usage# Provider is automatically inferred from the model using the pricing mapdiff --git a/bin/update_llm_pricing b/bin/update_llm_pricingnew file mode 100755index 0000000000..edfde17867--- /dev/null+++ b/bin/update_llm_pricing@@ -0,0 +1,9 @@+#!/usr/bin/env ruby+# Refreshes config/llm_pricing.yml from LiteLLM's price list and prints a+# Markdown summary of the changes (used as the pull request body).+require_relative "../lib/llm_pricing_updater"++path = File.expand_path("../config/llm_pricing.yml", __dir__)+result = LlmPricingUpdater.new(File.read(path), LlmPricingUpdater.fetch_prices).call+File.write(path, result.yaml)+puts result.summarydiff --git a/config/llm_pricing.yml b/config/llm_pricing.ymlnew file mode 100644index 0000000000..13c9210a62--- /dev/null+++ b/config/llm_pricing.yml@@ -0,0 +1,126 @@+# Standard USD pricing per 1M tokens, excluding regional and service-tier+# modifiers. Loaded into LlmUsage::PRICING. Sources, dated changes and+# estimation limits: docs/hosting/ai.md#cost-considerations+#+# Keep one model per line. bin/update_llm_pricing (run by the "Refresh LLM+# pricing" workflow) rewrites these lines from LiteLLM's price list and adds+# new Claude and GPT tier releases, keeping comments and aliases intact.+# Providers are checked in this order when inferring a model's provider.++# Last complete review of every listed model against official sources, not a+# provider price-change date. Shown on the LLM usage screen.+verified_on: 2026-10-17++providers:+ # Source: https://developers.openai.com/api/docs/pricing+ openai:+ # GPT-6 Astra, GPT-6.1 Terra and GPT-6 Luna Standard pricing, November 2026.+ gpt-6-astra: &gpt-6-astra { prompt: 10.0, completion: 50.0, long_context: { threshold: 272000, prompt: 20.0, completion: 75.0 } }+ gpt-6.1-terra: &gpt-6-1-terra { prompt: 1.0, completion: 5.0, long_context: { threshold: 272000, prompt: 2.0, completion: 7.5 } }+ gpt-6-luna: &gpt-6-luna { prompt: 0.1, completion: 0.5, long_context: { threshold: 272000, prompt: 0.2, completion: 0.75 } }+ # GPT-6.1 Sol and GPT-6 Sol Standard pricing, September 2026.+ gpt-6.1-sol: &gpt-6-1-sol { prompt: 2.0, completion: 10.0, long_context: { threshold: 272000, prompt: 4.0, completion: 15.0 } }+ gpt-6-sol: { prompt: 2.0, completion: 10.0, long_context: { threshold: 272000, prompt: 4.0, completion: 15.0 } }+ # GPT-4.1 and similar models+ gpt-4.1: { prompt: 2.0, completion: 8.0 }+ gpt-4.1-mini: { prompt: 0.4, completion: 1.6 }+ gpt-4.1-nano: { prompt: 0.1, completion: 0.4 }+ # 4o+ gpt-4o: { prompt: 2.5, completion: 10.0 }+ gpt-4o-2024-05-13: { prompt: 5.0, completion: 15.0 }+ gpt-4o-mini: { prompt: 0.15, completion: 0.6 }+ # Specialized models+ chat-latest: { prompt: 5.0, completion: 30.0 }+ gpt-5.3-codex: { prompt: 1.75, completion: 14.0 }+ # GPT-5 models+ # Sol promotion effective 2026-08-21, available at least through 2026-11-21.+ gpt-5.6-sol: &gpt-5-6-sol { prompt: 4.0, completion: 20.0, long_context: { threshold: 272000, prompt: 8.0, completion: 30.0 } }+ # OpenAI routes the gpt-5.6 alias to GPT-5.6 Sol.+ gpt-5.6: *gpt-5-6-sol+ # Trusted-access cyber models; listed so they don't match the rows above.+ gpt-5.6-cyber: { prompt: 12.5, completion: 75.0 }+ gpt-5.5-cyber: { prompt: 12.5, completion: 75.0 }+ # Terra and Luna price reductions effective 2026-07-30.+ gpt-5.6-terra: { prompt: 2.0, completion: 12.0, long_context: { threshold: 272000, prompt: 4.0, completion: 18.0 } }+ gpt-5.6-luna: { prompt: 0.2, completion: 1.2, long_context: { threshold: 272000, prompt: 0.4, completion: 1.8 } }+ gpt-5.5-pro: { prompt: 30.0, completion: 180.0, long_context: { threshold: 272000, prompt: 60.0, completion: 270.0 } }+ gpt-5.5: { prompt: 5.0, completion: 30.0, long_context: { threshold: 272000, prompt: 10.0, completion: 45.0 } }+ gpt-5.4: { prompt: 2.5, completion: 15.0, long_context: { threshold: 272000, prompt: 5.0, completion: 22.5 } }+ gpt-5.4-mini: { prompt: 0.75, completion: 4.5 }+ gpt-5.4-nano: { prompt: 0.2, completion: 1.25 }+ gpt-5.4-pro: { prompt: 30.0, completion: 180.0, long_context: { threshold: 272000, prompt: 60.0, completion: 270.0 } }+ # GPT-5.2 Pro is published at 12x the GPT-5.2 Thinking rate.+ gpt-5.2-pro: { prompt: 21.0, completion: 168.0 }+ gpt-5.2-chat-latest: { prompt: 1.75, completion: 14.0 }+ gpt-5.2: { prompt: 1.75, completion: 14.0 }+ gpt-5.1-chat-latest: { prompt: 1.25, completion: 10.0 }+ gpt-5.1: { prompt: 1.25, completion: 10.0 }+ gpt-5: { prompt: 1.25, completion: 10.0 }+ gpt-5-mini: { prompt: 0.25, completion: 2.0 }+ gpt-5-nano: { prompt: 0.05, completion: 0.4 }+ gpt-5-pro: { prompt: 15.0, completion: 120.0 }+ # o1 models+ o1-mini: { prompt: 1.1, completion: 4.4 }+ o1-pro: { prompt: 150.0, completion: 600.0 }+ o1: { prompt: 15.0, completion: 60.0 }+ # o-series models+ o3: { prompt: 2.0, completion: 8.0 }+ o4-mini: { prompt: 1.1, completion: 4.4 }+ o3-mini: { prompt: 1.1, completion: 4.4 }+ o3-pro: { prompt: 20.0, completion: 80.0 }+ # Tier fallbacks: an unlisted release of a tier (e.g. "gpt-6.2-sol",+ # "gpt-7-astra") is estimated at that tier's newest listed rate. Point them+ # at a newer release when one is added.+ gpt-astra: *gpt-6-astra+ gpt-sol: *gpt-6-1-sol+ gpt-terra: *gpt-6-1-terra+ gpt-luna: *gpt-6-luna++ # Source: https://ai.google.dev/gemini-api/docs/pricing+ google:+ # Gemini 4 models+ # Gemini 4 Argon promotional pricing through 2026-11-30: $2.00 / $10.00 ($4.00 / $20.00 starting December 2026)+ gemini-4-argon: { prompt: 2.0, completion: 10.0 }+ # Gemini 3 models+ # Flash series promotion through 2026-12-31: $0.75 / $3.75 ($1.50 / $7.50 starting 2027)+ gemini-3.8-flash: { prompt: 0.75, completion: 3.75 }+ gemini-3.7-flash: { prompt: 0.75, completion: 3.75 }+ gemini-3.6-flash: { prompt: 0.75, completion: 3.75 }+ gemini-3.5-flash: { prompt: 1.5, completion: 9.0 }+ gemini-3.5-flash-lite: { prompt: 0.3, completion: 2.5 }+ gemini-3.1-flash-lite: { prompt: 0.25, completion: 1.5 }+ gemini-3.1-pro: { prompt: 2.0, completion: 12.0, long_context: { threshold: 200000, prompt: 4.0, completion: 18.0 } }+ gemini-3.1-pro-preview: { prompt: 2.0, completion: 12.0, long_context: { threshold: 200000, prompt: 4.0, completion: 18.0 } }+ gemini-3-flash: { prompt: 0.5, completion: 3.0 }+ gemini-3-flash-preview: { prompt: 0.5, completion: 3.0 }+ # Gemini 2.5 models+ gemini-2.5-pro: { prompt: 1.25, completion: 10.0, long_context: { threshold: 200000, prompt: 2.5, completion: 15.0 } }+ gemini-2.5-flash: { prompt: 0.3, completion: 2.5 }++ # Source: https://platform.claude.com/docs/en/about-claude/pricing+ # cache_read is set only where Anthropic lists a rate other than 0.1x input.+ anthropic:+ claude-fable-5-1: &claude-fable-5-1 { prompt: 10.0, completion: 50.0, cache_read: 0.25 }+ claude-fable-5: { prompt: 10.0, completion: 50.0 }+ claude-opus-5-5: &claude-opus-5-5 { prompt: 4.0, completion: 20.0, cache_read: 0.2 }+ claude-opus-5: { prompt: 5.0, completion: 25.0 }+ claude-opus-4-8: { prompt: 5.0, completion: 25.0 }+ claude-opus-4-7: { prompt: 5.0, completion: 25.0 }+ claude-opus-4-6: { prompt: 5.0, completion: 25.0 }+ claude-opus-4-5: { prompt: 5.0, completion: 25.0 }+ claude-opus-4-1: { prompt: 15.0, completion: 75.0 }+ claude-opus-4: { prompt: 15.0, completion: 75.0 }+ claude-sonnet-5-5: &claude-sonnet-5-5 { prompt: 2.0, completion: 10.0, cache_read: 0.1 }+ claude-sonnet-5: { prompt: 2.0, completion: 10.0 }+ claude-sonnet-4-6: { prompt: 3.0, completion: 15.0 }+ claude-sonnet-4-5: { prompt: 3.0, completion: 15.0 }+ claude-sonnet-4: { prompt: 3.0, completion: 15.0 }+ claude-haiku-5-5: &claude-haiku-5-5 { prompt: 0.1, completion: 0.5, long_context: { threshold: 100000, prompt: 0.5, completion: 2.5 } }+ claude-haiku-4-5: { prompt: 1.0, completion: 5.0 }+ # Family fallbacks: a Claude model released after this review (e.g.+ # "claude-opus-6") is estimated at its family's newest listed rate. Point+ # them at a newer release when one is added.+ claude-fable: *claude-fable-5-1+ claude-opus: *claude-opus-5-5+ claude-sonnet: *claude-sonnet-5-5+ claude-haiku: *claude-haiku-5-5diff --git a/docs/hosting/ai.md b/docs/hosting/ai.mdindex 5c521ded33..2e1febb75a 100644--- a/docs/hosting/ai.md+++ b/docs/hosting/ai.md@@ -1135,13 +1135,24 @@ Good test queries that exercise different capabilities:### Cloud Costs**Full pricing table last verified: October 17, 2026.** Every model-listed in [`LlmUsage::PRICING`](../../app/models/llm_usage.rb) was checked against-official sources on that date, except the Claude and OpenAI rows: those were-last reviewed on November 1, 2026, without re-checking the Gemini rows.-`PRICING_VERIFIED_ON` supplies the full-table verification date shown on+listed in [`config/llm_pricing.yml`](../../config/llm_pricing.yml) was checked+against official sources on that date, except the Claude and OpenAI rows: those+were last reviewed on November 1, 2026, without re-checking the Gemini rows.+The file's `verified_on` supplies the full-table verification date shown onthe LLM usage screen. This is a manual review date, not a claim that providerschanged every price that day.+The **Refresh LLM pricing** workflow runs on every release tag and on demand.+It runs `bin/update_llm_pricing`, which reads+[LiteLLM's price list](https://github.com/BerriAI/litellm/blob/main/model_prices_and_context_window.json)+and then opens a pull request. While that pull request is open, later runs+skip, so nothing overwrites a review in progress. The pull request rewrites listed+rows whose prices changed and adds new Claude and GPT tier releases (for+example `claude-opus-6` or `gpt-6.2-sol`). Its description lists the changes+and any rows LiteLLM does not have. Check each change against the provider's+pricing page before merging. After adding a newer release, point the family or+tier fallback alias at it. Bump `verified_on` only after reviewing every row.+Rates are USD per million tokens at the Standard tier. The source coverage is:| Models in the code | Official pricing source |diff --git a/lib/llm_pricing_updater.rb b/lib/llm_pricing_updater.rbnew file mode 100644index 0000000000..fca8d9cd5b--- /dev/null+++ b/lib/llm_pricing_updater.rb@@ -0,0 +1,167 @@+require "json"+require "net/http"+require "yaml"+require "date"++# Refreshes config/llm_pricing.yml from LiteLLM's price list. Model rows are+# rewritten line by line, so comments and fallback aliases survive, and new+# Claude and GPT tier releases are added. Run by bin/update_llm_pricing.+class LlmPricingUpdater+ SOURCE_URL = "https://raw.githubusercontent.com/BerriAI/litellm/main/model_prices_and_context_window.json".freeze++ # Sure provider => [LiteLLM key prefix, LiteLLM provider]+ LITELLM = {+ "openai" => [ "", "openai" ],+ "google" => [ "gemini/", "gemini" ],+ "anthropic" => [ "", "anthropic" ]+ }.freeze++ # Releases the fallback rows would otherwise only estimate. Dated snapshots+ # and "-4-0" style aliases are left to the existing rows.+ NEW_MODELS = {+ "openai" => /\Agpt-\d+(\.\d+)?-(astra|sol|terra|luna)\z/,+ "anthropic" => /\Aclaude-(fable|opus|sonnet|haiku)-\d{1,2}(-[1-9]\d?)?\z/+ }.freeze++ PROVIDER_LINE = /\A (?<provider>[a-z]+):\s*\z/+ KEY_LINE = /\A {4}(?<model>[\w.\-]+):/+ MODEL_LINE = /\A(?<indent> {4})(?<model>[\w.\-]+): (?<anchor>&[\w.\-]+ )?(?<row>\{[^#]*\})(?<comment> +#.*)?\z/+ # Anything above this per 1M tokens is treated as bad source data.+ MAX_PRICE = 10_000++ Result = Struct.new(:yaml, :changed, :added, :missing, keyword_init: true) do+ def summary+ [+ "Prices from [LiteLLM's price list](#{SOURCE_URL}). Check each change against the provider's official pricing page before merging; update `verified_on` only after reviewing every row.",+ section("Changed", changed),+ section("Added (point the family or tier fallback at the newest release if needed)", added),+ section("Left unchanged", missing)+ ].compact.join("\n\n")+ end++ private+ def section(title, items)+ "### #{title}\n\n#{items.map { |item| "- #{item}" }.join("\n")}" if items.any?+ end+ end++ def self.fetch_prices+ response = Net::HTTP.get_response(URI(SOURCE_URL))+ raise "LiteLLM price list request failed: HTTP #{response.code}" unless response.is_a?(Net::HTTPSuccess)++ JSON.parse(response.body)+ end++ def initialize(yaml, prices)+ @lines = yaml.lines(chomp: true)+ @prices = prices+ end++ def call+ changed = []+ missing = []+ listed = Hash.new { |hash, provider| hash[provider] = [] }+ provider = nil++ lines = @lines.map do |line|+ if (match = PROVIDER_LINE.match(line))+ provider = match[:provider]+ next line+ end++ key = KEY_LINE.match(line)+ listed[provider] << key[:model] if key+ match = MODEL_LINE.match(line)+ next line unless match && LITELLM.key?(provider)++ model = match[:model]+ row = litellm_row(provider, model)+ current = YAML.safe_load(match[:row])++ if row.nil?+ missing << "`#{provider}/#{model}`: not in LiteLLM, or its entry is incomplete"+ line+ elsif current["long_context"] && !row["long_context"]+ # LiteLLM often omits tiers; removing a reviewed one is a manual call.+ missing << "`#{provider}/#{model}`: LiteLLM lists no long-context tier"+ line+ elsif row == current+ line+ else+ changed << "`#{provider}/#{model}`: `#{flow(current)}` → `#{flow(row)}`"+ "#{match[:indent]}#{model}: #{match[:anchor]}#{flow(row)}#{match[:comment]}"+ end+ end++ added = add_new_models(lines, listed)+ yaml = lines.map { |line| "#{line}\n" }.join+ YAML.safe_load(yaml, permitted_classes: [ Date ], aliases: true) # never write a file that stops loading++ Result.new(yaml: yaml, changed: changed, added: added, missing: missing)+ end++ private+ def add_new_models(lines, listed)+ NEW_MODELS.flat_map do |provider, pattern|+ prefix, litellm_provider = LITELLM.fetch(provider)+ models = @prices.keys.filter_map do |key|+ model = key.delete_prefix(prefix)+ model if key.start_with?(prefix) && pattern.match?(model) && !listed[provider].include?(model) &&+ @prices[key].is_a?(Hash) && @prices[key]["litellm_provider"] == litellm_provider+ end+ rows = models.sort.reverse.filter_map do |model|+ row = litellm_row(provider, model)+ [ model, row ] if row+ end+ next [] if rows.empty?++ header = lines.index { |line| line == " #{provider}:" }+ lines.insert(header + 1, " # Added by bin/update_llm_pricing; check against the official pricing page.",+ *rows.map { |model, row| " #{model}: #{flow(row)}" })+ rows.map { |model, row| "`#{provider}/#{model}`: `#{flow(row)}`" }+ end+ end++ def litellm_row(provider, model)+ prefix, litellm_provider = LITELLM.fetch(provider)+ entry = @prices["#{prefix}#{model}"]+ return unless entry.is_a?(Hash) && entry["litellm_provider"] == litellm_provider++ prompt = per_million(entry["input_cost_per_token"])+ completion = per_million(entry["output_cost_per_token"])+ return unless prompt && completion++ row = { "prompt" => prompt, "completion" => completion }+ # Sure prices cache reads for Anthropic only, at 0.1x input unless set.+ # Without a source rate, a reviewed cache_read would silently revert to 0.1x.+ if provider == "anthropic"+ cache_read = per_million(entry["cache_read_input_token_cost"])+ return unless cache_read++ row["cache_read"] = cache_read if cache_read != (prompt * 0.1).round(6)+ end++ threshold = entry.keys.filter_map { |key| key[/\Ainput_cost_per_token_above_(\d+)k_tokens\z/, 1]&.to_i }.min+ if threshold+ long_prompt = per_million(entry["input_cost_per_token_above_#{threshold}k_tokens"])+ long_completion = per_million(entry["output_cost_per_token_above_#{threshold}k_tokens"])+ # A partial tier is bad data: dropping it would underprice long prompts.+ return unless long_prompt && long_completion++ row["long_context"] = { "threshold" => threshold * 1000, "prompt" => long_prompt, "completion" => long_completion }+ end++ row+ end++ def per_million(cost)+ return unless cost.is_a?(Numeric) && cost.finite? && cost >= 0++ price = (cost * 1_000_000).round(6)+ price.to_f if price <= MAX_PRICE+ end++ def flow(row)+ "{ #{row.map { |key, value| "#{key}: #{value.is_a?(Hash) ? flow(value) : value}" }.join(", ")} }"+ end+enddiff --git a/test/lib/llm_pricing_updater_test.rb b/test/lib/llm_pricing_updater_test.rbnew file mode 100644index 0000000000..9461754469--- /dev/null+++ b/test/lib/llm_pricing_updater_test.rb@@ -0,0 +1,121 @@+require "test_helper"++class LlmPricingUpdaterTest < ActiveSupport::TestCase+ CONFIG = <<~YAML+ verified_on: 2026-10-17++ providers:+ openai:+ # GPT-6+ gpt-6.1-sol: &gpt-6-1-sol { prompt: 2.0, completion: 10.0 } # promotion {ends Q4}+ gpt-4.1: { prompt: 2.0, completion: 8.0 }+ gpt-sol: *gpt-6-1-sol+ anthropic:+ claude-opus-5-5: &claude-opus-5-5 { prompt: 4.0, completion: 20.0, cache_read: 0.2 }+ claude-opus: *claude-opus-5-5+ YAML++ test "rewrites changed rows in place, keeping comments, anchors and aliases" do+ result = update(+ "gpt-6.1-sol" => litellm(3, 12, "input_cost_per_token_above_272k_tokens" => 6e-6, "output_cost_per_token_above_272k_tokens" => 18e-6),+ "gpt-4.1" => litellm(2, 8),+ "claude-opus-5-5" => litellm(4, 20, provider: "anthropic", "cache_read_input_token_cost" => 0.2e-6)+ )++ assert_includes result.yaml, " gpt-6.1-sol: &gpt-6-1-sol { prompt: 3.0, completion: 12.0, " \+ "long_context: { threshold: 272000, prompt: 6.0, completion: 18.0 } } # promotion {ends Q4}\n"+ assert_includes result.yaml, " # GPT-6\n"+ assert_includes result.yaml, " gpt-4.1: { prompt: 2.0, completion: 8.0 }\n"+ assert_equal 1, result.changed.size+ assert_empty result.added+ assert_empty result.missing++ loaded = YAML.safe_load(result.yaml, permitted_classes: [ Date ], aliases: true)+ assert_equal 3.0, loaded.dig("providers", "openai", "gpt-sol", "prompt")+ end++ test "adds new Claude and GPT tier releases only" do+ result = update(+ "claude-opus-6" => litellm(4, 20, provider: "anthropic", "cache_read_input_token_cost" => 0.4e-6),+ "claude-sonnet-6" => litellm(2, 10, provider: "anthropic", "cache_read_input_token_cost" => 0.1e-6),+ "claude-opus-4-20250514" => litellm(15, 75, provider: "anthropic"),+ "claude-opus-4-0" => litellm(15, 75, provider: "anthropic"),+ "claude-haiku-6" => litellm(0.1, 0.5, provider: "bedrock"),+ "gpt-6.2-sol" => litellm(2, 10),+ "gpt-5.9" => litellm(1, 8)+ )++ lines = result.yaml.lines(chomp: true)+ assert_equal " claude-opus-6: { prompt: 4.0, completion: 20.0 }", lines[lines.index(" anthropic:") + 3]+ assert_equal " claude-sonnet-6: { prompt: 2.0, completion: 10.0, cache_read: 0.1 }", lines[lines.index(" anthropic:") + 2]+ assert_equal " gpt-6.2-sol: { prompt: 2.0, completion: 10.0 }", lines[lines.index(" openai:") + 2]+ assert_equal 3, result.added.size+ end++ test "skips source data it cannot trust and reports those rows as missing" do+ result = update(+ "gpt-6.1-sol" => litellm(-1, 10),+ "gpt-4.1" => litellm(2, 8, provider: "azure"),+ "claude-opus-5-5" => litellm(4, "20", provider: "anthropic")+ )++ assert_equal CONFIG, result.yaml+ assert_equal 3, result.missing.size+ end++ test "keeps reviewed long-context tiers and cache rates that LiteLLM cannot confirm" do+ config = <<~YAML+ providers:+ openai:+ gpt-5.4: { prompt: 2.5, completion: 15.0, long_context: { threshold: 272000, prompt: 5.0, completion: 22.5 } }+ gpt-5.5: { prompt: 5.0, completion: 30.0, long_context: { threshold: 272000, prompt: 10.0, completion: 45.0 } }+ anthropic:+ claude-opus-5-5: { prompt: 4.0, completion: 20.0, cache_read: 0.2 }+ YAML++ result = LlmPricingUpdater.new(config,+ "gpt-5.4" => litellm(3, 15),+ "gpt-5.5" => litellm(6, 30, "input_cost_per_token_above_272k_tokens" => 12e-6, "output_cost_per_token_above_272k_tokens" => "45"),+ "claude-opus-5-5" => litellm(4, 20, provider: "anthropic")+ ).call++ assert_equal config, result.yaml+ assert_empty result.changed+ assert_equal 3, result.missing.size+ end++ test "the bundled config is unchanged when LiteLLM matches it" do+ prices = LlmUsage::PRICING.flat_map do |provider, models|+ prefix, litellm_provider = LlmPricingUpdater::LITELLM.fetch(provider)+ models.map do |model, pricing|+ long_context = pricing[:long_context]+ extra = { "cache_read_input_token_cost" => (pricing[:cache_read] || pricing[:prompt] * 0.1) / 1e6 }+ if long_context+ tokens = "above_#{long_context[:threshold] / 1000}k_tokens"+ extra["input_cost_per_token_#{tokens}"] = long_context[:prompt] / 1e6+ extra["output_cost_per_token_#{tokens}"] = long_context[:completion] / 1e6+ end+ [ "#{prefix}#{model}", litellm(pricing[:prompt], pricing[:completion], provider: litellm_provider, **extra) ]+ end+ end.to_h+ config = Rails.root.join("config/llm_pricing.yml").read++ result = LlmPricingUpdater.new(config, prices).call++ assert_empty result.changed+ assert_empty result.added+ assert_empty result.missing+ assert_equal config, result.yaml+ end++ private+ def update(prices)+ LlmPricingUpdater.new(CONFIG, prices).call+ end++ def litellm(input, output, provider: "openai", **extra)+ input = input / 1e6 if input.is_a?(Numeric)+ output = output / 1e6 if output.is_a?(Numeric)+ { "litellm_provider" => provider, "input_cost_per_token" => input, "output_cost_per_token" => output }.merge(extra.transform_keys(&:to_s))+ end+end