Module: LlmCostTracker::Integrations

Defined in:
lib/llm_cost_tracker/integrations.rb,
lib/llm_cost_tracker/integrations/base.rb,
lib/llm_cost_tracker/integrations/openai.rb,
lib/llm_cost_tracker/integrations/ruby_llm.rb,
lib/llm_cost_tracker/integrations/anthropic.rb

Defined Under Namespace

Modules: Anthropic, Base, Openai, RubyLlm

Constant Summary collapse

INTEGRATION_CONSTANTS =
{ openai: :Openai, anthropic: :Anthropic, ruby_llm: :RubyLlm }.freeze

Class Method Summary collapse

Class Method Details

.checks(names = LlmCostTracker.configuration.instrumented_integrations) ⇒ Object



25
26
27
28
29
# File 'lib/llm_cost_tracker/integrations.rb', line 25

def checks(names = LlmCostTracker.configuration.instrumented_integrations)
  return [Base::Result.new(:ok, "integrations", "no SDK integrations enabled")] if names.empty?

  normalize(names).map { |name| fetch(name).status }
end

.fetch(name) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/llm_cost_tracker/integrations.rb', line 48

def fetch(name)
  const_name = INTEGRATION_CONSTANTS[name.to_sym]
  unless const_name
    raise LlmCostTracker::Error,
          "Unknown integration: #{name.inspect}. Use one of: #{names.join(', ')}"
  end

  const_get(const_name)
end

.install!(names = LlmCostTracker.configuration.instrumented_integrations) ⇒ Object



19
20
21
22
23
# File 'lib/llm_cost_tracker/integrations.rb', line 19

def install!(names = LlmCostTracker.configuration.instrumented_integrations)
  normalized = normalize(names)
  warn_double_instrumentation(normalized)
  normalized.each { |name| fetch(name).install }
end

.namesObject



58
59
60
# File 'lib/llm_cost_tracker/integrations.rb', line 58

def names
  INTEGRATION_CONSTANTS.keys
end

.normalize(names) ⇒ Object



31
32
33
# File 'lib/llm_cost_tracker/integrations.rb', line 31

def normalize(names)
  Array(names).flatten.uniq
end

.warn_double_instrumentation(names) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/llm_cost_tracker/integrations.rb', line 35

def warn_double_instrumentation(names)
  return unless names.include?(:ruby_llm)

  overlapping = names & DOUBLE_INSTRUMENTATION_OVERLAPS
  return if overlapping.empty?

  Logging.warn(
    ":ruby_llm is enabled together with #{overlapping.map(&:inspect).join(', ')}. " \
    "RubyLLM uses HTTP underneath, so calls routed to those providers may be recorded twice " \
    "(once via the SDK patch, once via the Faraday parser). Pick one path per provider."
  )
end