Module: LlmCostTracker::Integrations::Base

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

Defined Under Namespace

Classes: PatchTarget, Result

Instance Method Summary collapse

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


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

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

#constant(path) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/llm_cost_tracker/integrations/base.rb', line 59

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



38
39
40
# File 'lib/llm_cost_tracker/integrations/base.rb', line 38

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

#enforce_budget!Object



42
43
44
# File 'lib/llm_cost_tracker/integrations/base.rb', line 42

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

#installObject



16
17
18
19
20
21
22
# File 'lib/llm_cost_tracker/integrations/base.rb', line 16

def install
  validate_contract!
  patch_targets.each do |target|
    target_class = constant(target.constant_name)
    install_patch(target_class, target.patch) if target_class
  end
end

#minimum_versionObject



67
# File 'lib/llm_cost_tracker/integrations/base.rb', line 67

def minimum_version = nil

#patch_target(constant_name, with:, methods:, optional: false) ⇒ Object



73
74
75
# File 'lib/llm_cost_tracker/integrations/base.rb', line 73

def patch_target(constant_name, with:, methods:, optional: false)
  PatchTarget.new(constant_name, with, Array(methods), optional)
end

#patch_targetsObject



71
# File 'lib/llm_cost_tracker/integrations/base.rb', line 71

def patch_targets = []

#record_safelyObject



46
47
48
49
50
51
52
# File 'lib/llm_cost_tracker/integrations/base.rb', line 46

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



54
55
56
57
# File 'lib/llm_cost_tracker/integrations/base.rb', line 54

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

#statusObject



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/llm_cost_tracker/integrations/base.rb', line 24

def status
  name = integration_name
  problems = contract_problems
  if problems.any?
    return Result.new(name, :warn, "#{name} integration cannot be installed: #{problems.join('; ')}")
  end

  required_targets = patch_targets.reject(&:optional)
  installed = required_targets.count { |target| patch_installed?(constant(target.constant_name), target.patch) }
  return Result.new(name, :ok, "#{name} integration installed") if installed == required_targets.count

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

#version_constantObject



69
# File 'lib/llm_cost_tracker/integrations/base.rb', line 69

def version_constant = nil