Class: LlmCostTracker::PriceSync::Sources::OpenRouter

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

Constant Summary collapse

PER_TOKEN_TO_PER_MILLION =
1_000_000
SUPPORTED_PREFIXES =
%w[openai anthropic google].freeze
URL =
"https://openrouter.ai/api/v1/models"

Instance Method Summary collapse

Methods inherited from LlmCostTracker::PriceSync::Source

#name

Instance Method Details

#fetch(current_models:, fetcher:) ⇒ Object



21
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/open_router.rb', line 21

def fetch(current_models:, fetcher:)
  response = fetcher.get(url)
  payload = JSON.parse(response.body.to_s)
  index = payload.fetch("data", []).to_h { |entry| [entry["id"], entry] }

  prices = []
  missing_models = []

  current_models.each_key do |our_model|
    entry_id = ModelCatalog.resolve_from_openrouter(our_model, index)
    entry = entry_id && index[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



13
14
15
# File 'lib/llm_cost_tracker/price_sync/sources/open_router.rb', line 13

def priority
  20
end

#urlObject



17
18
19
# File 'lib/llm_cost_tracker/price_sync/sources/open_router.rb', line 17

def url
  URL
end