Class: LlmCostTracker::PriceSync::Sources::Litellm

Inherits:
LlmCostTracker::PriceSync::Source show all
Defined in:
lib/llm_cost_tracker/price_sync/sources/litellm.rb

Constant Summary collapse

PER_TOKEN_TO_PER_MILLION =
1_000_000
SUPPORTED_MODES =
%w[chat completion embedding responses].freeze
SUPPORTED_PROVIDERS =
%w[openai anthropic gemini text-completion-openai].freeze
URL =
"https://raw.githubusercontent.com/BerriAI/litellm/main/model_prices_and_context_window.json"

Instance Method Summary collapse

Methods inherited from LlmCostTracker::PriceSync::Source

#name

Instance Method Details

#fetch(current_models:, fetcher:) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/llm_cost_tracker/price_sync/sources/litellm.rb', line 22

def fetch(current_models:, fetcher:)
  response = fetcher.get(url)
  payload = JSON.parse(response.body.to_s)

  prices = []
  missing_models = []

  current_models.each_key do |our_model|
    entry_id = ModelCatalog.resolve_from_litellm(our_model, payload)
    entry = entry_id && payload[entry_id]

    if entry && supported_entry?(entry)
      prices << build_raw_price(our_model, entry, response)
    else
      missing_models << our_model
    end
  end

  SourceResult.new(
    prices: prices,
    missing_models: missing_models.sort,
    source_version: response_version(response)
  )
rescue JSON::ParserError => e
  raise Error, "Unable to parse #{url}: #{e.message}"
end

#priorityObject



14
15
16
# File 'lib/llm_cost_tracker/price_sync/sources/litellm.rb', line 14

def priority
  10
end

#urlObject



18
19
20
# File 'lib/llm_cost_tracker/price_sync/sources/litellm.rb', line 18

def url
  URL
end