Module: LlmCostTracker::Integrations::Base

Included in:
Anthropic, Openai
Defined in:
lib/llm_cost_tracker/integrations/base.rb

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/llm_cost_tracker/integrations/base.rb', line 11

def active?
  LlmCostTracker.configuration.instrumented?(integration_name)
end

#constant(path) ⇒ Object



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

def constant(path)
  path.to_s.split("::").reduce(Object) do |scope, const_name|
    return nil unless scope.const_defined?(const_name, false)

    scope.const_get(const_name, false)
  end
end

#elapsed_ms(started_at) ⇒ Object



29
30
31
# File 'lib/llm_cost_tracker/integrations/base.rb', line 29

def elapsed_ms(started_at)
  ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - started_at) * 1000).round
end

#enforce_budget!Object



33
34
35
# File 'lib/llm_cost_tracker/integrations/base.rb', line 33

def enforce_budget!
  LlmCostTracker::Tracker.enforce_budget! if active?
end

#installObject



15
16
17
# File 'lib/llm_cost_tracker/integrations/base.rb', line 15

def install
  target_patches.each { |target, patch| install_patch(target, patch) }
end

#record_safelyObject



37
38
39
40
41
42
43
# File 'lib/llm_cost_tracker/integrations/base.rb', line 37

def record_safely
  yield
rescue LlmCostTracker::Error
  raise
rescue StandardError => e
  Logging.warn("#{integration_name} integration failed to record usage: #{e.class}: #{e.message}")
end

#request_params(args, kwargs) ⇒ Object



45
46
47
48
# File 'lib/llm_cost_tracker/integrations/base.rb', line 45

def request_params(args, kwargs)
  params = args.first.is_a?(Hash) ? args.first : {}
  params.merge(kwargs)
end

#statusObject



19
20
21
22
23
24
25
26
27
# File 'lib/llm_cost_tracker/integrations/base.rb', line 19

def status
  name = integration_name
  installed = target_patches.count { |target, patch| patch_installed?(target, patch) }
  available = target_patches.count { |target, _patch| target }
  return Result.new(name, :ok, "#{name} integration installed") if installed.positive?
  return Result.new(name, :warn, "#{name} SDK classes are not loaded") if available.zero?

  Result.new(name, :warn, "#{name} integration is enabled but not installed")
end